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

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

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

ByteBuffer.putInt介绍

[英]Writes the given int to the current position and increases the position by 4.

The int is converted to bytes using the current byte order.
[中]将给定的int写入当前位置,并将该位置增加4。
int使用当前字节顺序转换为字节。

代码示例

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

static void writeHeader(ByteBuffer buffer, long offset, int size) {
  buffer.putLong(offset);
  buffer.putInt(size);
}

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

@Override
public byte[] getCacheKey()
{
 return ByteBuffer.allocate(1 + 8)
          .put(ExtractionCacheHelper.CACHE_TYPE_ID_SUBSTRING)
          .putInt(this.index)
          .putInt(this.end)
          .array();
}

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

@Override
public void write(ByteBuffer buffer, Object o) {
  if (o == null) {
    buffer.putInt(-1);
    return;
  }
  ByteBuffer arg = (ByteBuffer) o;
  int pos = arg.position();
  buffer.putInt(arg.remaining());
  buffer.put(arg);
  arg.position(pos);
}

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

@Override
 public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
  messageDigest.update(ID_BYTES);

  byte[] degreesData = ByteBuffer.allocate(4).putInt(degreesToRotate).array();
  messageDigest.update(degreesData);
 }
}

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

public static void writeInt(WritableByteChannel out, int intValue) throws IOException
{
 final ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
 buffer.putInt(intValue);
 buffer.flip();
 Channels.writeFully(out, buffer);
}

代码示例来源: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 static ImmutableRTree newImmutableFromMutable(RTree rTree)
{
 if (rTree.getSize() == 0) {
  return empty();
 }
 ByteBuffer buffer = ByteBuffer.allocate(calcNumBytes(rTree));
 buffer.put(VERSION);
 buffer.putInt(rTree.getNumDims());
 rTree.getRoot().storeInByteBuffer(buffer, buffer.position());
 buffer.position(0);
 return new ImmutableRTree(buffer, rTree.getBitmapFactory());
}

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

public byte[] toBytes()
{
 ByteBuffer outBytes = ByteBuffer.wrap(new byte[4 + groupKey.length + sortKey.length]);
 outBytes.putInt(groupKey.length);
 outBytes.put(groupKey);
 outBytes.put(sortKey);
 return outBytes.array();
}

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

@Override
public byte[] serialize(HadoopFsRecoverable obj) throws IOException {
  final byte[] targetFileBytes = obj.targetFile().toString().getBytes(CHARSET);
  final byte[] tempFileBytes = obj.tempFile().toString().getBytes(CHARSET);
  final byte[] targetBytes = new byte[20 + targetFileBytes.length + tempFileBytes.length];
  ByteBuffer bb = ByteBuffer.wrap(targetBytes).order(ByteOrder.LITTLE_ENDIAN);
  bb.putInt(MAGIC_NUMBER);
  bb.putLong(obj.offset());
  bb.putInt(targetFileBytes.length);
  bb.putInt(tempFileBytes.length);
  bb.put(targetFileBytes);
  bb.put(tempFileBytes);
  return targetBytes;
}

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

@Test
public void testUpdateInt() {
  final int value = 1000;
  final ByteBuffer buffer = ByteBuffer.allocate(4);
  buffer.putInt(value);
  Checksum crc1 = Crc32C.create();
  Checksum crc2 = Crc32C.create();
  Checksums.updateInt(crc1, value);
  crc2.update(buffer.array(), buffer.arrayOffset(), 4);
  assertEquals("Crc values should be the same", crc1.getValue(), crc2.getValue());
}

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

public static void write(ByteBuffer buf, int dataType, int data) {
 buf.putShort((short) SIZEOF); // size
 buf.put((byte) 0); // res0
 buf.put((byte) dataType); // dataType
 buf.putInt(data); // data
}

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

public static ByteBuffer socketAddress2ByteBuffer(final SocketAddress socketAddress, final ByteBuffer byteBuffer) {
  InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
  byteBuffer.put(inetSocketAddress.getAddress().getAddress(), 0, 4);
  byteBuffer.putInt(inetSocketAddress.getPort());
  byteBuffer.flip();
  return byteBuffer;
}

代码示例来源: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: 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 int storeInByteBuffer(ByteBuffer buffer, int position)
{
 buffer.position(position);
 buffer.putShort((short) (((isLeaf ? 0x1 : 0x0) << 15) | getChildren().size()));
 for (float v : getMinCoordinates()) {
  buffer.putFloat(v);
 }
 for (float v : getMaxCoordinates()) {
  buffer.putFloat(v);
 }
 byte[] bytes = bitmap.toBytes();
 buffer.putInt(bytes.length);
 buffer.put(bytes);
 int pos = buffer.position();
 int childStartOffset = pos + getChildren().size() * Integer.BYTES;
 for (Node child : getChildren()) {
  buffer.putInt(pos, childStartOffset);
  childStartOffset = child.storeInByteBuffer(buffer, childStartOffset);
  pos += Integer.BYTES;
 }
 return childStartOffset;
}

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

private void writeWithHeader(WritableByteChannel channel, byte[] body) throws IOException {
  synchronized (writeDataLock) {
    writeHeader.clear();
    writeHeader.putInt(body.length);
    writeHeader.flip();
    channel.write(writeHeader);
    channel.write(ByteBuffer.wrap(body));
  }
}

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

@Override
public byte[] serialize() {
  final int size = Integer.BYTES + content.length;
  return ByteBuffer.allocate(size)
      .putInt(content.length)
      .put(content)
      .array();
}

相关文章