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

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

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

ByteBuffer.position介绍

暂无

代码示例

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

/**
 * Returns a byte array containing the bytes from {@code start} to this
 * section's current position.
 */
private byte[] getBytesFrom(int start) {
  int end = data.position();
  byte[] result = new byte[end - start];
  data.position(start);
  data.get(result);
  return result;
}

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

/**
 * Initialize an instance based upon the underlying storage from {@code value}.
 * There is a potential to share the underlying array storage if {@link ByteBuffer#hasArray()} is {@code true}.
 * if {@code copy} is {@code true} a copy will be made of the memory.
 * if {@code copy} is {@code false} the underlying storage will be shared, if possible.
 */
public AsciiString(ByteBuffer value, boolean copy) {
  this(value, value.position(), value.remaining(), copy);
}

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

public MutableRecordBatch nextBatch() {
  int remaining = buffer.remaining();
  Integer batchSize = nextBatchSize();
  if (batchSize == null || remaining < batchSize)
    return null;
  byte magic = buffer.get(buffer.position() + MAGIC_OFFSET);
  ByteBuffer batchSlice = buffer.slice();
  batchSlice.limit(batchSize);
  buffer.position(buffer.position() + batchSize);
  if (magic > RecordBatch.MAGIC_VALUE_V1)
    return new DefaultRecordBatch(batchSlice);
  else
    return new AbstractLegacyRecordBatch.ByteBufferLegacyRecordBatch(batchSlice);
}

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

/**
 * Writes 0x00 until the position is aligned to a multiple of 4.
 */
public void alignToFourBytesWithZeroFill() {
  int alignedPos = SizeOf.roundToTimesOfFour(data.position());
  ensureBufferSize((alignedPos - data.position()) * SizeOf.UBYTE);
  while ((data.position() & 3) != 0) {
    data.put((byte) 0);
  }
  if (this.data.position() > this.dataBound) {
    this.dataBound = this.data.position();
  }
}

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

@Override
public int read(ByteBuffer dst) throws IOException {
  snapshot();
  if (dst.remaining() < this.limit) {
    buffer.limit(dst.remaining());
  }
  int pos = dst.position();
  dst.put(buffer);
  buffer.limit(this.limit);
  return dst.position() - pos;
}

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

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

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

@Override
public void write(long value) throws IOException
{
 for (int i = numBytes - 1; i >= 0; i--) {
  buffer.put((byte) (value >>> (i * 8)));
  if (output != null) {
   output.write(buffer.array());
   buffer.position(0);
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Try to read an EOL incrementing the buffer position if successful.
 * @return whether an EOL was consumed
 */
private boolean tryConsumeEndOfLine(ByteBuffer byteBuffer) {
  if (byteBuffer.remaining() > 0) {
    byte b = byteBuffer.get();
    if (b == '\n') {
      return true;
    }
    else if (b == '\r') {
      if (byteBuffer.remaining() > 0 && byteBuffer.get() == '\n') {
        return true;
      }
      else {
        throw new StompConversionException("'\\r' must be followed by '\\n'");
      }
    }
    // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
    ((Buffer) byteBuffer).position(byteBuffer.position() - 1);
  }
  return false;
}

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

private void doTestUpdateByteBufferWithOffsetPosition(byte[] bytes, ByteBuffer buffer, int offset) {
  buffer.put(bytes);
  buffer.flip();
  buffer.position(offset);
  Checksum bufferCrc = Crc32C.create();
  Checksums.update(bufferCrc, buffer, buffer.remaining());
  assertEquals(Crc32C.compute(bytes, offset, buffer.remaining()), bufferCrc.getValue());
  assertEquals(offset, buffer.position());
}

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

/**
 * Create a copy of {@code value} into this instance using the encoding type of {@code charset}.
 * The copy will start at index {@code start} and copy {@code length} bytes.
 */
public AsciiString(CharSequence value, Charset charset, int start, int length) {
  CharBuffer cbuf = CharBuffer.wrap(value, start, start + length);
  CharsetEncoder encoder = CharsetUtil.encoder(charset);
  ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * length));
  encoder.encode(cbuf, nativeBuffer, true);
  final int offset = nativeBuffer.arrayOffset();
  this.value = Arrays.copyOfRange(nativeBuffer.array(), offset, offset + nativeBuffer.position());
  this.offset = 0;
  this.length = this.value.length;
}

相关文章