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

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

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

ByteBuffer.limit介绍

暂无

代码示例

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

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

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

@Override
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
  ByteBuffer data = buffer.duplicate();
  try {
    data.limit(index + length).position(index);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  data.get(dst, dstIndex, length);
}

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

/**
 * transfers appReadBuffer contents (decrypted data) into dst bytebuffer
 * @param dst ByteBuffer
 */
private int readFromAppBuffer(ByteBuffer dst) {
  appReadBuffer.flip();
  int remaining = Math.min(appReadBuffer.remaining(), dst.remaining());
  if (remaining > 0) {
    int limit = appReadBuffer.limit();
    appReadBuffer.limit(appReadBuffer.position() + remaining);
    dst.put(appReadBuffer);
    appReadBuffer.limit(limit);
  }
  appReadBuffer.compact();
  return remaining;
}

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

@Override
public Hasher putBytes(ByteBuffer b) {
 if (b.hasArray()) {
  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   putByte(b.get());
  }
 }
 return this;
}

代码示例来源:origin: Tencent/tinker

private void ensureBufferSize(int bytes) {
  if (this.data.position() + bytes > this.data.limit()) {
    if (this.isResizeAllowed) {
      byte[] array = this.data.array();
      byte[] newArray = new byte[array.length + bytes + (array.length >> 1)];
      System.arraycopy(array, 0, newArray, 0, this.data.position());
      int lastPos = this.data.position();
      this.data = ByteBuffer.wrap(newArray);
      this.data.order(ByteOrder.LITTLE_ENDIAN);
      this.data.position(lastPos);
      this.data.limit(this.data.capacity());
    }
  }
}

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

/**
 * Get the last {@code byte} from this {@literal Buffer}.
 *
 * @return The last {@code byte}.
 */
public byte last() {
  int pos = buffer.position();
  int limit = buffer.limit();
  buffer.position(limit - 1); // go to right before last position
  byte b = buffer.get(); // get the last byte
  buffer.position(pos); // go back to original pos
  return b;
}

代码示例来源:origin: Tencent/tinker

public DexDataBuffer() {
  this.data = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
  this.data.order(ByteOrder.LITTLE_ENDIAN);
  this.dataBound = this.data.position();
  this.data.limit(this.data.capacity());
  this.isResizeAllowed = true;
}

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

@Override
public GenericRecord parse(ByteBuffer bytes)
{
 try {
  bytes.get(); // ignore first \0 byte
  int id = bytes.getInt(); // extract schema registry id
  int length = bytes.limit() - 1 - 4;
  int offset = bytes.position() + bytes.arrayOffset();
  Schema schema = registry.getByID(id);
  DatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
  return reader.read(null, DecoderFactory.get().binaryDecoder(bytes.array(), offset, length, null));
 }
 catch (Exception e) {
  throw new ParseException(e, "Fail to decode avro message!");
 }
}

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

@SuppressWarnings("ByteBufferBackingArray")
@NonNull
public static byte[] toBytes(@NonNull ByteBuffer byteBuffer) {
 final byte[] result;
 SafeArray safeArray = getSafeArray(byteBuffer);
 if (safeArray != null && safeArray.offset == 0 && safeArray.limit == safeArray.data.length) {
  result = byteBuffer.array();
 } else {
  ByteBuffer toCopy = byteBuffer.asReadOnlyBuffer();
  result = new byte[toCopy.limit()];
  toCopy.position(0);
  toCopy.get(result);
 }
 return result;
}

代码示例来源:origin: bumptech/glide

@Nullable
private static SafeArray getSafeArray(@NonNull ByteBuffer byteBuffer) {
 if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
  return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
 }
 return null;
}

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

private static <T> GenericIndexed<T> createGenericIndexedVersionOne(ByteBuffer byteBuffer, ObjectStrategy<T> strategy)
{
 boolean allowReverseLookup = byteBuffer.get() == REVERSE_LOOKUP_ALLOWED;
 int size = byteBuffer.getInt();
 ByteBuffer bufferToUse = byteBuffer.asReadOnlyBuffer();
 bufferToUse.limit(bufferToUse.position() + size);
 byteBuffer.position(bufferToUse.limit());
 return new GenericIndexed<>(
   bufferToUse,
   strategy,
   allowReverseLookup
 );
}

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

public void writeString(String s, String tag) throws IOException {
  if (s == null) {
    writeInt(-1, "len");
    return;
  }
  ByteBuffer bb = stringToByteBuffer(s);
  writeInt(bb.remaining(), "len");
  out.write(bb.array(), bb.position(), bb.limit());
}

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

@Override
public final HashCode hash() {
 munch();
 buffer.flip();
 if (buffer.remaining() > 0) {
  processRemaining(buffer);
  buffer.position(buffer.limit());
 }
 return makeHash();
}

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

@Override
public Object read(ByteBuffer buffer) {
  int size = buffer.getInt();
  if (size < 0)
    return null;
  if (size > buffer.remaining())
    throw new SchemaException("Error reading bytes of size " + size + ", only " + buffer.remaining() + " bytes available");
  ByteBuffer val = buffer.slice();
  val.limit(size);
  buffer.position(buffer.position() + size);
  return val;
}

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

private @Nullable float[] parseMetadata(ByteBuffer data) {
 if (data.remaining() != 16) {
  return null;
 }
 scratch.reset(data.array(), data.limit());
 scratch.setPosition(data.arrayOffset() + 4); // skip reserved bytes too.
 float[] result = new float[3];
 for (int i = 0; i < 3; i++) {
  result[i] = Float.intBitsToFloat(scratch.readLittleEndianInt());
 }
 return result;
}

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

private static void assertDiscardToSpsMatchesExpected(String input, String expectedOutput) {
 byte[] bitstream = Util.getBytesFromHexString(input);
 byte[] expectedOutputBitstream = Util.getBytesFromHexString(expectedOutput);
 ByteBuffer buffer = ByteBuffer.wrap(bitstream);
 buffer.position(buffer.limit());
 NalUnitUtil.discardToSps(buffer);
 assertThat(Arrays.copyOf(buffer.array(), buffer.position())).isEqualTo(expectedOutputBitstream);
}

相关文章

微信公众号

最新文章

更多