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

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

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

ByteBuffer.getLong介绍

[英]Returns the long at the current position and increases the position by 8.

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

代码示例

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

/**
 * Gets the last time a metadata string was used.
 */
long getLastTimestamp() {
  return ByteBuffer.wrap(value, 1, 8).getLong();
}

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

private static DurationValue readDuration( ByteBuffer bb, int offset )
{
  final long months = bb.getLong( offset );
  offset += Long.BYTES;
  final long days = bb.getLong( offset );
  offset += Long.BYTES;
  final long seconds = bb.getLong( offset );
  offset += Long.BYTES;
  final int nanos = bb.getInt( offset );
  return DurationValue.duration( months, days, seconds, nanos );
}

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

@Override
public long getLong(int rowId, int colId) {
 _fixedSizeData.position(rowId * _rowSizeInBytes + _columnOffsets[colId]);
 return _fixedSizeData.getLong();
}

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

private static LocalRecoverable deserializeV1(byte[] serialized) throws IOException {
    final ByteBuffer bb = ByteBuffer.wrap(serialized).order(ByteOrder.LITTLE_ENDIAN);

    if (bb.getInt() != MAGIC_NUMBER) {
      throw new IOException("Corrupt data: Unexpected magic number.");
    }

    final long offset = bb.getLong();
    final byte[] targetFileBytes = new byte[bb.getInt()];
    final byte[] tempFileBytes = new byte[bb.getInt()];
    bb.get(targetFileBytes);
    bb.get(tempFileBytes);

    final String targetPath = new String(targetFileBytes, CHARSET);
    final String tempPath = new String(tempFileBytes, CHARSET);

    return new LocalRecoverable(new File(targetPath), new File(tempPath), offset);

  }
}

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

@Override
public void decode(ByteBuffer byteBuffer) {
  this.transactionId = byteBuffer.getLong();
  this.branchId = byteBuffer.getLong();
  this.status = BranchStatus.get(byteBuffer.get());
  short len = byteBuffer.getShort();
  if (len > 0) {
    byte[] bs = new byte[len];
    byteBuffer.get(bs);
    this.resourceId = new String(bs, UTF8);
  }
  int iLen = byteBuffer.getInt();
  if (iLen > 0) {
    byte[] bs = new byte[iLen];
    byteBuffer.get(bs);
    this.applicationData = new String(bs, UTF8);
  }
}

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

public static long bytesToLong(byte[] bytes) {
  ByteBuffer buffer = ByteBuffer.allocate(16);
  buffer.put(bytes);
  buffer.flip();// need flip
  return buffer.getLong();
}

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

/**
 * ByteBuffer based copy of logic of {@link BloomKFilter#getNumSetBits()}
 * @param bfBuffer
 * @param start
 * @return
 */
public static int getNumSetBits(ByteBuffer bfBuffer, int start)
{
 ByteBuffer view = bfBuffer.duplicate().order(ByteOrder.BIG_ENDIAN);
 view.position(start);
 int numLongs = view.getInt(1 + start);
 int setBits = 0;
 for (int i = 0, pos = START_OF_SERIALIZED_LONGS + start; i < numLongs; i++, pos += Long.BYTES) {
  setBits += Long.bitCount(view.getLong(pos));
 }
 return setBits;
}

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

/**
 * @throws NullPointerException if {@code buffer} is null
 */
public JournalSegmentDescriptor(ByteBuffer buffer) {
 this.buffer = buffer;
 this.version = buffer.getInt();
 this.id = buffer.getLong();
 this.index = buffer.getLong();
 this.maxSegmentSize = buffer.getInt();
 this.maxEntries = buffer.getInt();
 this.updated = buffer.getLong();
 this.locked = buffer.get() == 1;
}

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

private static long readAndValidate( StoreChannel channel, File fileName ) throws IOException
{
  ByteBuffer buffer = ByteBuffer.allocate( HEADER_SIZE );
  readHeader( channel, buffer );
  buffer.flip();
  byte storageStatus = buffer.get();
  if ( storageStatus != CLEAN_GENERATOR )
  {
    throw new InvalidIdGeneratorException( "Id file not properly shutdown [ " +
        fileName + " ], delete this id file and build a new one" );
  }
  return buffer.getLong();
}

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

