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

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

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

ByteBuffer.array介绍

[英]Returns the byte array which this buffer is based on, if there is one.
[中]返回此缓冲区所基于的字节数组(如果有)。

代码示例

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

/** Updates this hasher with bytes from the given buffer. */
protected void update(ByteBuffer b) {
 if (b.hasArray()) {
  update(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   update(b.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: google/guava

@Override
public HashCode hashUnencodedChars(CharSequence input) {
 int len = input.length();
 ByteBuffer buffer = ByteBuffer.allocate(len * 2).order(ByteOrder.LITTLE_ENDIAN);
 for (int i = 0; i < len; i++) {
  buffer.putChar(input.charAt(i));
 }
 return hashBytes(buffer.array());
}

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

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

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

public byte[] build()
 {
  final ByteBuffer buffer = ByteBuffer.allocate(size);
  buffer.put(id);

  for (Item item : items) {
   buffer.put(item.typeKey).put(item.item);
  }

  return buffer.array();
 }
}

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

private static byte[] createUniqIDBuffer() {
  ByteBuffer buffer = ByteBuffer.allocate(4 + 2);
  long current = System.currentTimeMillis();
  if (current >= nextStartTime) {
    setStartTime(current);
  }
  buffer.position(0);
  buffer.putInt((int) (System.currentTimeMillis() - startTime));
  buffer.putShort((short) COUNTER.getAndIncrement());
  return buffer.array();
}

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

public void testFromByteArray_withTooLongArrayInputThrowsIllegalArgumentException() {
 byte[] buffer = MANY_VALUES_PAIRED_STATS.toByteArray();
 byte[] tooLongByteArray =
   ByteBuffer.allocate(buffer.length + 2)
     .order(ByteOrder.LITTLE_ENDIAN)
     .put(buffer)
     .putChar('.')
     .array();
 try {
  PairedStats.fromByteArray(tooLongByteArray);
  fail("Expected IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
}

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

@Override
public Object get(ByteBuffer buf, int position)
{
 ByteBuffer dataCopyBuffer = ByteBuffer.allocate(HyperLogLogCollector.getLatestNumBytesForDenseStorage());
 ByteBuffer mutationBuffer = buf.duplicate();
 mutationBuffer.position(position);
 mutationBuffer.get(dataCopyBuffer.array());
 return HyperLogLogCollector.makeCollector(dataCopyBuffer);
}

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

@Nullable
private static SafeArray getSafeArray(@NonNull ByteBuffer byteBuffer) {
 if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) {
  return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
 }
 return null;
}

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

@Override
public HashCode hashLong(long input) {
 return hashBytes(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(input).array());
}

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

public void writeString(String s, String tag) throws IOException {
  if (s == null) {
    writeInt(-1, "len");
    return;
  }
  ByteBuffer bb = stringToByteBuffer(s);
  writeInt(bb.remaining(), "len");
  out.write(bb.array(), bb.position(), bb.limit());
}

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

@Override
public int setBytes(int index, InputStream in, int length) throws IOException {
  ensureAccessible();
  if (buffer.hasArray()) {
    return in.read(buffer.array(), buffer.arrayOffset() + index, length);
  } else {
    byte[] tmp = ByteBufUtil.threadLocalTempArray(length);
    int readBytes = in.read(tmp, 0, length);
    if (readBytes <= 0) {
      return readBytes;
    }
    ByteBuffer tmpBuf = internalNioBuffer();
    tmpBuf.clear().position(index);
    tmpBuf.put(tmp, 0, readBytes);
    return readBytes;
  }
}

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

/**
 * Gets a byte array representation of this instance.
 *
 * <p><b>Note:</b> No guarantees are made regarding stability of the representation between
 * versions.
 */
public byte[] toByteArray() {
 ByteBuffer buffer = ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN);
 xStats.writeTo(buffer);
 yStats.writeTo(buffer);
 buffer.putDouble(sumOfProductsOfDeltas);
 return buffer.array();
}

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

/**
 * Gets a byte array representation of this instance.
 *
 * <p><b>Note:</b> No guarantees are made regarding stability of the representation between
 * versions.
 */
public byte[] toByteArray() {
 ByteBuffer buff = ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN);
 writeTo(buff);
 return buff.array();
}

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

private @Nullable float[] parseMetadata(ByteBuffer data) {
 if (data.remaining() != 16) {
  return null;
 }
 scratch.reset(data.array(), data.limit());
 scratch.setPosition(data.arrayOffset() + 4); // skip reserved bytes too.
 float[] result = new float[3];
 for (int i = 0; i < 3; i++) {
  result[i] = Float.intBitsToFloat(scratch.readLittleEndianInt());
 }
 return result;
}

代码示例来源:origin: Tencent/tinker

private void ensureBufferSize(int bytes) {
  if (this.data.position() + bytes > this.data.limit()) {
    if (this.isResizeAllowed) {
      byte[] array = this.data.array();
      byte[] newArray = new byte[array.length + bytes + (array.length >> 1)];
      System.arraycopy(array, 0, newArray, 0, this.data.position());
      int lastPos = this.data.position();
      this.data = ByteBuffer.wrap(newArray);
      this.data.order(ByteOrder.LITTLE_ENDIAN);
      this.data.position(lastPos);
      this.data.limit(this.data.capacity());
    }
  }
}

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

@Override
public Hasher putBytes(ByteBuffer b) {
 if (b.hasArray()) {
  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   putByte(b.get());
  }
 }
 return this;
}

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

相关文章

微信公众号

最新文章

更多