/** 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()); } } }
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(); }
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) { } }
@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); }
@Nullable private static SafeArray getSafeArray(@NonNull ByteBuffer byteBuffer) { if (!byteBuffer.isReadOnly() && byteBuffer.hasArray()) { return new SafeArray(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit()); } return null; }
@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()); }
@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; } }
@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()); }
/** * 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(); }
/** * 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(); }
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; }
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()); } } }