Buffer
A collection of bytes in memory.
Moving data from one buffer to another is fast. Instead of copying bytes from one place in memory to another, this class just changes ownership of the underlying byte arrays.
This buffer grows with your data. Just like ArrayList, each buffer starts small. It consumes only the memory it needs to.
This buffer pools its byte arrays. When you allocate a byte array in Java, the runtime must zero-fill the requested array before returning it to you. Even if you're going to write over that space anyway. This class avoids zero-fill and GC churn by pooling byte arrays.
Types
Functions
Returns a new sink that buffers writes to sink
. The returned sink will batch writes to sink
. Use this wherever you write to a sink to get an ergonomic and efficient access to data.
Returns a new source that buffers reads from source
. The returned source will perform bulk reads into its in-memory buffer. Use this wherever you read a source to get an ergonomic and efficient access to data.
Returns a sink that uses cipher to encrypt or decrypt this.
Returns a source that uses cipher to encrypt or decrypt this.
Closes this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once.
Closes this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once.
Closes this source and releases the resources held by this source. It is an error to read a closed source. It is safe to close a source more than once.
Returns the number of bytes in segments that are not writable. This is the number of bytes that can be flushed immediately to an underlying sink without harming throughput.
Overload of copyTo with byteCount = size - offset, work around for https://youtrack.jetbrains.com/issue/KT-30847
Copy byteCount
bytes from this, starting at offset
, to out
.
Copy byteCount
bytes from this, starting at offset
, to out
.
Returns an DeflaterSink that DEFLATE-compresses data to this Sink while writing.
Writes all buffered data to the underlying sink, if one exists. Like flush, but weaker. Call this before this buffered sink goes out of scope so that its data can reach its destination.
Writes complete segments to the underlying sink, if one exists. Like flush, but weaker. Use this to limit the memory held in the buffer to a single segment. Typically application code will not need to call this: it is only necessary when application code writes directly to this sink's buffer.
Writes all buffered data to the underlying sink, if one exists. Then that sink is recursively flushed which pushes data as far as possible towards its ultimate destination. Typically that destination is a network socket or file.
Returns the 160-bit SHA-1 HMAC of this buffer.
Returns the 160-bit SHA-1 HMAC of this buffer.
Returns the 160-bit SHA-1 HMAC of this buffer.
Returns the 256-bit SHA-256 HMAC of this buffer.
Returns the 256-bit SHA-256 HMAC of this buffer.
Returns the 256-bit SHA-256 HMAC of this buffer.
Returns the 512-bit SHA-512 HMAC of this buffer.
Returns the 512-bit SHA-512 HMAC of this buffer.
Returns the 512-bit SHA-512 HMAC of this buffer.
Equivalent to indexOf(b, 0).
Equivalent to indexOf(bytes, 0).
Returns the index of the first b
in the buffer at or after fromIndex
. This expands the buffer as necessary until b
is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.
Returns the index of the first match for bytes
in the buffer at or after fromIndex
. This expands the buffer as necessary until bytes
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 the index of b
if it is found in the range of fromIndex
inclusive to toIndex
exclusive. If b
isn't found, or if fromIndex == toIndex
, then -1 is returned.
Returns the index of b
in this at or beyond fromIndex
, or -1 if this buffer does not contain b
in that range.
Equivalent to indexOfElement(targetBytes, 0).
Returns the first index in this buffer that is at or after fromIndex
and that contains any of the bytes in targetBytes
. This expands the buffer as necessary until a target byte is found. This reads an unbounded number of bytes into the buffer. Returns -1 if the stream is exhausted before the requested byte is found.
Returns an InflaterSource that DEFLATE-decompresses this Source while reading.
Returns an input stream that reads from this source.
Returns the 128-bit MD5 hash of this buffer.
Returns an output stream that writes to this sink.
Returns a new BufferedSource
that can read data from this BufferedSource
without consuming it. The returned source becomes invalid once this source is next read or closed.
Returns true if the bytes at offset
in this source equal bytes
. This expands the buffer as necessary until a byte does not match, all bytes are matched, or if the stream is exhausted before enough bytes could determine a match.
Returns true if byteCount
bytes at offset
in this source equal bytes
at bytesOffset
. This expands the buffer as necessary until a byte does not match, all bytes are matched, or if the stream is exhausted before enough bytes could determine a match.
Removes up to sink.length
bytes from this and copies them into sink
. Returns the number of bytes read, or -1 if this source is exhausted.
Removes up to byteCount
bytes from this and copies them into sink
at offset
. Returns the number of bytes read, or -1 if this source is exhausted.
Removes at least 1, and up to byteCount
bytes from this and appends them to sink
. Returns the number of bytes read, or -1 if this source is exhausted.
Removes at least 1, and up to byteCount
bytes from this and appends them to sink
. Returns the number of bytes read, or -1 if this source is exhausted.
Removes at least 1, and up to byteCount
bytes from this and appends them to sink
. Returns the number of bytes read, or -1 if this source is exhausted.
Removes all bytes from this and returns them as a byte array.
Removes byteCount
bytes from this and returns them as a byte array.
Removes all bytes from this and returns them as a byte string.
Removes byteCount
bytes from this and returns them as a byte string.
Reads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-'). This will iterate until a non-digit character is found.
Read and exhaust bytes from input
into this.
Read byteCount
bytes from input
into this.
Removes exactly sink.length
bytes from this and copies them into sink
. Throws an java.io.EOFException if the requested number of bytes cannot be read.
Removes exactly byteCount
bytes from this and appends them to sink
. Throws an java.io.EOFException if the requested number of bytes cannot be read.
Reads a long form this source in hexadecimal form (i.e., as a string in base 16). This will iterate until a non-hexadecimal character is found.
Removes eight bytes from this source and returns a little-endian long.
Removes two bytes from this source and returns a little-endian short.
Removes all bytes from this, decodes them as charset
, and returns the string.
Removes byteCount
bytes from this, decodes them as charset
, and returns the string.
Removes all bytes from this, decodes them as UTF-8, and returns the string. Returns the empty string if this source is empty.
Removes byteCount
bytes from this, decodes them as UTF-8, and returns the string.
Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
Removes and returns characters up to but not including the next line break. A line break is either "\n"
or "\r\n"
; these characters are not included in the result.
Removes and returns characters up to but not including the next line break. A line break is either "\n"
or "\r\n"
; these characters are not included in the result.
Like readUtf8LineStrict, except this allows the caller to specify the longest allowed match. Use this to protect against streams that may not include "\n"
or "\r\n"
.
Returns true when the buffer contains at least byteCount
bytes, expanding it as necessary. Returns false if the source is exhausted before the requested bytes can be read.
Finds the first byte string in options
that is a prefix of this buffer, consumes it from this source, and returns its index. If no byte string in options
is a prefix of this buffer this returns -1 and no bytes are consumed.
Returns the 160-bit SHA-1 hash of this buffer.
Returns the 256-bit SHA-256 hash of this buffer.
Returns the 512-bit SHA-512 hash of this buffer.
Returns an immutable copy of this buffer as a byte string.
Returns an immutable copy of the first byteCount
bytes of this buffer as a byte string.
Returns a human-readable string that describes the contents of this buffer. Typically this is a string like [text=Hello]
or [hex=0000ffff]
.
Like OutputStream.write, this writes a complete byte array to this sink.
Removes byteCount
bytes from source
and appends them to this sink.
Like OutputStream.write, this writes byteCount
bytes of source
, starting at offset
.
Removes byteCount
bytes from source
and appends them to this.
Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
Writes a little-endian int to this sink using four bytes.
Writes a little-endian long to this sink using eight bytes.
Writes a big-endian short to this sink using two bytes.
Writes a little-endian short to this sink using two bytes.
Write byteCount
bytes from this to out
.
Encodes string
in UTF-8 and writes it to this sink.
Encodes the characters at beginIndex
up to endIndex
from string
in UTF-8 and writes it to this sink.
Encodes codePoint
in UTF-8 and writes it to this sink.