/** * 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 MutableRecordBatch nextBatch() { int remaining = buffer.remaining(); Integer batchSize = nextBatchSize(); if (batchSize == null || remaining < batchSize) return null; byte magic = buffer.get(buffer.position() + MAGIC_OFFSET); ByteBuffer batchSlice = buffer.slice(); batchSlice.limit(batchSize); buffer.position(buffer.position() + batchSize); if (magic > RecordBatch.MAGIC_VALUE_V1) return new DefaultRecordBatch(batchSlice); else return new AbstractLegacyRecordBatch.ByteBufferLegacyRecordBatch(batchSlice); }
/** * Get the last {@code byte} from this {@literal Buffer}. * * @return The last {@code byte}. */ public byte last() { int pos = buffer.position(); int limit = buffer.limit(); buffer.position(limit - 1); // go to right before last position byte b = buffer.get(); // get the last byte buffer.position(pos); // go back to original pos return b; }
/** * Writes 0x00 until the position is aligned to a multiple of 4. */ public void alignToFourBytesWithZeroFill() { int alignedPos = SizeOf.roundToTimesOfFour(data.position()); ensureBufferSize((alignedPos - data.position()) * SizeOf.UBYTE); while ((data.position() & 3) != 0) { data.put((byte) 0); } if (this.data.position() > this.dataBound) { this.dataBound = this.data.position(); } }
@SuppressWarnings("ByteBufferBackingArray") @NonNull public static byte[] toBytes(@NonNull ByteBuffer byteBuffer) { final byte[] result; SafeArray safeArray = getSafeArray(byteBuffer); if (safeArray != null && safeArray.offset == 0 && safeArray.limit == safeArray.data.length) { result = byteBuffer.array(); } else { ByteBuffer toCopy = byteBuffer.asReadOnlyBuffer(); result = new byte[toCopy.limit()]; toCopy.position(0); toCopy.get(result); } return result; }
private Hasher putBytesInternal(ByteBuffer readBuffer) { // If we have room for all of it, this is easy if (readBuffer.remaining() <= buffer.remaining()) { buffer.put(readBuffer); munchIfFull(); return this; } // First add just enough to fill buffer size, and munch that int bytesToCopy = bufferSize - buffer.position(); for (int i = 0; i < bytesToCopy; i++) { buffer.put(readBuffer.get()); } munch(); // buffer becomes empty here, since chunkSize divides bufferSize // Now process directly from the rest of the input buffer while (readBuffer.remaining() >= chunkSize) { process(readBuffer); } // Finally stick the remainder back in our usual buffer buffer.put(readBuffer); return this; }
private static <T> GenericIndexed<T> createGenericIndexedVersionOne(ByteBuffer byteBuffer, ObjectStrategy<T> strategy) { boolean allowReverseLookup = byteBuffer.get() == REVERSE_LOOKUP_ALLOWED; int size = byteBuffer.getInt(); ByteBuffer bufferToUse = byteBuffer.asReadOnlyBuffer(); bufferToUse.limit(bufferToUse.position() + size); byteBuffer.position(bufferToUse.limit()); return new GenericIndexed<>( bufferToUse, strategy, allowReverseLookup ); }
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 public Object read(ByteBuffer buffer) { int size = buffer.getInt(); if (size < 0) return null; if (size > buffer.remaining()) throw new SchemaException("Error reading bytes of size " + size + ", only " + buffer.remaining() + " bytes available"); ByteBuffer val = buffer.slice(); val.limit(size); buffer.position(buffer.position() + size); return val; }
/** * Try to read an EOL incrementing the buffer position if successful. * @return whether an EOL was consumed */ private boolean tryConsumeEndOfLine(ByteBuffer byteBuffer) { if (byteBuffer.remaining() > 0) { byte b = byteBuffer.get(); if (b == '\n') { return true; } else if (b == '\r') { if (byteBuffer.remaining() > 0 && byteBuffer.get() == '\n') { return true; } else { throw new StompConversionException("'\\r' must be followed by '\\n'"); } } // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer ((Buffer) byteBuffer).position(byteBuffer.position() - 1); } return false; }
private void doTestUpdateByteBufferWithOffsetPosition(byte[] bytes, ByteBuffer buffer, int offset) { buffer.put(bytes); buffer.flip(); buffer.position(offset); Checksum bufferCrc = Crc32C.create(); Checksums.update(bufferCrc, buffer, buffer.remaining()); assertEquals(Crc32C.compute(bytes, offset, buffer.remaining()), bufferCrc.getValue()); assertEquals(offset, buffer.position()); }
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()); } } }
/** * Create a copy of {@code value} into this instance using the encoding type of {@code charset}. * The copy will start at index {@code start} and copy {@code length} bytes. */ public AsciiString(CharSequence value, Charset charset, int start, int length) { CharBuffer cbuf = CharBuffer.wrap(value, start, start + length); CharsetEncoder encoder = CharsetUtil.encoder(charset); ByteBuffer nativeBuffer = ByteBuffer.allocate((int) (encoder.maxBytesPerChar() * length)); encoder.encode(cbuf, nativeBuffer, true); final int offset = nativeBuffer.arrayOffset(); this.value = Arrays.copyOfRange(nativeBuffer.array(), offset, offset + nativeBuffer.position()); this.offset = 0; this.length = this.value.length; }