Class CompositeReadableBuffer
java.lang.Object
io.grpc.internal.AbstractReadableBuffer
io.grpc.internal.CompositeReadableBuffer
- All Implemented Interfaces:
ReadableBuffer, Closeable, AutoCloseable
A
ReadableBuffer that is composed of 0 or more ReadableBuffers. This provides a
facade that allows multiple buffers to be treated as one.
When a buffer is added to a composite, its life cycle is controlled by the composite. Once the composite has read past the end of a given buffer, that buffer is automatically closed and removed from the composite.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interfaceprivate static interfaceA simple read operation to perform on a singleReadableBuffer. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Queue<ReadableBuffer> private static final CompositeReadableBuffer.NoThrowReadOperation<byte[]> private static final CompositeReadableBuffer.NoThrowReadOperation<ByteBuffer> private booleanprivate final Deque<ReadableBuffer> private intprivate Deque<ReadableBuffer> private static final CompositeReadableBuffer.NoThrowReadOperation<Void> private static final CompositeReadableBuffer.ReadOperation<OutputStream> private static final CompositeReadableBuffer.NoThrowReadOperation<Void> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBuffer(ReadableBuffer buffer) Adds a newReadableBufferat the end of the buffer list.private voidRemoves one buffer from the front and closes it.private voidIf the current buffer is exhausted, removes and closes it.booleanIndicates whether or notReadableBuffer.getByteBuffer()operation is supported for this buffer.voidclose()Closes this buffer and releases any resources.private voidenqueueBuffer(ReadableBuffer buffer) private <T> intexecute(CompositeReadableBuffer.ReadOperation<T> op, int length, T dest, int value) Executes the givenCompositeReadableBuffer.ReadOperationagainst theReadableBuffers required to satisfy the requestedlength.private <T> intexecuteNoThrow(CompositeReadableBuffer.NoThrowReadOperation<T> op, int length, T dest, int value) Gets aByteBufferthat contains some bytes of the content next to be read, ornullif this buffer has been exhausted.voidmark()Marks the current position in this buffer.booleanIndicates whether or notReadableBuffer.mark()operation is supported for this buffer.intGets the current number of readable bytes remaining in this buffer.voidreadBytes(byte[] dest, int destOffset, int length) Readslengthbytes from this buffer and writes them to the destination array.readBytes(int length) Readslengthbytes from this buffer and returns a new Buffer containing them.voidreadBytes(CompositeReadableBuffer dest, int length) Readslengthbytes from this buffer and writes them to the destination buffer.voidreadBytes(OutputStream dest, int length) Readslengthbytes from this buffer and writes them to the destination stream.voidreadBytes(ByteBuffer dest) Reads from this buffer until the destination's position reaches its limit, and increases the read position by the number of the transferred bytes.intReads the next unsigned byte from this buffer and increments the read position by 1.voidreset()Repositions this buffer to the position at the timeReadableBuffer.mark()was last called on this buffer.voidskipBytes(int length) Increments the read position by the given length.Methods inherited from class AbstractReadableBuffer
array, arrayOffset, checkReadable, hasArray, readIntMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ReadableBuffer
touch
-
Field Details
-
readableBuffers
-
rewindableBuffers
-
readableBytes
private int readableBytes -
buffers
-
marked
private boolean marked -
UBYTE_OP
-
SKIP_OP
-
BYTE_ARRAY_OP
-
BYTE_BUF_OP
-
STREAM_OP
-
-
Constructor Details
-
CompositeReadableBuffer
public CompositeReadableBuffer(int initialCapacity) -
CompositeReadableBuffer
public CompositeReadableBuffer()
-
-
Method Details
-
addBuffer
Adds a newReadableBufferat the end of the buffer list. After a buffer is added, it is expected that thisCompositeBufferhas complete ownership. Any attempt to modify the buffer (i.e. modifying the readable bytes) may result in corruption of the internal state of thisCompositeBuffer. -
enqueueBuffer
-
readableBytes
public int readableBytes()Description copied from interface:ReadableBufferGets the current number of readable bytes remaining in this buffer. -
readUnsignedByte
public int readUnsignedByte()Description copied from interface:ReadableBufferReads the next unsigned byte from this buffer and increments the read position by 1. -
skipBytes
public void skipBytes(int length) Description copied from interface:ReadableBufferIncrements the read position by the given length. -
readBytes
public void readBytes(byte[] dest, int destOffset, int length) Description copied from interface:ReadableBufferReadslengthbytes from this buffer and writes them to the destination array. Increments the read position bylength.- Parameters:
dest- the destination array to receive the bytes.destOffset- the starting offset in the destination array.length- the number of bytes to be copied.
-
readBytes
Description copied from interface:ReadableBufferReads from this buffer until the destination's position reaches its limit, and increases the read position by the number of the transferred bytes.- Parameters:
dest- the destination buffer to receive the bytes.
-
readBytes
Description copied from interface:ReadableBufferReadslengthbytes from this buffer and writes them to the destination stream. Increments the read position bylength. If the required bytes are not readable, throwsIndexOutOfBoundsException.- Parameters:
dest- the destination stream to receive the bytes.length- the number of bytes to be copied.- Throws:
IOException- thrown if any error was encountered while writing to the stream.
-
readBytes
Readslengthbytes from this buffer and writes them to the destination buffer. Increments the read position bylength. If the required bytes are not readable, throwsIndexOutOfBoundsException.- Parameters:
dest- the destination buffer to receive the bytes.length- the number of bytes to be copied.- Throws:
IndexOutOfBoundsException- if required bytes are not readable
-
readBytes
Description copied from interface:ReadableBufferReadslengthbytes from this buffer and returns a new Buffer containing them. Some implementations may return a Buffer sharing the backing memory with this buffer to prevent copying. However, that means that the returned buffer may keep the (possibly much larger) backing memory in use even after this buffer is closed.- Parameters:
length- the number of bytes to contain in returned Buffer.
-
markSupported
public boolean markSupported()Description copied from interface:ReadableBufferIndicates whether or notReadableBuffer.mark()operation is supported for this buffer.- Specified by:
markSupportedin interfaceReadableBuffer- Overrides:
markSupportedin classAbstractReadableBuffer
-
mark
public void mark()Description copied from interface:ReadableBufferMarks the current position in this buffer. A subsequent call to theReadableBuffer.reset()method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.- Specified by:
markin interfaceReadableBuffer- Overrides:
markin classAbstractReadableBuffer
-
reset
public void reset()Description copied from interface:ReadableBufferRepositions this buffer to the position at the timeReadableBuffer.mark()was last called on this buffer.- Specified by:
resetin interfaceReadableBuffer- Overrides:
resetin classAbstractReadableBuffer
-
byteBufferSupported
public boolean byteBufferSupported()Description copied from interface:ReadableBufferIndicates whether or notReadableBuffer.getByteBuffer()operation is supported for this buffer.- Specified by:
byteBufferSupportedin interfaceReadableBuffer- Overrides:
byteBufferSupportedin classAbstractReadableBuffer
-
getByteBuffer
Description copied from interface:ReadableBufferGets aByteBufferthat contains some bytes of the content next to be read, ornullif this buffer has been exhausted. The number of bytes contained in the returned buffer is implementation specific. The position of this buffer is unchanged after calling this method. The returned buffer's content should not be modified, but the position, limit, and mark may be changed. Operations for changing the position, limit, and mark of the returned buffer does not affect the position, limit, and mark of this buffer. Buffers returned by this method have independent position, limit and mark. This is an optional method, so callers should first checkReadableBuffer.byteBufferSupported().- Specified by:
getByteBufferin interfaceReadableBuffer- Overrides:
getByteBufferin classAbstractReadableBuffer
-
close
public void close()Description copied from interface:ReadableBufferCloses this buffer and releases any resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceReadableBuffer- Overrides:
closein classAbstractReadableBuffer
-
execute
private <T> int execute(CompositeReadableBuffer.ReadOperation<T> op, int length, T dest, int value) throws IOException Executes the givenCompositeReadableBuffer.ReadOperationagainst theReadableBuffers required to satisfy the requestedlength.- Throws:
IOException
-
executeNoThrow
private <T> int executeNoThrow(CompositeReadableBuffer.NoThrowReadOperation<T> op, int length, T dest, int value) -
advanceBufferIfNecessary
private void advanceBufferIfNecessary()If the current buffer is exhausted, removes and closes it. -
advanceBuffer
private void advanceBuffer()Removes one buffer from the front and closes it.
-