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

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

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

ByteBuffer.put介绍

[英]Writes all the remaining bytes of the src byte buffer to this buffer's current position, and increases both buffers' position by the number of bytes copied.
[中]将src字节缓冲区的所有剩余字节写入该缓冲区的当前位置,并按复制的字节数增加两个缓冲区的位置。

代码示例

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

@Override
public byte[] getCacheKey()
{
 return ByteBuffer.allocate(1 + 8)
          .put(ExtractionCacheHelper.CACHE_TYPE_ID_SUBSTRING)
          .putInt(this.index)
          .putInt(this.end)
          .array();
}

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

@Override
public void write(ByteBuffer buffer, Object o) {
  if (o == null) {
    buffer.putInt(-1);
    return;
  }
  ByteBuffer arg = (ByteBuffer) o;
  int pos = arg.position();
  buffer.putInt(arg.remaining());
  buffer.put(arg);
  arg.position(pos);
}

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

@Override
public void setBytes(int index, ByteBuffer src) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + src.remaining()).position(index);
  data.put(src);
}

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

private void expandBuffer(int remainingRequired) {
  int expandSize = Math.max((int) (buffer.limit() * REALLOCATION_FACTOR), buffer.position() + remainingRequired);
  ByteBuffer temp = ByteBuffer.allocate(expandSize);
  int limit = limit();
  buffer.flip();
  temp.put(buffer);
  buffer.limit(limit);
  // reset the old buffer's position so that the partial data in the new buffer cannot be mistakenly consumed
  // we should ideally only do this for the original buffer, but the additional complexity doesn't seem worth it
  buffer.position(initialPosition);
  buffer = temp;
}

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

static ByteBuffer encodeChunk(ByteBuffer data) {
  if (data.remaining() == 0) {
    return ByteBuffer.wrap(LAST_CHUNK);
  }
  byte[] startBytes = getChunkHeaderBytes(data.remaining());
  ByteBuffer chunkBuffer = ByteBuffer.allocate(startBytes.length + data.remaining() + 2);
  chunkBuffer.put(startBytes);
  chunkBuffer.put(data);
  chunkBuffer.put(LINE_SEPARATOR_BYTES);
  chunkBuffer.flip();
  return chunkBuffer;
}

代码示例来源: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: spring-projects/spring-framework

private void write(ByteBuffer source) {
  int length = source.remaining();
  ByteBuffer tmp = this.byteBuffer.duplicate();
  int limit = this.writePosition + source.remaining();
  ((Buffer) tmp).clear().position(this.writePosition).limit(limit);
  tmp.put(source);
  this.writePosition += length;
}

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

static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer dst) {
  buf.checkIndex(index, dst.remaining());
  if (dst.remaining() == 0) {
    return;
  }
  if (dst.isDirect()) {
    if (dst.isReadOnly()) {
      // We need to check if dst is ready-only so we not write something in it by using Unsafe.
      throw new ReadOnlyBufferException();
    }
    // Copy to direct memory
    long dstAddress = PlatformDependent.directBufferAddress(dst);
    PlatformDependent.copyMemory(addr, dstAddress + dst.position(), dst.remaining());
    dst.position(dst.position() + dst.remaining());
  } else if (dst.hasArray()) {
    // Copy to array
    PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dst.position(), dst.remaining());
    dst.position(dst.position() + dst.remaining());
  } else  {
    dst.put(buf.nioBuffer());
  }
}

代码示例来源:origin: weibocom/motan

private ByteBuffer grow(int size) {
  ByteBuffer newbuf = ByteBuffer.allocate(size);
  newbuf.put(buf.array());
  newbuf.position(buf.position());
  return newbuf;
}

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

private void convertToMutableByteBuffer()
{
 ByteBuffer tmpBuffer = ByteBuffer.allocate(storageBuffer.remaining());
 tmpBuffer.put(storageBuffer.asReadOnlyBuffer());
 tmpBuffer.position(0);
 storageBuffer = tmpBuffer;
 initPosition = 0;
}

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

private Hasher putBytesInternal(ByteBuffer readBuffer) {
 // If we have room for all of it, this is easy
 if (readBuffer.remaining() <= buffer.remaining()) {
  buffer.put(readBuffer);
  munchIfFull();
  return this;
 }
 // First add just enough to fill buffer size, and munch that
 int bytesToCopy = bufferSize - buffer.position();
 for (int i = 0; i < bytesToCopy; i++) {
  buffer.put(readBuffer.get());
 }
 munch(); // buffer becomes empty here, since chunkSize divides bufferSize
 // Now process directly from the rest of the input buffer
 while (readBuffer.remaining() >= chunkSize) {
  process(readBuffer);
 }
 // Finally stick the remainder back in our usual buffer
 buffer.put(readBuffer);
 return this;
}

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

@Override
public void writeByte(int b) {
  ensureBufferSize(SizeOf.UBYTE);
  data.put((byte) b);
  if (this.data.position() > this.dataBound) {
    this.dataBound = this.data.position();
  }
}

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

public void testFromByteArray_withTooLongArrayInputThrowsIllegalArgumentException() {
 byte[] buffer = MANY_VALUES_PAIRED_STATS.toByteArray();
 byte[] tooLongByteArray =
   ByteBuffer.allocate(buffer.length + 2)
     .order(ByteOrder.LITTLE_ENDIAN)
     .put(buffer)
     .putChar('.')
     .array();
 try {
  PairedStats.fromByteArray(tooLongByteArray);
  fail("Expected IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
}

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

private int copyToByteBufferByteWise( int sourceOffset, ByteBuffer buf )
{
  int bytesToCopy = Math.min( buf.limit() - buf.position(), pageSize - sourceOffset );
  for ( int i = 0; i < bytesToCopy; i++ )
  {
    byte b = getByte( sourceOffset + i );
    buf.put( b );
  }
  return bytesToCopy;
}

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

protected static void generateRecordForId( long id, ByteBuffer buf )
{
  buf.position( 0 );
  int x = (int) (id + 1);
  buf.putInt( x );
  while ( buf.position() < buf.limit() )
  {
    x++;
    buf.put( (byte) (x & 0xFF) );
  }
  buf.position( 0 );
}

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

public void testFromByteArrayWithTooShortArrayInputThrowsIllegalArgumentException() {
 byte[] buffer = MANY_VALUES_STATS_VARARGS.toByteArray();
 byte[] tooShortByteArray =
   ByteBuffer.allocate(buffer.length - 1)
     .order(ByteOrder.LITTLE_ENDIAN)
     .put(buffer, 0, Stats.BYTES - 1)
     .array();
 try {
  Stats.fromByteArray(tooShortByteArray);
  fail("Expected IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
}

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

public static ImmutableRTree newImmutableFromMutable(RTree rTree)
{
 if (rTree.getSize() == 0) {
  return empty();
 }
 ByteBuffer buffer = ByteBuffer.allocate(calcNumBytes(rTree));
 buffer.put(VERSION);
 buffer.putInt(rTree.getNumDims());
 rTree.getRoot().storeInByteBuffer(buffer, buffer.position());
 buffer.position(0);
 return new ImmutableRTree(buffer, rTree.getBitmapFactory());
}

代码示例来源: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);
  }
}

相关文章

微信公众号

最新文章

更多