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

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

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

ByteBuffer.putLong介绍

[英]Writes the given long to the specified index of this buffer.

The long is converted to bytes using the current byte order. The position is not changed.
[中]将给定的long写入此缓冲区的指定索引。
long使用当前字节顺序转换为字节。位置没有改变。

代码示例

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

@Override
public void writeDuration( long months, long days, long seconds, int nanos )
{
  buf.putLong( months );
  buf.putLong( days );
  buf.putLong( seconds );
  buf.putInt( nanos );
}

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

@Override
public byte[] getCacheKey()
{
 return ByteBuffer.allocate(2 * Long.BYTES).putLong(duration).putLong(origin).array();
}

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

@Override
public void write(long value) throws IOException
{
 if (outBuffer != null) {
  outBuffer.putLong(value);
 }
 if (outStream != null) {
  orderBuffer.rewind();
  orderBuffer.putLong(value);
  outStream.write(orderBuffer.array());
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
  byte[] data = ByteBuffer.allocate(12).putLong(dateModified).putInt(orientation).array();
  messageDigest.update(data);
  messageDigest.update(mimeType.getBytes(CHARSET));
 }
}

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

ByteBuffer buffer = ByteBuffer.allocate( chunkSize + CHUNK_HEADER_SIZE );
buffer.putShort( chunkSize );
    buffer.put( value.byteValue() );
    buffer.putShort( value.shortValue() );
    buffer.putInt( value.intValue() );
    buffer.putLong( value.longValue() );
buffer.flip();
return ByteBufUtil.hexDump( buffer.array() );

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

/**
 * Adds metadata at current position (position is moved forward). Does not flip or reset.
 * @return The passed <code>destination</code> with metadata added.
 */
private ByteBuffer addMetaData(final ByteBuffer destination, boolean includeNextBlockMetadata) {
 destination.put(this.fileContext.isUseHBaseChecksum() ? (byte) 1 : (byte) 0);
 destination.putLong(this.offset);
 if (includeNextBlockMetadata) {
  destination.putInt(this.nextBlockOnDiskSize);
 }
 return destination;
}

代码示例来源:origin: qunarcorp/qmq

@Override
  public AppendMessageResult<Void> doAppend(long baseOffset, ByteBuffer targetBuffer, int freeSpace, ConsumerLogMessage message) {
    workingBuffer.clear();
    final long wroteOffset = baseOffset + targetBuffer.position();
    workingBuffer.flip();
    workingBuffer.limit(CONSUMER_LOG_UNIT_BYTES);
    workingBuffer.putLong(System.currentTimeMillis());
    workingBuffer.putLong(message.getOffset());
    workingBuffer.putInt(message.getSize());
    workingBuffer.putShort(message.getHeaderSize());
    targetBuffer.put(workingBuffer.array(), 0, CONSUMER_LOG_UNIT_BYTES);
    return new AppendMessageResult<>(AppendMessageStatus.SUCCESS, wroteOffset, CONSUMER_LOG_UNIT_BYTES);
  }
}

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

public void writeLong(WritableByteChannel out, long longValue) throws IOException
{
 final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
 buffer.putLong(longValue);
 buffer.flip();
 Channels.writeFully(out, buffer);
}

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

public Binary toBinary() {
 ByteBuffer buf = ByteBuffer.allocate(12);
 buf.order(ByteOrder.LITTLE_ENDIAN);
 buf.putLong(timeOfDayNanos);
 buf.putInt(julianDay);
 buf.flip();
 return Binary.fromByteBuffer(buf);
}

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

private static byte[] encode(final UUID uuid) {
  final ByteBuffer bb = ByteBuffer.wrap(new byte[17]);
  bb.put((byte) 0x09);
  bb.putLong(uuid.getMostSignificantBits());
  bb.putLong(uuid.getLeastSignificantBits());
  return bb.array();
}

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

@Test
public void testUpdateLong() {
  final long value = Integer.MAX_VALUE + 1;
  final ByteBuffer buffer = ByteBuffer.allocate(8);
  buffer.putLong(value);
  Checksum crc1 = new Crc32();
  Checksum crc2 = new Crc32();
  Checksums.updateLong(crc1, value);
  crc2.update(buffer.array(), buffer.arrayOffset(), 8);
  assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
}

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

