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

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

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

ByteBuffer.getInt介绍

[英]Returns the int at the current position and increases the position by 4.

The 4 bytes starting at the current position are composed into a int according to the current byte order and returned.
[中]返回当前位置的整数,并将该位置增加4。
从当前位置开始的4个字节根据当前字节顺序组成一个int并返回。

代码示例

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

@Override
public int get(int index)
{
 return buffer.getInt(buffer.position() + (index * numBytes)) >>> bitsToShift;
}

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

Idmap_header(ByteBuffer buf, int offset) {
  super(buf, offset);
  magic = buf.getInt(offset);
  version = buf.getInt(offset + 4);
  target_crc32 = buf.getInt(offset + 8);
  overlay_crc32 = buf.getInt(offset + 12);
  buf.get(target_path, offset + 16, 256);
  buf.get(overlay_path, offset + 16 + 256, 256);
  target_package_id = buf.getShort(offset + 16 + 256 + 256);
  type_count = buf.getShort(offset + 16 + 256 + 256 + 2);
 }
} // __attribute__((packed));

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

private static String deserializeV1(byte[] serialized) {
  final ByteBuffer bb = ByteBuffer.wrap(serialized).order(ByteOrder.LITTLE_ENDIAN);
  final byte[] targetStringBytes = new byte[bb.getInt()];
  bb.get(targetStringBytes);
  return new String(targetStringBytes, CHARSET);
}

代码示例来源:origin: Polidea/RxAndroidBle

public List<UUID> extractUUIDs(byte[] scanResult) {
  List<UUID> uuids = new ArrayList<>();
  ByteBuffer buffer = ByteBuffer.wrap(scanResult).order(ByteOrder.LITTLE_ENDIAN);
  while (buffer.remaining() > 2) {
    int length = buffer.get() & 0xFF; // convert to unsigned
    if (length == 0) break;
      case DATA_TYPE_SERVICE_UUIDS_32_BIT_COMPLETE: // Complete list of 32-bit UUIDs
        while (length >= 4) {
          uuids.add(UUID.fromString(String.format(UUID_BASE_FORMAT, buffer.getInt())));
          length -= 4;
      case DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE: // Complete list of 128-bit UUIDs
        while (length >= 16) {
          long lsb = buffer.getLong();
          long msb = buffer.getLong();
          uuids.add(new UUID(msb, lsb));
          length -= 16;
        int safeLengthToProceed = Math.min(length - 1, buffer.remaining());
        buffer.position(buffer.position() + safeLengthToProceed);
        break;

代码示例来源:origin: Codecademy/EventHub

@Override
public MetaData fromBytes(byte[] bytes) {
 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
 int userId = byteBuffer.getInt();
 int eventTypeId = byteBuffer.getInt();
 byte[] location = new byte[LOCATION_SIZE];
 byteBuffer.get(location);
 return new MetaData(userId, eventTypeId, location);
}

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

@Override
protected void _parseDetails(ByteBuffer content) {
  content.position(6);// ignore 6 reserved bytes;
  dataReferenceIndex = IsoTypeReader.readUInt16(content);   // 8
  reserved1 = content.getInt();
  flags = IsoTypeReader.readUInt32(content);
  timeScale = content.getInt();
  frameDuration = content.getInt();
  numberOfFrames = IsoTypeReader.readUInt8(content);
  reserved2 = IsoTypeReader.readUInt24(content);
  rest = new byte[content.remaining()];
  content.get(rest);
}

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

@Override
public Object get(ByteBuffer buf, int position)
{
 ByteBuffer mutationBuffer = buf.duplicate();
 mutationBuffer.position(position);
 // | k (byte) | numLongs (int) | bitset (long[numLongs]) |
 int sizeBytes = 1 + Integer.BYTES + (buf.getInt(position + 1) * Long.BYTES);
 mutationBuffer.limit(position + sizeBytes);
 return mutationBuffer.slice();
}

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

@Override
public boolean getBoolean()
{
  checkEnoughBytes();
  switch (fieldType) {
    case BYTE:
      return value.get() != 0;
    case SHORT:
      return value.getShort() != 0;
    case INT:
      return value.getInt() != 0;
    case LONG:
      return value.getLong() != 0;
    default:
      throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("conversion '%s' to boolean not supported", fieldType));
  }
}

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

@Override
 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
 {
  int b1Length = ByteBuffer.wrap(b1, s1 + 4, l1 - 4).getInt();
  int b2Length = ByteBuffer.wrap(b2, s2 + 4, l2 - 4).getInt();
  final int retVal = compareBytes(b1, s1 + 8, b1Length, b2, s2 + 8, b2Length);
  return retVal;
 }
}

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

@Test
public void shouldPackLocalDateTimeWithTimeZoneId()
{
  LocalDateTime localDateTime = LocalDateTime.of( 1999, 12, 30, 9, 49, 20, 999999999 );
  ZoneId zoneId = ZoneId.of( "Europe/Stockholm" );
  ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, zoneId );
  PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) );
  ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() );
  buffer.getShort(); // skip struct header
  assertEquals( INT_32, buffer.get() );
  assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() );
  assertEquals( INT_32, buffer.get() );
  assertEquals( localDateTime.getNano(), buffer.getInt() );
  buffer.getShort(); // skip zoneId string header
  byte[] zoneIdBytes = new byte[zoneId.getId().getBytes( UTF_8 ).length];
  buffer.get( zoneIdBytes );
  assertEquals( zoneId.getId(), new String( zoneIdBytes, UTF_8 ) );
}

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

