private Object convertToByteBuffer(@Nullable Object source, TypeDescriptor sourceType) { byte[] bytes = (byte[]) (source instanceof byte[] ? source : this.conversionService.convert(source, sourceType, BYTE_ARRAY_TYPE)); if (bytes == null) { return ByteBuffer.wrap(new byte[0]); } ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length); byteBuffer.put(bytes); // Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since // otherwise the overridden ByteBuffer-returning rewind method would be chosen // which isn't available on JDK 8. return ((Buffer) byteBuffer).rewind(); }
private void expandBuffer(int remainingRequired) { int expandSize = Math.max((int) (buffer.limit() * REALLOCATION_FACTOR), buffer.position() + remainingRequired); ByteBuffer temp = ByteBuffer.allocate(expandSize); int limit = limit(); buffer.flip(); temp.put(buffer); buffer.limit(limit); // reset the old buffer's position so that the partial data in the new buffer cannot be mistakenly consumed // we should ideally only do this for the original buffer, but the additional complexity doesn't seem worth it buffer.position(initialPosition); buffer = temp; }
@Override protected void processRemaining(ByteBuffer buffer) { b += buffer.remaining(); for (int i = 0; buffer.hasRemaining(); i += 8) { finalM ^= (buffer.get() & 0xFFL) << i; } }
/** * Flips the buffer output buffer so we can start reading bytes from it. If we are starting to * drain because there was overflow, and there aren't actually any characters to drain, then the * overflow must be due to a small output buffer. */ private void startDraining(boolean overflow) { byteBuffer.flip(); if (overflow && byteBuffer.remaining() == 0) { byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2); } else { draining = true; } }
/** * Initialize an instance based upon the underlying storage from {@code value}. * There is a potential to share the underlying array storage if {@link ByteBuffer#hasArray()} is {@code true}. * if {@code copy} is {@code true} a copy will be made of the memory. * if {@code copy} is {@code false} the underlying storage will be shared, if possible. */ public AsciiString(ByteBuffer value, boolean copy) { this(value, value.position(), value.remaining(), copy); }
public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) { if (!buffer.hasRemaining()) { return EMPTY_BUFFER; } if (buffer.hasArray()) { return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { return new ByteBufferBackedChannelBuffer(buffer); } }
private ByteBuffer assembleChunksAndReset() { ByteBuffer result; if (this.chunks.size() == 1) { result = this.chunks.remove(); } else { result = ByteBuffer.allocate(getBufferSize()); for (ByteBuffer partial : this.chunks) { result.put(partial); } result.flip(); } this.chunks.clear(); this.expectedContentLength = null; return result; }
public void testFromByteArrayWithTooShortArrayInputThrowsIllegalArgumentException() { byte[] buffer = MANY_VALUES_STATS_VARARGS.toByteArray(); byte[] tooShortByteArray = ByteBuffer.allocate(buffer.length - 1) .order(ByteOrder.LITTLE_ENDIAN) .put(buffer, 0, Stats.BYTES - 1) .array(); try { Stats.fromByteArray(tooShortByteArray); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
private DataBuffer toDataBuffer(String body, Charset charset) { byte[] bytes = body.getBytes(charset); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); return this.bufferFactory.wrap(byteBuffer); }
/** Returns a {@link HashCode} for a sequence of longs, in big-endian order. */ private static HashCode toHashCode(long... longs) { ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN); for (long x : longs) { bb.putLong(x); } return HashCode.fromBytes(bb.array()); }
/** * This is invoked for the last bytes of the input, which are not enough to fill a whole chunk. * The passed {@code ByteBuffer} is guaranteed to be non-empty. * * <p>This implementation simply pads with zeros and delegates to {@link #process(ByteBuffer)}. */ protected void processRemaining(ByteBuffer bb) { bb.position(bb.limit()); // move at the end bb.limit(chunkSize + 7); // get ready to pad with longs while (bb.position() < chunkSize) { bb.putLong(0); } bb.limit(chunkSize); bb.flip(); process(bb); }
@Override public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) { if (dst instanceof ByteBufferBackedChannelBuffer) { ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst; ByteBuffer data = bbdst.buffer.duplicate(); data.limit(dstIndex + length).position(dstIndex); getBytes(index, data); } else if (buffer.hasArray()) { dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length); } else { dst.setBytes(dstIndex, this, index, length); } }
/** * 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(); }