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

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

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

ByteBuffer.getShort介绍

[英]Returns the short at the current position and increases the position by 2.

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

代码示例

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

protected static long get6BLongFromByteBuffer( ByteBuffer buffer, int startOffset )
{
  long low4b = buffer.getInt( startOffset ) & 0xFFFFFFFFL;
  long high2b = buffer.getShort( startOffset + Integer.BYTES ) & 0xFFFF;
  long result = low4b | (high2b << 32);
  return result == 0xFFFFFFFFFFFFL ? -1 : result;
}

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

protected static int get3ByteIntFromByteBuffer( ByteBuffer buffer, int address )
{
  int lowWord = buffer.getShort( address ) & 0xFFFF;
  int highByte = buffer.get( address + Short.BYTES ) & 0xFF;
  int result = lowWord | (highByte << Short.SIZE);
  return result == 0xFFFFFF ? -1 : result;
}

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

private static short shortBE(ByteBuffer buffer, int offset) {
  return buffer.order() == ByteOrder.BIG_ENDIAN ?
      buffer.getShort(offset) : ByteBufUtil.swapShort(buffer.getShort(offset));
}

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

/**
 * build unit from buffer from current position.
 */
private boolean read(final ByteBuffer buffer) {
  if (buffer.position() + 2 > buffer.limit()) {
    return false;
  }
  this.size = buffer.getShort();
  if (this.size < 1) {
    return false;
  }
  this.tagsCode = buffer.getLong();
  this.msgStoreTime = buffer.getLong();
  this.bitMapSize = buffer.getShort();
  if (this.bitMapSize < 1) {
    return true;
  }
  if (this.filterBitMap == null || this.filterBitMap.length != this.bitMapSize) {
    this.filterBitMap = new byte[bitMapSize];
  }
  buffer.get(this.filterBitMap);
  return true;
}

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