@Override
public Event deserialize(byte[] message) throws IOException {
  ByteBuffer buffer = ByteBuffer.wrap(message).order(ByteOrder.LITTLE_ENDIAN);
  int address = buffer.getInt(0);
  int typeOrdinal = buffer.getInt(4);
  return new Event(EventType.values()[typeOrdinal], address);
}

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

for (int i = 0; i < 20; ++i) {
 final SmooshedWriter writer = smoosher.addWithSmooshedWriter(StringUtils.format("%d", i), 7);
 writer.write(ByteBuffer.wrap(Ints.toByteArray(i)));
 try {
  writer.close();
for (int i = 0; i < 20; ++i) {
 ByteBuffer buf = mapper.mapFile(StringUtils.format("%d", i));
 Assert.assertEquals(0, buf.position());
 Assert.assertEquals(4, buf.remaining());
 Assert.assertEquals(4, buf.capacity());
 Assert.assertEquals(i, buf.getInt());

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

@Override
public int getPartition(BytesWritable bytesWritable, Writable value, int numPartitions)
{
 final ByteBuffer bytes = ByteBuffer.wrap(bytesWritable.getBytes());
 bytes.position(4); // Skip length added by SortableBytes
 int shardNum = bytes.getInt();
 if ("local".equals(JobHelper.getJobTrackerAddress(config))) {
  return shardNum % numPartitions;
 } else {
  if (shardNum >= numPartitions) {
   throw new ISE("Not enough partitions, shard[%,d] >= numPartitions[%,d]", shardNum, numPartitions);
  }
  return shardNum;
 }
}

代码示例来源:origin: TeamNewPipe/NewPipe

private int parse_mdia(ByteBuffer data) {
  while (data.hasRemaining()) {
    int end = data.position() + data.getInt();
    if (data.getInt() == ATOM_MDHD) {
      byte version = data.get();
      data.position(data.position() + 3 + ((version == 0 ? 4 : 8) * 2));
      return data.getInt();
    }
    data.position(end);
  }
  return 0;// this NEVER should happen
}

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

@Override
public SerializablePairLongString fromByteBuffer(ByteBuffer buffer, int numBytes)
{
 final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
 long lhs = readOnlyBuffer.getLong();
 int stringSize = readOnlyBuffer.getInt();
 String lastString = null;
 if (stringSize > 0) {
  byte[] stringBytes = new byte[stringSize];
  readOnlyBuffer.get(stringBytes, 0, stringSize);
  lastString = StringUtils.fromUtf8(stringBytes);
 }
 return new SerializablePairLongString(lhs, lastString);
}

相关文章

微信公众号

最新文章

更多