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

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

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

ByteBuffer.get介绍

[英]Reads bytes from the current position into the specified byte array and increases the position by the number of bytes read.

Calling this method has the same effect as get(dest, 0, dest.length).
[中]将当前位置的字节读取到指定的字节数组中,并按读取的字节数增加该位置。
调用此方法与get(dest,0,dest.length)的效果相同。

代码示例

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

protected static long get5BLongFromByteBuffer( ByteBuffer buffer, int startOffset )
{
  long low4b = buffer.getInt( startOffset ) & 0xFFFFFFFFL;
  long high1b = buffer.get( startOffset + Integer.BYTES ) & 0xFF;
  long result = low4b | (high1b << 32);
  return result == 0xFFFFFFFFFFL ? -1 : result;
}

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

@Override
protected void processRemaining(ByteBuffer buffer) {
 b += buffer.remaining();
 for (int i = 0; buffer.hasRemaining(); i += 8) {
  finalM ^= (buffer.get() & 0xFFL) << i;
 }
}

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

@Override
public void setBytes(int index, ByteBuffer src) {
  src.get(array, index, src.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

/**
 * Returns the checksum of all but the first 12 bytes of {@code dex}.
 */
public int computeChecksum() throws IOException {
  Adler32 adler32 = new Adler32();
  byte[] buffer = new byte[8192];
  ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
  data.limit(data.capacity());
  data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM);
  while (data.hasRemaining()) {
    int count = Math.min(buffer.length, data.remaining());
    data.get(buffer, 0, count);
    adler32.update(buffer, 0, count);
  }
  return (int) adler32.getValue();
}

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

@Override
public Hasher putBytes(ByteBuffer buffer) {
 ByteOrder bo = buffer.order();
 buffer.order(ByteOrder.LITTLE_ENDIAN);
 while (buffer.remaining() >= 4) {
  putInt(buffer.getInt());
 }
 while (buffer.hasRemaining()) {
  putByte(buffer.get());
 }
 buffer.order(bo);
 return this;
}

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

@Override
public int read() {
 if (!byteBuffer.hasRemaining()) {
  return -1;
 }
 return byteBuffer.get();
}

代码示例来源:origin: Meituan-Dianping/walle

public static byte[] readLengthPrefixedByteArray(ByteBuffer buf) throws IOException {
  int len = buf.getInt();
  if (len < 0) {
    throw new IOException("Negative length");
  } else if (len > buf.remaining()) {
    throw new IOException("Underflow while reading length-prefixed value. Length: " + len
        + ", available: " + buf.remaining());
  }
  byte[] result = new byte[len];
  buf.get(result);
  return result;
}

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

@Override
public Object get(ByteBuffer buf, int position)
{
 ByteBuffer dataCopyBuffer = ByteBuffer.allocate(HyperLogLogCollector.getLatestNumBytesForDenseStorage());
 ByteBuffer mutationBuffer = buf.duplicate();
 mutationBuffer.position(position);
 mutationBuffer.get(dataCopyBuffer.array());
 return HyperLogLogCollector.makeCollector(dataCopyBuffer);
}

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

ResTable_type(ByteBuffer buf, int offset) {
 super(buf, offset);
 header = new ResChunk_header(buf, offset);
 id = buf.get(offset + ResChunk_header.SIZEOF);
 flags = buf.get(offset + ResChunk_header.SIZEOF + 1);
 reserved = buf.getShort(offset + ResChunk_header.SIZEOF + 2);
 entryCount = buf.getInt(offset + ResChunk_header.SIZEOF + 4);
 entriesStart = buf.getInt(offset + ResChunk_header.SIZEOF + 8);
 buf.position(offset + ResChunk_header.SIZEOF + 12);
 config = ResTable_config.createConfig(buf);
}

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

public DeltaLongEncodingReader(ByteBuffer fromBuffer)
{
 this.buffer = fromBuffer.asReadOnlyBuffer();
 byte version = buffer.get();
 if (version == CompressionFactory.DELTA_ENCODING_VERSION) {
  base = buffer.getLong();
  bitsPerValue = buffer.getInt();
  fromBuffer.position(buffer.position());
  deserializer = VSizeLongSerde.getDeserializer(bitsPerValue, buffer, buffer.position());
 } else {
  throw new IAE("Unknown version[%s]", version);
 }
}

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

public static final Pair<Bucket, byte[]> fromGroupKey(byte[] keyBytes)
{
 ByteBuffer buf = ByteBuffer.wrap(keyBytes);
 Bucket bucket = new Bucket(buf.getInt(), DateTimes.utc(buf.getLong()), buf.getInt());
 byte[] bytesLeft = new byte[buf.remaining()];
 buf.get(bytesLeft);
 return Pair.of(bucket, bytesLeft);    
}

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

public ImmutableRTree(ByteBuffer data, BitmapFactory bitmapFactory)
{
 final int initPosition = data.position();
 Preconditions.checkArgument(data.get(initPosition) == VERSION, "Mismatching versions");
 this.numDims = data.getInt(1 + initPosition) & 0x7FFF;
 this.data = data;
 this.root = new ImmutableNode(numDims, initPosition, 1 + Integer.BYTES, data, bitmapFactory);
}

相关文章