Package io.grpc.internal
Interface ReadableBuffer
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable
- All Known Implementing Classes:
AbstractReadableBuffer,CompositeReadableBuffer,ForwardingReadableBuffer,NettyReadableBuffer,ReadableBuffers.ByteArrayWrapper,ReadableBuffers.ByteReadableBufferWrapper
public interface ReadableBuffer extends java.io.CloseableInterface for an abstract byte buffer. Buffers are intended to be a read-only, except for the read position which is incremented after each read call.Buffers may optionally expose a backing array for optimization purposes, similar to what is done in
ByteBuffer. It is not expected that callers will attempt to modify the backing array.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description byte[]array()Gets the backing array for this buffer.intarrayOffset()Gets the offset in the backing array of the current read position.booleanbyteBufferSupported()Indicates whether or notgetByteBuffer()operation is supported for this buffer.voidclose()Closes this buffer and releases any resources.java.nio.ByteBuffergetByteBuffer()Gets aByteBufferthat contains some bytes of the content next to be read, ornullif this buffer has been exhausted.booleanhasArray()Indicates whether or not this buffer exposes a backing array.voidmark()Marks the current position in this buffer.booleanmarkSupported()Indicates whether or notmark()operation is supported for this buffer.intreadableBytes()Gets 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.ReadableBufferreadBytes(int length)Readslengthbytes from this buffer and returns a new Buffer containing them.voidreadBytes(java.io.OutputStream dest, int length)Readslengthbytes from this buffer and writes them to the destination stream.voidreadBytes(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.intreadInt()Reads a 4-byte signed integer from this buffer using big-endian byte ordering.intreadUnsignedByte()Reads the next unsigned byte from this buffer and increments the read position by 1.voidreset()Repositions this buffer to the position at the timemark()was last called on this buffer.voidskipBytes(int length)Increments the read position by the given length.default voidtouch()Note that the current callsite has access to this buffer, or do nothing.
-
-
-
Method Detail
-
readableBytes
int readableBytes()
Gets the current number of readable bytes remaining in this buffer.
-
readUnsignedByte
int readUnsignedByte()
Reads the next unsigned byte from this buffer and increments the read position by 1.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
readInt
int readInt()
Reads a 4-byte signed integer from this buffer using big-endian byte ordering. Increments the read position by 4.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
skipBytes
void skipBytes(int length)
Increments the read position by the given length.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
readBytes
void readBytes(byte[] dest, int destOffset, int length)Readslengthbytes 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.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable ordestis too small.
-
readBytes
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.- Parameters:
dest- the destination buffer to receive the bytes.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
readBytes
void readBytes(java.io.OutputStream dest, int length) throws java.io.IOExceptionReadslengthbytes 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.java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
readBytes
ReadableBuffer readBytes(int length)
Readslengthbytes 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.- Throws:
java.lang.IndexOutOfBoundsException- if required bytes are not readable
-
hasArray
boolean hasArray()
Indicates whether or not this buffer exposes a backing array.
-
array
byte[] array()
Gets the backing array for this buffer. This is an optional method, so callers should first checkhasArray().- Throws:
java.lang.UnsupportedOperationException- the buffer does not support this method
-
arrayOffset
int arrayOffset()
Gets the offset in the backing array of the current read position. This is an optional method, so callers should first checkhasArray()- Throws:
java.lang.UnsupportedOperationException- the buffer does not support this method
-
touch
default void touch()
Note that the current callsite has access to this buffer, or do nothing. This is only useful when the buffer has leak detection and intrumentation to record usages before the buffer was leaked. That can make it much easier to track down where the buffer was leaked. If this isn't such a buffer, the method does nothing.
-
markSupported
boolean markSupported()
Indicates whether or notmark()operation is supported for this buffer.
-
mark
void mark()
Marks the current position in this buffer. A subsequent call to thereset()method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.
-
reset
void reset()
Repositions this buffer to the position at the timemark()was last called on this buffer.
-
byteBufferSupported
boolean byteBufferSupported()
Indicates whether or notgetByteBuffer()operation is supported for this buffer.
-
getByteBuffer
@Nullable java.nio.ByteBuffer getByteBuffer()
Gets 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 checkbyteBufferSupported().- Throws:
java.lang.UnsupportedOperationException- the buffer does not support this method.
-
close
void close()
Closes this buffer and releases any resources.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
-