Package io.grpc.internal
Class CompositeReadableBuffer
- java.lang.Object
-
- io.grpc.internal.AbstractReadableBuffer
-
- io.grpc.internal.CompositeReadableBuffer
-
- All Implemented Interfaces:
ReadableBuffer
,java.io.Closeable
,java.lang.AutoCloseable
public class CompositeReadableBuffer extends AbstractReadableBuffer
AReadableBuffer
that is composed of 0 or moreReadableBuffer
s. 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 Classes Modifier and Type Class Description private static interface
CompositeReadableBuffer.NoThrowReadOperation<T>
private static interface
CompositeReadableBuffer.ReadOperation<T>
A simple read operation to perform on a singleReadableBuffer
.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<ReadableBuffer>
buffers
private static CompositeReadableBuffer.NoThrowReadOperation<byte[]>
BYTE_ARRAY_OP
private static CompositeReadableBuffer.NoThrowReadOperation<java.nio.ByteBuffer>
BYTE_BUF_OP
private boolean
marked
private java.util.Deque<ReadableBuffer>
readableBuffers
private int
readableBytes
private java.util.Deque<ReadableBuffer>
rewindableBuffers
private static CompositeReadableBuffer.NoThrowReadOperation<java.lang.Void>
SKIP_OP
private static CompositeReadableBuffer.ReadOperation<java.io.OutputStream>
STREAM_OP
private static CompositeReadableBuffer.NoThrowReadOperation<java.lang.Void>
UBYTE_OP
-
Constructor Summary
Constructors Constructor Description CompositeReadableBuffer()
CompositeReadableBuffer(int initialCapacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBuffer(ReadableBuffer buffer)
Adds a newReadableBuffer
at the end of the buffer list.private void
advanceBuffer()
Removes one buffer from the front and closes it.private void
advanceBufferIfNecessary()
If the current buffer is exhausted, removes and closes it.boolean
byteBufferSupported()
Indicates whether or notReadableBuffer.getByteBuffer()
operation is supported for this buffer.void
close()
Closes this buffer and releases any resources.private void
enqueueBuffer(ReadableBuffer buffer)
private <T> int
execute(CompositeReadableBuffer.ReadOperation<T> op, int length, T dest, int value)
Executes the givenCompositeReadableBuffer.ReadOperation
against theReadableBuffer
s required to satisfy the requestedlength
.private <T> int
executeNoThrow(CompositeReadableBuffer.NoThrowReadOperation<T> op, int length, T dest, int value)
java.nio.ByteBuffer
getByteBuffer()
Gets aByteBuffer
that contains some bytes of the content next to be read, ornull
if this buffer has been exhausted.void
mark()
Marks the current position in this buffer.boolean
markSupported()
Indicates whether or notReadableBuffer.mark()
operation is supported for this buffer.int
readableBytes()
Gets the current number of readable bytes remaining in this buffer.void
readBytes(byte[] dest, int destOffset, int length)
Readslength
bytes from this buffer and writes them to the destination array.ReadableBuffer
readBytes(int length)
Readslength
bytes from this buffer and returns a new Buffer containing them.void
readBytes(CompositeReadableBuffer dest, int length)
Readslength
bytes from this buffer and writes them to the destination buffer.void
readBytes(java.io.OutputStream dest, int length)
Readslength
bytes from this buffer and writes them to the destination stream.void
readBytes(java.nio.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.int
readUnsignedByte()
Reads the next unsigned byte from this buffer and increments the read position by 1.void
reset()
Repositions this buffer to the position at the timeReadableBuffer.mark()
was last called on this buffer.void
skipBytes(int length)
Increments the read position by the given length.-
Methods inherited from class io.grpc.internal.AbstractReadableBuffer
array, arrayOffset, checkReadable, hasArray, readInt
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.grpc.internal.ReadableBuffer
touch
-
-
-
-
Field Detail
-
readableBuffers
private final java.util.Deque<ReadableBuffer> readableBuffers
-
rewindableBuffers
private java.util.Deque<ReadableBuffer> rewindableBuffers
-
readableBytes
private int readableBytes
-
buffers
private final java.util.Queue<ReadableBuffer> buffers
-
marked
private boolean marked
-
UBYTE_OP
private static final CompositeReadableBuffer.NoThrowReadOperation<java.lang.Void> UBYTE_OP
-
SKIP_OP
private static final CompositeReadableBuffer.NoThrowReadOperation<java.lang.Void> SKIP_OP
-
BYTE_ARRAY_OP
private static final CompositeReadableBuffer.NoThrowReadOperation<byte[]> BYTE_ARRAY_OP
-
BYTE_BUF_OP
private static final CompositeReadableBuffer.NoThrowReadOperation<java.nio.ByteBuffer> BYTE_BUF_OP
-
STREAM_OP
private static final CompositeReadableBuffer.ReadOperation<java.io.OutputStream> STREAM_OP
-
-
Method Detail
-
addBuffer
public void addBuffer(ReadableBuffer buffer)
Adds a newReadableBuffer
at the end of the buffer list. After a buffer is added, it is expected that thisCompositeBuffer
has 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
private void enqueueBuffer(ReadableBuffer buffer)
-
readableBytes
public int readableBytes()
Description copied from interface:ReadableBuffer
Gets the current number of readable bytes remaining in this buffer.
-
readUnsignedByte
public int readUnsignedByte()
Description copied from interface:ReadableBuffer
Reads the next unsigned byte from this buffer and increments the read position by 1.
-
skipBytes
public void skipBytes(int length)
Description copied from interface:ReadableBuffer
Increments the read position by the given length.
-
readBytes
public void readBytes(byte[] dest, int destOffset, int length)
Description copied from interface:ReadableBuffer
Readslength
bytes 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
public void readBytes(java.nio.ByteBuffer dest)
Description copied from interface:ReadableBuffer
Reads 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
public void readBytes(java.io.OutputStream dest, int length) throws java.io.IOException
Description copied from interface:ReadableBuffer
Readslength
bytes 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:
java.io.IOException
- thrown if any error was encountered while writing to the stream.
-
readBytes
public void readBytes(CompositeReadableBuffer dest, int length)
Readslength
bytes 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:
java.lang.IndexOutOfBoundsException
- if required bytes are not readable
-
readBytes
public ReadableBuffer readBytes(int length)
Description copied from interface:ReadableBuffer
Readslength
bytes 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:ReadableBuffer
Indicates whether or notReadableBuffer.mark()
operation is supported for this buffer.- Specified by:
markSupported
in interfaceReadableBuffer
- Overrides:
markSupported
in classAbstractReadableBuffer
-
mark
public void mark()
Description copied from interface:ReadableBuffer
Marks 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:
mark
in interfaceReadableBuffer
- Overrides:
mark
in classAbstractReadableBuffer
-
reset
public void reset()
Description copied from interface:ReadableBuffer
Repositions this buffer to the position at the timeReadableBuffer.mark()
was last called on this buffer.- Specified by:
reset
in interfaceReadableBuffer
- Overrides:
reset
in classAbstractReadableBuffer
-
byteBufferSupported
public boolean byteBufferSupported()
Description copied from interface:ReadableBuffer
Indicates whether or notReadableBuffer.getByteBuffer()
operation is supported for this buffer.- Specified by:
byteBufferSupported
in interfaceReadableBuffer
- Overrides:
byteBufferSupported
in classAbstractReadableBuffer
-
getByteBuffer
@Nullable public java.nio.ByteBuffer getByteBuffer()
Description copied from interface:ReadableBuffer
Gets aByteBuffer
that contains some bytes of the content next to be read, ornull
if 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:
getByteBuffer
in interfaceReadableBuffer
- Overrides:
getByteBuffer
in classAbstractReadableBuffer
-
close
public void close()
Description copied from interface:ReadableBuffer
Closes this buffer and releases any resources.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceReadableBuffer
- Overrides:
close
in classAbstractReadableBuffer
-
execute
private <T> int execute(CompositeReadableBuffer.ReadOperation<T> op, int length, T dest, int value) throws java.io.IOException
Executes the givenCompositeReadableBuffer.ReadOperation
against theReadableBuffer
s required to satisfy the requestedlength
.- Throws:
java.io.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.
-
-