/** * 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; } }
@Test(expected = IllegalArgumentException.class) public void testWriteTransactionalNotAllowedMagicV1() { ByteBuffer buffer = ByteBuffer.allocate(128); buffer.position(bufferOffset); long pid = 9809; short epoch = 15; int sequence = 2342; new MemoryRecordsBuilder(buffer, RecordBatch.MAGIC_VALUE_V1, compressionType, TimestampType.CREATE_TIME, 0L, 0L, pid, epoch, sequence, true, false, RecordBatch.NO_PARTITION_LEADER_EPOCH, buffer.capacity()); }
@Override public void doMap(NullWritable key, Text value, Context context) throws IOException, InterruptedException { tmpBuf.clear(); int size = value.getLength()+ 1; if (size >= tmpBuf.capacity()) { tmpBuf = ByteBuffer.allocate(countNewSize(tmpBuf.capacity(), size)); } tmpBuf.put(Bytes.toBytes(index)[3]); tmpBuf.put(value.getBytes(), 0, value.getLength()); outputKey.set(tmpBuf.array(), 0, tmpBuf.position()); sortableKey.init(outputKey, type); context.write(sortableKey, NullWritable.get()); }
@Override public void queueInput(ByteBuffer inputBuffer) { Assertions.checkState(outputChannels != null); int position = inputBuffer.position(); int limit = inputBuffer.limit(); int frameCount = (limit - position) / (2 * channelCount); int outputSize = frameCount * outputChannels.length * 2; if (buffer.capacity() < outputSize) { buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder()); } else { buffer.clear(); } while (position < limit) { for (int channelIndex : outputChannels) { buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex)); } position += channelCount * 2; } inputBuffer.position(limit); buffer.flip(); outputBuffer = buffer; }
@Test public void getOrientation_withExifSegmentAndPreambleButLessThanLength_returnsUnknown() throws IOException { ByteBuffer jpegHeaderBytes = getExifMagicNumber(); ByteBuffer exifSegmentPreamble = ByteBuffer.wrap(DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES); ByteBuffer data = ByteBuffer.allocate(2 + 1 + 1 + 2 + exifSegmentPreamble.capacity()); data.put(jpegHeaderBytes) .put((byte) DefaultImageHeaderParser.SEGMENT_START_ID) .put((byte) DefaultImageHeaderParser.EXIF_SEGMENT_TYPE) // SEGMENT_LENGTH, add two because length includes the segment length short, and one to go // beyond the preamble bytes length for the test. .putShort( (short) (DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES.length + 2 + 1)) .put(DefaultImageHeaderParser.JPEG_EXIF_SEGMENT_PREAMBLE_BYTES); data.position(0); DefaultImageHeaderParser parser = new DefaultImageHeaderParser(); assertEquals(ImageHeaderParser.UNKNOWN_ORIENTATION, parser.getOrientation(data, byteArrayPool)); }
@Override public void queueInput(ByteBuffer buffer) { int remaining = buffer.remaining(); if (remaining == 0) { return; } audioBufferSink.handleBuffer(buffer.asReadOnlyBuffer()); if (this.buffer.capacity() < remaining) { this.buffer = ByteBuffer.allocateDirect(remaining).order(ByteOrder.nativeOrder()); } else { this.buffer.clear(); } this.buffer.put(buffer); this.buffer.flip(); outputBuffer = this.buffer; }
updateNow(); while (buf.isReadable()) { if (incomingBuffer.remaining() > buf.readableBytes()) { int newLimit = incomingBuffer.position() + buf.readableBytes(); incomingBuffer.limit(newLimit); incomingBuffer.limit(incomingBuffer.capacity()); incomingBuffer.flip(); if (incomingBuffer == lenBuffer) { recvCount.getAndIncrement(); } else if (!initialized) { readConnectResult(); lenBuffer.clear(); incomingBuffer = lenBuffer; initialized = true; } else { sendThread.readResponse(incomingBuffer); lenBuffer.clear(); incomingBuffer = lenBuffer; updateLastHeard();
private long flushFreeIds0( ByteBuffer writeBuffer ) throws IOException { channel.position( channel.size() ); writeBuffer.clear(); while ( !freeIds.isEmpty() ) { long id = freeIds.dequeue(); if ( id == NO_RESULT ) { continue; } writeBuffer.putLong( id ); if ( writeBuffer.position() == writeBuffer.capacity() ) { writeBuffer.flip(); channel.writeAll( writeBuffer ); writeBuffer.clear(); } } writeBuffer.flip(); if ( writeBuffer.hasRemaining() ) { channel.writeAll( writeBuffer ); } return channel.position(); }
/** * Wrap the visible portion of a {@link java.nio.ByteBuffer}. */ public static Slice wrappedBuffer(ByteBuffer buffer) { if (buffer.isDirect()) { long address = getAddress(buffer); return new Slice(null, address + buffer.position(), buffer.remaining(), buffer.capacity(), buffer); } if (buffer.hasArray()) { return new Slice(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } throw new IllegalArgumentException("cannot wrap " + buffer.getClass().getName()); }
@Override public void reset() { // Clear the entire usedFlagBuffer final int usedFlagBufferCapacity = usedFlagBuffer.capacity(); // putLong() instead of put() can boost the performance of clearing the buffer final int n = (usedFlagBufferCapacity / Long.BYTES) * Long.BYTES; for (int i = 0; i < n; i += Long.BYTES) { usedFlagBuffer.putLong(i, 0L); } for (int i = n; i < usedFlagBufferCapacity; i++) { usedFlagBuffer.put(i, (byte) 0); } }
private void createAndVerifyNewWriteBuffer(ByteBuffer buffer, boolean useDirectBuffer) { buffer.position(buffer.capacity()); ByteBuffer newBuffer = Buffers.expandWriteBufferIfNeeded(Buffers.BufferType.UNTRACKED, buffer, 500, mock(DMStats.class)); assertEquals(buffer.position(), newBuffer.position()); assertEquals(500, newBuffer.capacity()); newBuffer.flip(); for (int i = 0; i < 256; i++) { byte expected = (byte) (i & 0xff); byte actual = (byte) (newBuffer.get() & 0xff); assertEquals(expected, actual); } }
/** * Creates an instance of this class that will write to the received `buffer` up to its `limit`. If necessary to * satisfy `write` or `position` calls, larger buffers will be allocated so the {@link #buffer()} method may return * a different buffer than the received `buffer` parameter. * * Prefer one of the constructors that allocate the internal buffer for clearer semantics. */ public ByteBufferOutputStream(ByteBuffer buffer) { this.buffer = buffer; this.initialPosition = buffer.position(); this.initialCapacity = buffer.capacity(); }
@Override public FlushableChannel put( byte[] value, int length ) throws IOException { int offset = 0; while ( offset < length ) { int chunkSize = min( length - offset, buffer.capacity() >> 1 ); bufferWithGuaranteedSpace( chunkSize ).put( value, offset, chunkSize ); offset += chunkSize; } return this; }