java.nio.ByteBuffer.flip()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中java.nio.ByteBuffer.flip()方法的一些代码示例,展示了ByteBuffer.flip()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.flip()方法的具体详情如下:
包路径:java.nio.ByteBuffer
类名称:ByteBuffer
方法名:flip

ByteBuffer.flip介绍

暂无

代码示例

代码示例来源:origin: google/guava

/**
 * Flips the buffer output buffer so we can start reading bytes from it. If we are starting to
 * drain because there was overflow, and there aren't actually any characters to drain, then the
 * overflow must be due to a small output buffer.
 */
private void startDraining(boolean overflow) {
 byteBuffer.flip();
 if (overflow && byteBuffer.remaining() == 0) {
  byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2);
 } else {
  draining = true;
 }
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Reads and returns {@link ByteBuffer} from the underlying {@link SocketChannel} represented by
 * the <code>key</code>. Due to the fact that there is a dedicated channel for each client
 * connection we don't need to store the sender.
 */
@Override
public ByteBuffer read(SelectionKey key) throws IOException {
 SocketChannel socketChannel = (SocketChannel) key.channel();
 ByteBuffer buffer = ByteBuffer.allocate(1024);
 int read = socketChannel.read(buffer);
 buffer.flip();
 if (read == -1) {
  throw new IOException("Socket closed");
 }
 return buffer;
}

代码示例来源:origin: google/guava

private void munch() {
  buffer.flip();
  while (buffer.remaining() >= chunkSize) {
   // we could limit the buffer to ensure process() does not read more than
   // chunkSize number of bytes, but we trust the implementations
   process(buffer);
  }
  buffer.compact(); // preserve any remaining data that do not make a full chunk
 }
}

代码示例来源:origin: apache/incubator-druid

@Override
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out)
{
 final ByteBuffer copyBuffer = in.duplicate();
 copyBuffer.limit(copyBuffer.position() + numBytes);
 out.put(copyBuffer).flip();
 in.position(in.position() + numBytes);
}

代码示例来源:origin: runelite/runelite

private void ensureRemaining(int remaining)
{
  while (remaining > buffer.remaining())
  {
    int newCapacity = buffer.capacity() * 2;
    ByteBuffer old = buffer;
    old.flip();
    buffer = ByteBuffer.allocate(newCapacity);
    buffer.put(old);
  }
}

代码示例来源:origin: apache/kafka

/**
 * Check if the given ByteBuffer capacity
 * @param existingBuffer ByteBuffer capacity to check
 * @param newLength new length for the ByteBuffer.
 * returns ByteBuffer
 */
public static ByteBuffer ensureCapacity(ByteBuffer existingBuffer, int newLength) {
  if (newLength > existingBuffer.capacity()) {
    ByteBuffer newBuffer = ByteBuffer.allocate(newLength);
    existingBuffer.flip();
    newBuffer.put(existingBuffer);
    return newBuffer;
  }
  return existingBuffer;
}

代码示例来源:origin: google/ExoPlayer

@Override
public void queueInput(ByteBuffer inputBuffer) {
 Assertions.checkState(outputChannels != null);
 int position = inputBuffer.position();
 int limit = inputBuffer.limit();
 int frameCount = (limit - position) / (2 * channelCount);
 int outputSize = frameCount * outputChannels.length * 2;
 if (buffer.capacity() < outputSize) {
  buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
 } else {
  buffer.clear();
 }
 while (position < limit) {
  for (int channelIndex : outputChannels) {
   buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex));
  }
  position += channelCount * 2;
 }
 inputBuffer.position(limit);
 buffer.flip();
 outputBuffer = buffer;
}

代码示例来源:origin: google/ExoPlayer

@Override
public void queueInput(ByteBuffer buffer) {
 int remaining = buffer.remaining();
 if (remaining == 0) {
  return;
 }
 audioBufferSink.handleBuffer(buffer.asReadOnlyBuffer());
 if (this.buffer.capacity() < remaining) {
  this.buffer = ByteBuffer.allocateDirect(remaining).order(ByteOrder.nativeOrder());
 } else {
  this.buffer.clear();
 }
 this.buffer.put(buffer);
 this.buffer.flip();
 outputBuffer = this.buffer;
}

代码示例来源:origin: neo4j/neo4j

@Override
public void write( int b ) throws IOException
{
  buffer.clear();
  buffer.put( (byte) b );
  buffer.flip();
  channel.write( buffer );
}

代码示例来源:origin: jersey/jersey