@Override
public void reset()
{
 // Clear the entire usedFlagBuffer
 final int usedFlagBufferCapacity = usedFlagBuffer.capacity();
 // putLong() instead of put() can boost the performance of clearing the buffer
 final int n = (usedFlagBufferCapacity / Long.BYTES) * Long.BYTES;
 for (int i = 0; i < n; i += Long.BYTES) {
  usedFlagBuffer.putLong(i, 0L);
 }
 for (int i = n; i < usedFlagBufferCapacity; i++) {
  usedFlagBuffer.put(i, (byte) 0);
 }
}

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

private void write( StoreChannel channel, long time, long identifier, long version, long lastCommittedTxId,
    long indexVersion ) throws IOException
{
  buf.clear();
  buf.putLong( time ).putLong( identifier ).putLong( version ).putLong( lastCommittedTxId ).putLong( indexVersion );
  buf.flip();
  channel.writeAll( buf, 0 );
  channel.force( true );
}

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

/**
 * Serializes histogram fields that are common to both the full and sparse encoding modes.
 *
 * @param buf Destination buffer
 */
private void writeByteBufferCommonFields(ByteBuffer buf)
{
 buf.putDouble(lowerLimit);
 buf.putDouble(upperLimit);
 buf.putInt(numBuckets);
 buf.put((byte) outlierHandlingMode.ordinal());
 buf.putLong(count);
 buf.putLong(lowerOutlierCount);
 buf.putLong(upperOutlierCount);
 buf.putLong(missingValueCount);
 buf.putDouble(max);
 buf.putDouble(min);
}

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

@Override
public void write(FileChannel fileChannel) throws IOException {
  byteBuffer().position(0);
  byteBuffer().putShort(magic);
  byteBuffer().putLong(fileLength);
  byteBuffer().putInt(totalNum.get());
  byteBuffer().putInt(aliveNum.get());
  byteBuffer().putInt(isFull);
  byteBuffer().putLong(storeTxLogRecordId);
  byteBuffer().flip();
  fileChannel.position(0);
  fileChannel.write(byteBuffer());
  fileChannel.force(true);
}

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

private MemoryRecords buildOverflowBatch(int remaining) {
  // We do not have any records left to down-convert. Construct an overflow message for the length remaining.
  // This message will be ignored by the consumer because its length will be past the length of maximum
  // possible response size.
  // DefaultRecordBatch =>
  //      BaseOffset => Int64
  //      Length => Int32
  //      ...
  ByteBuffer overflowMessageBatch = ByteBuffer.allocate(
      Math.max(MIN_OVERFLOW_MESSAGE_LENGTH, Math.min(remaining + 1, MAX_READ_SIZE)));
  overflowMessageBatch.putLong(-1L);
  // Fill in the length of the overflow batch. A valid batch must be at least as long as the minimum batch
  // overhead.
  overflowMessageBatch.putInt(Math.max(remaining + 1, DefaultRecordBatch.RECORD_BATCH_OVERHEAD));
  log.debug("Constructed overflow message batch for partition {} with length={}", topicPartition(), remaining);
  return MemoryRecords.readableRecords(overflowMessageBatch);
}

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

public ByteBuffer toByteBuffer()
{
 return ByteBuffer.allocate(Long.BYTES + Double.BYTES + Double.BYTES)
          .putLong(count)
          .putDouble(sum)
          .putDouble(nvariance);
}

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

@Override
  public void write(FileChannel fileChannel) throws IOException {
    byteBuffer().position(0);
    byteBuffer().putShort(magic);
    byteBuffer().putLong(this.firstRecordId);
    byteBuffer().flip();

    fileChannel.position(0);
    fileChannel.write(byteBuffer());
    fileChannel.force(true);
  }
}

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

/**
 * @param states Cache states.
 */
private static void putCacheStates(ByteBuffer buf, Map<Integer, CacheState> states) {
  buf.putShort((short)states.size());
  for (Map.Entry<Integer, CacheState> entry : states.entrySet()) {
    buf.putInt(entry.getKey());
    CacheState state = entry.getValue();
    // Need 2 bytes for the number of partitions.
    buf.putShort((short)state.size());
    for (int i = 0; i < state.size(); i++) {
      buf.putShort((short)state.partitionByIndex(i));
      buf.putLong(state.partitionSizeByIndex(i));
      buf.putLong(state.partitionCounterByIndex(i));
    }
  }
}

相关文章