Java Code Examples for java.nio.Buffer#flip()

The following examples show how to use java.nio.Buffer#flip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferObject.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param newSize size required in bytes
 */
public void loadBufferData(Buffer buf, int newSize) {
    boolean clear = false;

    if (buf.position() != 0) {
        log.debug("flip your buffer!");
        buf.flip();
    }

    GLState.bindBuffer(target, id);

    /* reuse memory allocated for vbo when possible and allocated
     * memory is less then four times the new data */
    if (!GLAdapter.NO_BUFFER_SUB_DATA && !clear &&
            (size > newSize) && (size < newSize * 4)) {
        gl.bufferSubData(target, 0, newSize, buf);
    } else {
        mBufferMemoryUsage += newSize - size;
        size = newSize;
        //GL.bufferData(target, size, buf, GL20.DYNAMIC_DRAW);
        gl.bufferData(target, size, buf, GL.STATIC_DRAW);
    }
}
 
Example 2
Source File: Util.java    From feign with Apache License 2.0 6 votes vote down vote up
/**
 * Adapted from {@code com.google.common.io.CharStreams.toString()}.
 */
public static String toString(Reader reader) throws IOException {
  if (reader == null) {
    return null;
  }
  try {
    StringBuilder to = new StringBuilder();
    CharBuffer charBuf = CharBuffer.allocate(BUF_SIZE);
    // must cast to super class Buffer otherwise break when running with java 11
    Buffer buf = charBuf;
    while (reader.read(charBuf) != -1) {
      buf.flip();
      to.append(charBuf);
      buf.clear();
    }
    return to.toString();
  } finally {
    ensureClosed(reader);
  }
}
 
Example 3
Source File: SamplingProfiler.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private void readActivationEventsToBuffer(FileChannel activationEventsFileChannel, long eof, ByteBuffer byteBuffer) throws IOException {
    Buffer buf = byteBuffer;
    buf.clear();
    long remaining = eof - activationEventsFileChannel.position();
    activationEventsFileChannel.read(byteBuffer);
    buf.flip();
    if (remaining < buf.capacity()) {
        buf.limit((int) remaining);
    }
}
 
Example 4
Source File: SamplingProfiler.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
long startProcessingActivationEventsFile() throws IOException {
    Buffer activationEventsBuffer = this.activationEventsBuffer;
    if (activationEventsFileChannel.position() > 0) {
        flushActivationEvents();
        activationEventsBuffer.limit(0);
    } else {
        activationEventsBuffer.flip();
    }
    long eof = activationEventsFileChannel.position();
    activationEventsFileChannel.position(0);
    return eof;
}
 
Example 5
Source File: RenderUtils.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
static void nioBuffToGL(GL2GL3 gl, Renderer r, int bufferHandle, int itemSize, Buffer buff) {

	buff.flip();
	int size = buff.limit() * itemSize;
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, bufferHandle);
	gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, size, buff, GL2GL3.GL_STATIC_DRAW);
	gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, 0);
	r.usingVRAM(size);

}
 
Example 6
Source File: TensorTest.java    From java with Apache License 2.0 4 votes vote down vote up
private static void flipBuffer(Buffer buf) {
  buf.flip();
}
 
Example 7
Source File: Utils.java    From ShizuruNotes with Apache License 2.0 4 votes vote down vote up
static void flipBuffer(Buffer buffer) {
  buffer.flip();
}
 
Example 8
Source File: BufferHelper.java    From incubator-heron with Apache License 2.0 2 votes vote down vote up
/**
 * Flip the provided buffer.
 * <p>
 * This wrapper around {@link Buffer#flip()} is required because of
 * incompatible ABI changes between Java 8 and 11. In Java 8, {@link ByteBuffer#flip()} returns
 * a {@link Buffer}, whereas in Java 11, this method returns a {@link ByteBuffer}.
 * <p>
 * If this function is used, any object of {@link Buffer} (and subclasses) are first cast to
 * {@link Buffer} objects, then flipped, thus avoiding the binary incompatibility.
 *
 * @param buffer The buffer to flip
 */
static void flip(Buffer buffer) {
  buffer.flip();
}