@VisibleForTesting
static DLSN deserialize0(String dlsn) {
  byte[] data = Base64.decodeBase64(dlsn);
  ByteBuffer bb = ByteBuffer.wrap(data);
  byte version = bb.get();
  if (VERSION0 != version || VERSION0_LEN != data.length) {
    throw new IllegalArgumentException("Invalid DLSN " + dlsn);
  }
  return new DLSN(bb.getLong(), bb.getLong(), bb.getLong());
}

代码示例来源:origin: google/physical-web

private static String decodeUrnUuid(byte[] serviceData, StringBuilder urnBuilder) {
 ByteBuffer buf = ByteBuffer.wrap(serviceData);
 buf.order(ByteOrder.BIG_ENDIAN);
 long mostSignificantBytes, leastSignificantBytes;
 try {
  buf.position(3);
  mostSignificantBytes = buf.getLong();
  leastSignificantBytes = buf.getLong();
 } catch (BufferUnderflowException e){
  return "";
 }
 UUID uuid = new UUID(mostSignificantBytes, leastSignificantBytes);
 urnBuilder.append(uuid.toString());
 return urnBuilder.toString();
}

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

/**
 * populate metric values from the raw data.
 */
void populateMetric(Metric metric) {
  ByteBuffer bb = ByteBuffer.wrap(this.value, 0, METRIC_VALUE_SIZE);
  bb.get();  // version
  metric.setValue(bb.getDouble());
  metric.setCount(bb.getLong());
  metric.setMin(bb.getDouble());
  metric.setMax(bb.getDouble());
  metric.setSum(bb.getDouble());
}

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

/**
 * @param partFile Partition file.
 */
private int resolvePageSizeFromPartitionFile(Path partFile) throws IOException, IgniteCheckedException {
  try (FileIO fileIO = ioFactory.create(partFile.toFile())) {
    int minimalHdr = FilePageStore.HEADER_SIZE;
    if (fileIO.size() < minimalHdr)
      throw new IgniteCheckedException("Partition file is too small: " + partFile);
    ByteBuffer hdr = ByteBuffer.allocate(minimalHdr).order(ByteOrder.LITTLE_ENDIAN);
    fileIO.readFully(hdr);
    hdr.rewind();
    hdr.getLong(); // Read signature.
    hdr.getInt(); // Read version.
    hdr.get(); // Read type.
    int pageSize = hdr.getInt();
    if (pageSize == 2048) {
      U.quietAndWarn(log, "You are currently using persistent store with 2K pages (DataStorageConfiguration#" +
        "pageSize). If you use SSD disk, consider migrating to 4K pages for better IO performance.");
    }
    return pageSize;
  }
}

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

public static Date getNearlyTimeFromID(String msgID) {
  ByteBuffer buf = ByteBuffer.allocate(8);
  byte[] bytes = UtilAll.string2bytes(msgID);
  buf.put((byte) 0);
  buf.put((byte) 0);
  buf.put((byte) 0);
  buf.put((byte) 0);
  buf.put(bytes, 10, 4);
  buf.position(0);
  long spanMS = buf.getLong();
  Calendar cal = Calendar.getInstance();
  long now = cal.getTimeInMillis();
  cal.set(Calendar.DAY_OF_MONTH, 1);
  cal.set(Calendar.HOUR_OF_DAY, 0);
  cal.set(Calendar.MINUTE, 0);
  cal.set(Calendar.SECOND, 0);
  cal.set(Calendar.MILLISECOND, 0);
  long monStartTime = cal.getTimeInMillis();
  if (monStartTime + spanMS >= now) {
    cal.add(Calendar.MONTH, -1);
    monStartTime = cal.getTimeInMillis();
  }
  cal.setTimeInMillis(monStartTime + spanMS);
  return cal.getTime();
}

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

@Override
public CustomDerivedObject deserialize(byte[] bytes) {
  ByteBuffer buffer =  ByteBuffer.wrap(bytes);
  return new CustomDerivedObject(buffer.getLong(), buffer.getInt());
}

代码示例来源: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: 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.fileLength = byteBuffer().getLong();
  this.totalNum.set(byteBuffer().getInt());
  this.aliveNum.set(byteBuffer().getInt());
  this.isFull = byteBuffer().getInt();
  this.storeTxLogRecordId = byteBuffer().getLong();
}

相关文章