Buffer
A collection of bytes in memory.
The buffer can be viewed as an unbound queue whose size grows with the data being written and shrinks with data being consumed. Internally, the buffer consists of data segments, and the buffer's capacity grows and shrinks in units of data segments instead of individual bytes.
The buffer was designed to reduce memory allocations when possible. Instead of copying bytes from one place in memory to another, this class just changes ownership of the underlying data segments.
To reduce allocations and speed up the buffer's extension, it may use data segments pooling.
Buffer implements both Source and Sink and could be used as a source or a sink, but unlike regular sinks and sources its close, flush, emit, hintEmit does not affect buffer's state and exhausted only indicates that a buffer is empty.
Properties
Functions
Returns a new ByteChannel instance representing this buffer.
Returns WritableByteChannel backed by this sink. Closing the channel will also close the sink.
Returns ReadableByteChannel backed by this source. Closing the source will close the source.
Returns an input stream that reads from this source. Closing the stream will also close this source.
Returns an input stream that reads from this source. Closing the stream will also close this source.
Returns an output stream that writes to this sink. Closing the stream will also close this sink.
Returns an output stream that writes to this sink. Closing the stream will also close this sink.
Returns a new sink that buffers writes to the sink. The returned sink will batch writes to the sink. Use this wherever you write to a sink to get ergonomic and efficient access to data.
Returns a new source that buffers reads from the source. The returned source will perform bulk reads into its in-memory buffer. Use this wherever you read a source to get ergonomic and efficient access to data.
Copies bytes from this buffer's subrange, starting at startIndex and ending at endIndex, to out. This method does not consume data from the buffer.
This method does not affect the buffer's content as there is no upstream to write data to.
Returns the index of the first match for byteString in the source at or after startIndex. This expands the source's buffer as necessary until byteString is found. This reads an unbounded number of bytes into the buffer. Returns -1
if the stream is exhausted before the requested bytes are found.
Returns an index of byte first occurrence in the range of startIndex to endIndex, or -1
when the range doesn't contain byte.
Removes at least 1, and up to byteCount bytes from this source and appends them to sink. Returns the number of bytes read, or -1 if this source is exhausted.
Removes up to endIndex - startIndex
bytes from this source, copies them into sink subrange starting at startIndex and ending at endIndex, and returns the number of bytes read, or -1 if this source is exhausted.
Writes up to ByteBuffer.remaining bytes from this buffer to the sink. Return the number of bytes written.
Reads at most ByteBuffer.remaining bytes from this source into sink and returns the number of bytes read.
Removes all bytes from this source and returns them as a byte array.
Removes byteCount bytes from this source and returns them as a byte array.
Consumes all bytes from this source and wraps it into a byte string.
Consumes exactly byteCount bytes from this source and wraps it into a byte string.
Decodes a single code point value from UTF-8 code units, reading between 1 and 4 bytes as necessary.
Reads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading -
).
Removes eight bytes from this source and returns a floating point number with type Double composed of it according to the big-endian order.
Removes eight bytes from this source and returns a floating point number with type Double composed of it according to the little-endian order.
Removes four bytes from this source and returns a floating point number with type Float composed of it according to the little-endian order.
Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
Removes and returns UTF-8 encoded characters up to but not including the next line break, throwing EOFException if a line break was not encountered. A line break is either "\n"
or "\r\n"
; these characters are not included in the result.
Removes eight bytes from this source and returns a long integer composed of it according to the little-endian order.
Removes two bytes from this source and returns a short integer composed of it according to the little-endian order.
Removes all bytes from this buffer, decodes them as UTF-8, and returns the string.
Removes all bytes from this source, decodes them as UTF-8, and returns the string.
Removes byteCount bytes from this source, decodes them as UTF-8, and returns the string.
Removes exactly endIndex - startIndex
bytes from this source and copies them into sink subrange starting at startIndex and ending at endIndex.
Removes four bytes from this source and returns an unsigned integer composed of it according to the little-endian order.
Removes eight bytes from this source and returns an unsigned long integer composed of it according to the little-endian order.
Removes two bytes from this source and returns an unsigned short integer composed of it according to the big-endian order.
Removes two bytes from this source and returns an unsigned short integer composed of it according to the little-endian order.
Attempts to fill the buffer with at least byteCount bytes of data from the underlying source and throw EOFException when the source is exhausted before fulfilling the requirement.
Creates a byte string containing a copy of all the data from this buffer.
Return true
if the next byte to be consumed from this source is equal to byte. Otherwise, return false
as well as when the source is exhausted.
Returns a human-readable string that describes the contents of this buffer. For buffers containing few bytes, this is a string like Buffer(size=4 hex=0000ffff)
. However, if the buffer is too large, a string will contain its size and only a prefix of data, like Buffer(size=1024 hex=01234…)
. Thus, the string could not be used to compare buffers or verify buffer's content.
Reads all data from source into this buffer.
Writes bytes from source array or its subrange to this sink.
Writes subsequence of data from byteString starting at startIndex and ending at endIndex into a sink.
Writes data from the source into this sink and returns the number of bytes written.
Encodes codePoint in UTF-8 and writes it to this sink.
Writes long to this sink in signed decimal form (i.e., as a string in base 10).
Writes long to this sink in hexadecimal form (i.e., as a string in base 16).
Writes four bytes containing int, in the little-endian order, to this sink.
Writes eight bytes containing long, in the little-endian order, to this sink.
Writes two bytes containing short, in the big-endian order, to this sink.
Writes two bytes containing short, in the little-endian order, to this sink.
Encodes the characters at startIndex up to endIndex from chars in UTF-8 and writes it to this sink.
Encodes the characters at startIndex up to endIndex from string in UTF-8 and writes it to this sink.
Encodes substring of string starting at startIndex and ending at endIndex using charset and writes into this sink.
Provides direct access to the sink's internal buffer and hints its emit before exit.
Writes am unsigned byte to this sink.
Writes four bytes containing int, in the little-endian order, to this sink.
Writes eight bytes containing long, in the big-endian order, to this sink.
Writes eight bytes containing long, in the little-endian order, to this sink.
Writes two bytes containing short, in the big-endian order, to this sink.
Writes two bytes containing short, in the little-endian order, to this sink.