void append(ByteBuffer b) {
    if (buffer == null) {
      buffer = ByteBuffer.allocate(b.remaining());
      buffer.flip();
    }
    int newSize = buffer.limit() + b.remaining();
    buffer = Utils.appendBuffers(buffer, b, newSize, 50);
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

private void flushBytes() throws IOException {
  int position;
  if ((position = mBytes.position()) > 0) {
    mBytes.flip();
    mOutputStream.write(mBytes.array(), 0, position);
    mBytes.clear();
  }
}

代码示例来源:origin: redisson/redisson

private void expand(int expandSize) {
  snapshot();
  ByteBuffer newBuff = (buffer.isDirect()
      ? ByteBuffer.allocateDirect(buffer.capacity() + expandSize)
      : ByteBuffer.allocate(buffer.capacity() + expandSize));
  buffer.flip();
  newBuff.put(buffer);
  buffer = newBuff;
  reset();
}

代码示例来源:origin: libgdx/libgdx

void setup (byte[] pcm, int channels, int sampleRate) {
  int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2));
  int samples = bytes / (2 * channels);
  duration = samples / (float)sampleRate;
  ByteBuffer buffer = ByteBuffer.allocateDirect(bytes);
  buffer.order(ByteOrder.nativeOrder());
  buffer.put(pcm, 0, bytes);
  buffer.flip();
  if (bufferID == -1) {
    bufferID = alGenBuffers();
    alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate);
  }
}

代码示例来源:origin: google/guava

/**
 * This is invoked for the last bytes of the input, which are not enough to fill a whole chunk.
 * The passed {@code ByteBuffer} is guaranteed to be non-empty.
 *
 * <p>This implementation simply pads with zeros and delegates to {@link #process(ByteBuffer)}.
 */
protected void processRemaining(ByteBuffer bb) {
 bb.position(bb.limit()); // move at the end
 bb.limit(chunkSize + 7); // get ready to pad with longs
 while (bb.position() < chunkSize) {
  bb.putLong(0);
 }
 bb.limit(chunkSize);
 bb.flip();
 process(bb);
}

代码示例来源:origin: apache/kafka

@Test
public void testParseUnknownVersion() {
  ByteBuffer buffer = ByteBuffer.allocate(32);
  buffer.putShort((short) 5);
  buffer.putShort(ControlRecordType.ABORT.type);
  buffer.putInt(23432); // some field added in version 5
  buffer.flip();
  ControlRecordType type = ControlRecordType.parse(buffer);
  assertEquals(ControlRecordType.ABORT, type);
}

代码示例来源:origin: apache/kylin

@Override
  public void run() throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(1024 * 1024);
    long totalBytes = 0;
    for (int i = 0; i < testTimes; i++) {
      buf.clear();
      newCounter.writeRegisters(buf);
      totalBytes += buf.position();
      buf.flip();
      newCounter.readRegisters(buf);
    }
    System.out.println("new serialize bytes : " + totalBytes / testTimes + "B");
  }
});

代码示例来源:origin: apache/kafka

private void doTestWriteToByteBuffer(ByteBuffer source, ByteBuffer dest) throws IOException {
  int numBytes = source.remaining();
  int position = source.position();
  DataOutputStream out = new DataOutputStream(new ByteBufferOutputStream(dest));
  Utils.writeTo(out, source, source.remaining());
  dest.flip();
  assertEquals(numBytes, dest.remaining());
  assertEquals(position, source.position());
  assertEquals(source, dest);
}

代码示例来源:origin: apache/kafka

@Test
public void testDirectBuffer() throws IOException {
  byte[] compressed = compressedBytes();
  ByteBuffer buffer;
  buffer = ByteBuffer.allocateDirect(compressed.length);
  buffer.put(compressed).flip();
  testDecompression(buffer);
  int offset = 42;
  buffer = ByteBuffer.allocateDirect(compressed.length + offset + 123);
  buffer.position(offset);
  buffer.put(compressed).flip();
  buffer.position(offset);
  testDecompression(buffer);
}

代码示例来源:origin: google/ExoPlayer

/**
 * Copies remaining bytes from {@code data} to populate a new output buffer from the processor.
 */
private void output(ByteBuffer data) {
 prepareForOutput(data.remaining());
 buffer.put(data);
 buffer.flip();
 outputBuffer = buffer;
}

代码示例来源:origin: apache/kafka

private void testWriteByteBuffer(ByteBuffer input) throws IOException {
  long value = 234239230L;
  input.putLong(value);
  input.flip();
  ByteBufferOutputStream output = new ByteBufferOutputStream(ByteBuffer.allocate(32));
  output.write(input);
  assertEquals(8, input.position());
  assertEquals(8, output.position());
  assertEquals(value, output.buffer().getLong(0));
  output.close();
}

相关文章

微信公众号

最新文章

更多