public List<UUID> extractUUIDs(byte[] scanResult) {
  List<UUID> uuids = new ArrayList<>();
  ByteBuffer buffer = ByteBuffer.wrap(scanResult).order(ByteOrder.LITTLE_ENDIAN);
    byte length = buffer.get();
    if (length == 0) break;
    byte type = buffer.get();
    switch (type) {
      case 0x02: // Partial list of 16-bit UUIDs
      case 0x03: // Complete list of 16-bit UUIDs
        while (length >= 2) {
          final String serviceUuidString = String.format(UUID_BASE, buffer.getShort());
          final UUID serviceUuid = UUID.fromString(serviceUuidString);
          uuids.add(serviceUuid);
      case 0x05: // Complete list of 32-bit UUIDs
        while (length >= 4) {
          final String serviceUuidString = String.format(UUID_BASE, buffer.getInt());
          final UUID serviceUuid = UUID.fromString(serviceUuidString);
          uuids.add(serviceUuid);
        buffer.position(buffer.position() + length - 1);
        break;

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

private void fillKeys(byte[] rowKey, String[] result) {
  int keyNum = keyIndexes.length;
  ByteBuffer byteBuffer = ByteBuffer.wrap(rowKey);
  byteBuffer.getShort(); // read shard
  for (int i = 0; i < keyNum; i++) {
    short keyLen = byteBuffer.getShort();
    byte[] keyBytes = new byte[keyLen];
    byteBuffer.get(keyBytes);
    result[keyIndexes[i]] = Bytes.toString(keyBytes);
  }
}

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

/**
 * Copies the first key/value from the given stream, and initializes
 * decompression state based on it. Assumes that we have already read key
 * and value lengths. Does not set {@link #qualifierLength} (not used by
 * decompression) or {@link #prevOffset} (set by the calle afterwards).
 */
private void decompressFirstKV(ByteBuffer out, DataInputStream in)
  throws IOException {
 int kvPos = out.position();
 out.putInt(keyLength);
 out.putInt(valueLength);
 prevTimestampOffset = out.position() + keyLength -
   KeyValue.TIMESTAMP_TYPE_SIZE;
 ByteBufferUtils.copyFromStreamToBuffer(out, in, keyLength + valueLength);
 rowLength = out.getShort(kvPos + KeyValue.ROW_OFFSET);
 familyLength = out.get(kvPos + KeyValue.ROW_OFFSET +
   KeyValue.ROW_LENGTH_SIZE + rowLength);
 type = out.get(prevTimestampOffset + KeyValue.TIMESTAMP_SIZE);
}

代码示例来源:origin: mcxiaoke/packer-ng-plugin

public void testByteBuffer() throws IOException {
    byte[] string = "Hello".getBytes();
    ByteBuffer buf = ByteBuffer.allocate(1024);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(123);
    buf.putChar('z');
    buf.putShort((short) 2017);
    buf.putFloat(3.1415f);
    buf.put(string);
    buf.putLong(9876543210L);
    buf.putDouble(3.14159265);
    buf.put((byte) 5);
    buf.flip(); // important
//        TestUtils.showBuffer(buf);
    assertEquals(123, buf.getInt());
    assertEquals('z', buf.getChar());
    assertEquals(2017, buf.getShort());
    assertEquals(3.1415f, buf.getFloat());
    byte[] so = new byte[string.length];
    buf.get(so);
    assertTrue(TestUtils.sameBytes(string, so));
    assertEquals(9876543210L, buf.getLong());
    assertEquals(3.14159265, buf.getDouble());
    assertEquals((byte) 5, buf.get());
  }

代码示例来源:origin: pxb1988/dex2jar

private String[] getTypeList(int offset) {
  if (offset == 0) {
    return new String[0];
  }
  typeListIn.position(offset);
  int size = typeListIn.getInt();
  String[] types = new String[size];
  for (int i = 0; i < size; i++) {
    types[i] = getType(0xFFFF & typeListIn.getShort());
  }
  return types;
}

代码示例来源:origin: stackoverflow.com

byte[] arr = { 0x00, 0x01 };
ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default
short num = wrapped.getShort(); // 1

ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort(num);
byte[] bytes = dbuf.array(); // { 0, 1 }

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

/**
 * Only read first 2 byte to get unit size.
 * <p>
 * if size > 0, then skip buffer position with size.
 * </p>
 * <p>
 * if size <= 0, nothing to do.
 * </p>
 */
private void readBySkip(final ByteBuffer buffer) {
  ByteBuffer temp = buffer.slice();
  short tempSize = temp.getShort();
  this.size = tempSize;
  if (tempSize > 0) {
    buffer.position(buffer.position() + this.size);
  }
}

代码示例来源:origin: stackoverflow.com

ByteBuffer buffer = ByteBuffer.wrap(myArray);
buffer.order(ByteOrder.LITTLE_ENDIAN);  // if you want little-endian
int result = buffer.getShort();

代码示例来源:origin: ltsopensource/light-task-scheduler

@Override
public void read(FileChannel fileChannel) throws IOException {
  if (fileChannel.size() == 0) {
    return;
  }
  fileChannel.position(0);
  fileChannel.read(byteBuffer());
  byteBuffer().position(0);
  short readMagic = byteBuffer().getShort();
  if (readMagic != magic) {
    throw new IOException("Invalid file type magic number 0x" + Integer.toHexString(readMagic & 0xFFFF));
  }
  this.firstRecordId = byteBuffer().getLong();
}

代码示例来源:origin: alibaba/fescar

@Override
public void decode(ByteBuffer byteBuffer) {
  this.transactionId = byteBuffer.getLong();
  short len = byteBuffer.getShort();
  if (len > 0) {
    byte[] bs = new byte[len];
    byteBuffer.get(bs);
    this.setExtraData(new String(bs, UTF8));
  }
}

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

@Override
public void queueInput(ByteBuffer inputBuffer) {
 Assertions.checkState(outputChannels != null);
 int position = inputBuffer.position();
 int limit = inputBuffer.limit();
 int frameCount = (limit - position) / (2 * channelCount);
 int outputSize = frameCount * outputChannels.length * 2;
 if (buffer.capacity() < outputSize) {
  buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder());
 } else {
  buffer.clear();
 }
 while (position < limit) {
  for (int channelIndex : outputChannels) {
   buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex));
  }
  position += channelCount * 2;
 }
 inputBuffer.position(limit);
 buffer.flip();
 outputBuffer = buffer;
}

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

byte[] buffer = bbuf.array();
f.readFully(buffer);
bbuf.order(ByteOrder.LITTLE_ENDIAN);
for (eocdIdx = buffer.length - kEOCDLen; eocdIdx >= 0; eocdIdx--) {
  if (buffer[eocdIdx] == 0x50
      && bbuf.getInt(eocdIdx) == kEOCDSignature) {
    if (LOGV) {
      Log.v(LOG_TAG, "+++ Found EOCD at index: " + eocdIdx);
int numEntries = bbuf.getShort(eocdIdx + kEOCDNumEntries);
long dirSize = bbuf.getInt(eocdIdx + kEOCDSize) & 0xffffffffL;
long dirOffset = bbuf.getInt(eocdIdx + kEOCDFileOffset) & 0xffffffffL;
buf.order(ByteOrder.LITTLE_ENDIAN);

代码示例来源:origin: alibaba/fescar

@Override
public void decode(byte[] a) {
  ByteBuffer byteBuffer = ByteBuffer.wrap(a);
  this.transactionId = byteBuffer.getLong();
  this.timeout = byteBuffer.getInt();
  short applicationIdLen = byteBuffer.getShort();
  if (applicationIdLen > 0) {
    byte[] byApplicationId = new byte[applicationIdLen];
    byteBuffer.get(byApplicationId);
    this.applicationId = new String(byApplicationId);
  }
  short serviceGroupLen = byteBuffer.getShort();
  if (serviceGroupLen > 0) {
    byte[] byServiceGroup = new byte[serviceGroupLen];
    byteBuffer.get(byServiceGroup);
    this.transactionServiceGroup = new String(byServiceGroup);
  }
  short txNameLen = byteBuffer.getShort();
  if (txNameLen > 0) {
    byte[] byTxName = new byte[txNameLen];
    byteBuffer.get(byTxName);
    this.transactionName = new String(byTxName);
  }
  this.beginTime = byteBuffer.getLong();
}

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

public List<UUID> extractUUIDs(byte[] scanResult) {
  List<UUID> uuids = new ArrayList<>();
  ByteBuffer buffer = ByteBuffer.wrap(scanResult).order(ByteOrder.LITTLE_ENDIAN);
    int length = buffer.get() & 0xFF; // convert to unsigned
    if (length == 0) break;
    byte type = buffer.get();
    switch (type) {
      case DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL: // Partial list of 16-bit UUIDs
      case DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE: // Complete list of 16-bit UUIDs
        while (length >= 2) {
          uuids.add(UUID.fromString(String.format(UUID_BASE_FORMAT, buffer.getShort())));
          length -= 2;
      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;
        buffer.position(buffer.position() + safeLengthToProceed);
        break;

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

protected void decodeFromLenPfxBytes(byte[] rowKey, int[] valueIdx, String[] result) {
  ByteBuffer byteBuffer = ByteBuffer.wrap(rowKey);
  for (int i = 0; i < valueIdx.length; i++) {
    short keyLen = byteBuffer.getShort();
    byte[] keyBytes = new byte[keyLen];
    byteBuffer.get(keyBytes);
    result[valueIdx[i]] = fromBytes(keyBytes);
  }
}

相关文章