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

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

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

ByteBuffer.clear介绍

暂无

代码示例

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

@Override
public ChannelBuffer copy(int index, int length) {
  ByteBuffer src;
  try {
    src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  ByteBuffer dst = buffer.isDirect()
      ? ByteBuffer.allocateDirect(length)
      : ByteBuffer.allocate(length);
  dst.put(src);
  dst.clear();
  return new ByteBufferBackedChannelBuffer(dst);
}

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

@Override
public CharBuffer slice () {
  byteBuffer.limit(limit << 1);
  byteBuffer.position(position << 1);
  CharBuffer result = new CharToByteBufferAdapter(byteBuffer.slice());
  byteBuffer.clear();
  return result;
}

代码示例来源:origin: hankcs/HanLP

private static byte[] readBytesFromFileInputStream(FileInputStream fis) throws IOException
{
  FileChannel channel = fis.getChannel();
  int fileSize = (int) channel.size();
  ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
  channel.read(byteBuffer);
  byteBuffer.flip();
  byte[] bytes = byteBuffer.array();
  byteBuffer.clear();
  channel.close();
  fis.close();
  return bytes;
}

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

private void flushBuffer(SocketChannel sc, ByteBuffer out) throws IOException {
 if (out.position() == 0)
  return;
 out.flip();
 while (out.remaining() > 0) {
  sc.write(out);
 }
 out.clear();
}

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

private void cloneToApplicationBuffer(final ByteBuffer buffer) {
  if (applicationBuffer.capacity() < buffer.remaining()) {
    applicationBuffer = ByteBuffer.allocate(buffer.remaining());
  } else {
    applicationBuffer.clear();
  }
  applicationBuffer.put(buffer);
  applicationBuffer.flip();
}

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

private void getBytes(int index, ByteBuffer dst, boolean internal) {
  checkIndex(index, dst.remaining());
  ByteBuffer tmpBuf;
  if (internal) {
    tmpBuf = internalNioBuffer();
  } else {
    tmpBuf = buffer.duplicate();
  }
  tmpBuf.clear().position(index).limit(index + dst.remaining());
  dst.put(tmpBuf);
}

代码示例来源:origin: igniterealtime/Openfire

@Override
  public synchronized void write(byte[] bytes, int off, int len) throws IOException {
    outAppData = resizeApplicationBuffer(bytes.length);
    outAppData.put(bytes, off, len);
    outAppData.flip();
    doWrite(outAppData);
    outAppData.clear();
  }
};

代码示例来源:origin: qunarcorp/qmq

@Override
  public AppendMessageResult<MessageSequence> doAppend(long baseOffset, ByteBuffer targetBuffer, int freeSpace, PullLogMessage message) {
    workingBuffer.clear();
    final long wroteOffset = baseOffset + targetBuffer.position();
    workingBuffer.flip();
    workingBuffer.limit(PULL_LOG_UNIT_BYTES);
    workingBuffer.putLong(message.getMessageSequence());
    targetBuffer.put(workingBuffer.array(), 0, PULL_LOG_UNIT_BYTES);
    final long messageIndex = wroteOffset / PULL_LOG_UNIT_BYTES;
    return new AppendMessageResult<>(AppendMessageStatus.SUCCESS, wroteOffset, PULL_LOG_UNIT_BYTES, new MessageSequence(messageIndex, wroteOffset));
  }
}

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

public synchronized void writeBuffers(List<ByteBuffer> buffers)
 throws IOException {
 buffer.clear();
 for (ByteBuffer b : buffers) {
  buffer.putInt(b.remaining());
  buffer.put(b);                              // copy data.  sigh.
 }
 buffer.putInt(0);
 buffer.flip();
 channel.send(buffer, remote);
 LOG.info("sent to "+remote);
}

代码示例来源: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: 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: apache/avro

private void read(ByteBuffer buffer) throws IOException {
 buffer.clear();
 while (buffer.hasRemaining())
  if (channel.read(buffer) == -1)
   throw new EOFException();
 buffer.flip();
}

代码示例来源: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: apache/kylin

@Override
protected void doCleanup(Context context) throws IOException, InterruptedException {
  Iterator<Integer> it = hllcMap.keySet().iterator();
  ByteBuffer buf = ByteBuffer.allocate(BufferedMeasureCodec.DEFAULT_BUFFER_SIZE);
  while (it.hasNext()) {
    int key = it.next();
    HLLCounter hllc = hllcMap.get(key);
    buf.clear();
    hllc.writeRegisters(buf);
    buf.flip();
    context.write(new IntWritable(key), new BytesWritable(buf.array(), buf.limit()));
  }
}

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

private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException
{
  channel.position( channel.size() );
  writeBuffer.clear();
  while ( !freeIds.isEmpty() )
  {
    long id = freeIds.dequeue();
    if ( id == NO_RESULT )
    {
      continue;
    }
    writeBuffer.putLong( id );
    if ( writeBuffer.position() == writeBuffer.capacity() )
    {
      writeBuffer.flip();
      channel.writeAll( writeBuffer );
      writeBuffer.clear();
    }
  }
  writeBuffer.flip();
  if ( writeBuffer.hasRemaining() )
  {
    channel.writeAll( writeBuffer );
  }
  return channel.position();
}

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

public AbstractLegacyRecordBatch nextBatch() throws IOException {
    offsetAndSizeBuffer.clear();
    Utils.readFully(stream, offsetAndSizeBuffer);
    if (offsetAndSizeBuffer.hasRemaining())
      return null;
    long offset = offsetAndSizeBuffer.getLong(Records.OFFSET_OFFSET);
    int size = offsetAndSizeBuffer.getInt(Records.SIZE_OFFSET);
    if (size < LegacyRecord.RECORD_OVERHEAD_V0)
      throw new CorruptRecordException(String.format("Record size is less than the minimum record overhead (%d)", LegacyRecord.RECORD_OVERHEAD_V0));
    if (size > maxMessageSize)
      throw new CorruptRecordException(String.format("Record size exceeds the largest allowable message size (%d).", maxMessageSize));
    ByteBuffer batchBuffer = ByteBuffer.allocate(size);
    Utils.readFully(stream, batchBuffer);
    if (batchBuffer.hasRemaining())
      return null;
    batchBuffer.flip();
    return new BasicLegacyRecordBatch(offset, new LegacyRecord(batchBuffer));
  }
}

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

public static boolean readAndFlip( ReadableByteChannel channel, ByteBuffer buffer, int bytes )
    throws IOException
{
  buffer.clear();
  buffer.limit( bytes );
  while ( buffer.hasRemaining() )
  {
    int read = channel.read( buffer );
    if ( read == -1 )
    {
      return false;
    }
  }
  buffer.flip();
  return true;
}

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

@Override
public BufferedChannelOutput flush() throws IOException
{
  buffer.flip();
  do
  {
    channel.write( buffer );
  }
  while ( buffer.remaining() > 0 );
  buffer.clear();
  return this;
}

代码示例来源:origin: alibaba/canal

private void writeWithHeader(WritableByteChannel channel, byte[] body) throws IOException {
  synchronized (writeDataLock) {
    writeHeader.clear();
    writeHeader.putInt(body.length);
    writeHeader.flip();
    channel.write(writeHeader);
    channel.write(ByteBuffer.wrap(body));
  }
}

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

public static ByteBuffer writeLogHeader( ByteBuffer buffer, long logVersion, long previousCommittedTxId )
{
  buffer.clear();
  buffer.putLong( encodeLogVersion( logVersion ) );
  buffer.putLong( previousCommittedTxId );
  buffer.flip();
  return buffer;
}

相关文章