Buffer

class Buffer : BufferedSource, BufferedSink

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.

class Buffer : BufferedSource, BufferedSink, Cloneable, ByteChannel

Constructors

Buffer
Link copied to clipboard
common
fun Buffer()

Types

UnsafeCursor
Link copied to clipboard
common
class UnsafeCursor
A handle to the underlying data in a buffer.
class UnsafeCursor : Closeable

Functions

buffer
Link copied to clipboard
open override fun buffer(): Buffer
Returns this source's internal buffer.
clear
Link copied to clipboard
common
fun clear()
Discards all bytes in this buffer.
fun clear()
clone
Link copied to clipboard
open override fun clone(): Buffer
Returns a deep copy of this buffer.
close
Link copied to clipboard
open override fun close()
common
abstract override fun close()
Closes this source and releases the resources held by this source.
completeSegmentByteCount
Link copied to clipboard
common
fun completeSegmentByteCount(): Long
Returns the number of bytes in segments that are not writable.
fun completeSegmentByteCount(): Long
copy
Link copied to clipboard
common
fun copy(): Buffer
Returns a deep copy of this buffer.
fun copy(): Buffer
copyTo
Link copied to clipboard
common
fun copyTo(out: Buffer, offset: Long = 0L): Buffer
Overload of copyTo with byteCount = size - offset, work around for https://youtrack.jetbrains.
fun copyTo(out: Buffer, offset: Long = 0L): Buffer
fun copyTo(out: OutputStream, offset: Long = 0L, byteCount: Long = size - offset): Buffer
Copy byteCount bytes from this, starting at offset, to out.
common
fun copyTo(out: Buffer, offset: Long = 0L, byteCount: Long): Buffer
Copy byteCount bytes from this, starting at offset, to out.
fun copyTo(out: Buffer, offset: Long = 0L, byteCount: Long): Buffer
cursor
Link copied to clipboard
common
open fun cursor(): Cursor?
Returns a cursor to access the position and size of this source if it supports random access.
open fun cursor(): Cursor?
Returns a cursor to access the position and size of this source if it supports random access.
emit
Link copied to clipboard
common
open override fun emit(): Buffer
Writes all buffered data to the underlying sink, if one exists.
open override fun emit(): Buffer
emitCompleteSegments
Link copied to clipboard
common
open override fun emitCompleteSegments(): Buffer
Writes complete segments to the underlying sink, if one exists.
open override fun emitCompleteSegments(): Buffer
equals
Link copied to clipboard
open operator fun equals(other: Any?): Boolean
open operator override fun equals(other: Any?): Boolean
exhausted
Link copied to clipboard
open override fun exhausted(): Boolean
common
abstract fun exhausted(): Boolean
Returns true if there are no more bytes in this source.
flush
Link copied to clipboard
open override fun flush()
common
abstract override fun flush()
Writes all buffered data to the underlying sink, if one exists.
get
Link copied to clipboard
common
operator fun get(pos: Long): Byte
Returns the byte at pos.
@JvmName(name = "getByte")
operator fun get(pos: Long): Byte
hashCode
Link copied to clipboard
open fun hashCode(): Int
open override fun hashCode(): Int
hmacSha1
Link copied to clipboard
common
fun hmacSha1(key: ByteString): ByteString
Returns the 160-bit SHA-1 HMAC of this buffer.
fun hmacSha1(key: ByteString): ByteString
Returns the 160-bit SHA-1 HMAC of this buffer.
hmacSha256
Link copied to clipboard
common
fun hmacSha256(key: ByteString): ByteString
Returns the 256-bit SHA-256 HMAC of this buffer.
fun hmacSha256(key: ByteString): ByteString
Returns the 256-bit SHA-256 HMAC of this buffer.
hmacSha512
Link copied to clipboard
common
fun hmacSha512(key: ByteString): ByteString
Returns the 512-bit SHA-512 HMAC of this buffer.
fun hmacSha512(key: ByteString): ByteString
Returns the 512-bit SHA-512 HMAC of this buffer.
indexOf
Link copied to clipboard
open override fun indexOf(b: Byte): Long
open override fun indexOf(bytes: ByteString): Long
open override fun indexOf(bytes: ByteString, fromIndex: Long): Long
open override fun indexOf(b: Byte, fromIndex: Long, toIndex: Long): Long
common
abstract fun indexOf(b: Byte): Long
Equivalent to indexOf(b, 0).
common
abstract fun indexOf(bytes: ByteString): Long
Equivalent to indexOf(bytes, 0).
open override fun indexOf(b: Byte, fromIndex: Long): Long
Returns the index of b in this at or beyond fromIndex, or -1 if this buffer does not contain b in that range.
common
abstract fun indexOf(b: Byte, fromIndex: Long): Long
Returns the index of the first b in the buffer at or after fromIndex.
common
abstract fun indexOf(bytes: ByteString, fromIndex: Long): Long
Returns the index of the first match for bytes in the buffer at or after fromIndex.
common
abstract fun indexOf(b: Byte, fromIndex: Long, toIndex: Long): Long
Returns the index of b if it is found in the range of fromIndex inclusive to toIndex exclusive.
indexOfElement
Link copied to clipboard
open override fun indexOfElement(targetBytes: ByteString): Long
open override fun indexOfElement(targetBytes: ByteString, fromIndex: Long): Long
common
abstract fun indexOfElement(targetBytes: ByteString): Long
common
abstract fun indexOfElement(targetBytes: ByteString, fromIndex: Long): Long
Returns the first index in this buffer that is at or after fromIndex and that contains any of the bytes in targetBytes.
inputStream
Link copied to clipboard
open override fun inputStream(): InputStream
Returns an input stream that reads from this source.
isOpen
Link copied to clipboard
open override fun isOpen(): Boolean
md5
Link copied to clipboard
common
fun md5(): ByteString
fun md5(): ByteString
Returns the 128-bit MD5 hash of this buffer.
outputStream
Link copied to clipboard
open override fun outputStream(): OutputStream
Returns an output stream that writes to this sink.
peek
Link copied to clipboard
open override fun peek(): BufferedSource
common
abstract fun peek(): BufferedSource
Returns a new BufferedSource that can read data from this BufferedSource without consuming it.
rangeEquals
Link copied to clipboard
open override fun rangeEquals(offset: Long, bytes: ByteString): Boolean
open override fun rangeEquals(offset: Long, bytes: ByteString, bytesOffset: Int, byteCount: Int): Boolean
common
abstract fun rangeEquals(offset: Long, bytes: ByteString): Boolean
Returns true if the bytes at offset in this source equal bytes.
common
abstract fun rangeEquals(offset: Long, bytes: ByteString, bytesOffset: Int, byteCount: Int): Boolean
Returns true if byteCount bytes at offset in this source equal bytes at bytesOffset.
read
Link copied to clipboard
open override fun read(sink: ByteBuffer): Int
open override fun read(sink: ByteArray): Int
open override fun read(sink: ByteArray, offset: Int, byteCount: Int): Int
common
abstract fun read(sink: ByteArray): Int
Removes up to sink.length bytes from this and copies them into sink.
open override fun read(sink: Buffer, byteCount: Long): Long
Removes at least 1, and up to byteCount bytes from this and appends them to sink.
common
abstract fun read(sink: Buffer, byteCount: Long): Long
Removes at least 1, and up to byteCount bytes from this and appends them to sink.
common
abstract fun read(sink: ByteArray, offset: Int, byteCount: Int): Int
Removes up to byteCount bytes from this and copies them into sink at offset.
readAll
Link copied to clipboard
open override fun readAll(sink: Sink): Long
common
abstract fun readAll(sink: Sink): Long
Removes all bytes from this and appends them to sink.
readAndWriteUnsafe
Link copied to clipboard
fun readAndWriteUnsafe(unsafeCursor: Buffer.UnsafeCursor = UnsafeCursor()): Buffer.UnsafeCursor
fun readAndWriteUnsafe(unsafeCursor: Buffer.UnsafeCursor = UnsafeCursor()): Buffer.UnsafeCursor
readByte
Link copied to clipboard
open override fun readByte(): Byte
common
abstract fun readByte(): Byte
Removes a byte from this source and returns it.
readByteArray
Link copied to clipboard
open override fun readByteArray(): ByteArray
open override fun readByteArray(byteCount: Long): ByteArray
common
abstract fun readByteArray(): ByteArray
Removes all bytes from this and returns them as a byte array.
common
abstract fun readByteArray(byteCount: Long): ByteArray
Removes byteCount bytes from this and returns them as a byte array.
readByteString
Link copied to clipboard
open override fun readByteString(): ByteString
open override fun readByteString(byteCount: Long): ByteString
common
abstract fun readByteString(): ByteString
Removes all bytes from this and returns them as a byte string.
common
abstract fun readByteString(byteCount: Long): ByteString
Removes byteCount bytes from this and returns them as a byte string.
readDecimalLong
Link copied to clipboard
open override fun readDecimalLong(): Long
common
abstract fun readDecimalLong(): Long
Reads a long from this source in signed decimal form (i.e., as a string in base 10 with optional leading '-').
readFrom
Link copied to clipboard
fun readFrom(input: InputStream): Buffer
Read and exhaust bytes from input into this.
fun readFrom(input: InputStream, byteCount: Long): Buffer
Read byteCount bytes from input into this.
readFully
Link copied to clipboard
open override fun readFully(sink: ByteArray)
open override fun readFully(sink: Buffer, byteCount: Long)
common
abstract fun readFully(sink: ByteArray)
Removes exactly sink.length bytes from this and copies them into sink.
common
abstract fun readFully(sink: Buffer, byteCount: Long)
Removes exactly byteCount bytes from this and appends them to sink.
readHexadecimalUnsignedLong
Link copied to clipboard
open override fun readHexadecimalUnsignedLong(): Long
common
abstract fun readHexadecimalUnsignedLong(): Long
Reads a long form this source in hexadecimal form (i.e., as a string in base 16).
readInt
Link copied to clipboard
open override fun readInt(): Int
common
abstract fun readInt(): Int
Removes four bytes from this source and returns a big-endian int.
readIntLe
Link copied to clipboard
open override fun readIntLe(): Int
common
abstract fun readIntLe(): Int
Removes four bytes from this source and returns a little-endian int.
readLong
Link copied to clipboard
open override fun readLong(): Long
common
abstract fun readLong(): Long
Removes eight bytes from this source and returns a big-endian long.
readLongLe
Link copied to clipboard
open override fun readLongLe(): Long
common
abstract fun readLongLe(): Long
Removes eight bytes from this source and returns a little-endian long.
readShort
Link copied to clipboard
open override fun readShort(): Short
common
abstract fun readShort(): Short
Removes two bytes from this source and returns a big-endian short.
readShortLe
Link copied to clipboard
open override fun readShortLe(): Short
common
abstract fun readShortLe(): Short
Removes two bytes from this source and returns a little-endian short.
readString
Link copied to clipboard
open override fun readString(charset: Charset): String
Removes all bytes from this, decodes them as charset, and returns the string.
open override fun readString(byteCount: Long, charset: Charset): String
Removes byteCount bytes from this, decodes them as charset, and returns the string.
readUnsafe
Link copied to clipboard
fun readUnsafe(unsafeCursor: Buffer.UnsafeCursor = UnsafeCursor()): Buffer.UnsafeCursor
fun readUnsafe(unsafeCursor: Buffer.UnsafeCursor = UnsafeCursor()): Buffer.UnsafeCursor
readUtf8
Link copied to clipboard
open override fun readUtf8(): String
open override fun readUtf8(byteCount: Long): String
common
abstract fun readUtf8(): String
Removes all bytes from this, decodes them as UTF-8, and returns the string.
common
abstract fun readUtf8(byteCount: Long): String
Removes byteCount bytes from this, decodes them as UTF-8, and returns the string.
readUtf8CodePoint
Link copied to clipboard
open override fun readUtf8CodePoint(): Int
common
abstract fun readUtf8CodePoint(): Int
Removes and returns a single UTF-8 code point, reading between 1 and 4 bytes as necessary.
readUtf8Line
Link copied to clipboard
open override fun readUtf8Line(): String?
common
abstract fun readUtf8Line(): String?
Removes and returns characters up to but not including the next line break.
readUtf8LineStrict
Link copied to clipboard
open override fun readUtf8LineStrict(): String
open override fun readUtf8LineStrict(limit: Long): String
common
abstract fun readUtf8LineStrict(): String
Removes and returns characters up to but not including the next line break.
common
abstract fun readUtf8LineStrict(limit: Long): String
Like readUtf8LineStrict, except this allows the caller to specify the longest allowed match.
request
Link copied to clipboard
open override fun request(byteCount: Long): Boolean
common
abstract fun request(byteCount: Long): Boolean
Returns true when the buffer contains at least byteCount bytes, expanding it as necessary.
require
Link copied to clipboard
open override fun require(byteCount: Long)
common
abstract fun require(byteCount: Long)
Returns when the buffer contains at least byteCount bytes.
select
Link copied to clipboard
open override fun select(options: Options): Int
common
abstract fun select(options: Options): Int
Finds the first string in options that is a prefix of this buffer, consumes it from this buffer, and returns its index.
sha1
Link copied to clipboard
common
fun sha1(): ByteString
fun sha1(): ByteString
Returns the 160-bit SHA-1 hash of this buffer.
sha256
Link copied to clipboard
common
fun sha256(): ByteString
fun sha256(): ByteString
Returns the 256-bit SHA-256 hash of this buffer.
sha512
Link copied to clipboard
common
fun sha512(): ByteString
fun sha512(): ByteString
Returns the 512-bit SHA-512 hash of this buffer.
skip
Link copied to clipboard
common
open override fun skip(byteCount: Long)
Discards byteCount bytes from the head of this buffer.
open override fun skip(byteCount: Long)
snapshot
Link copied to clipboard
common
fun snapshot(): ByteString
Returns an immutable copy of this buffer as a byte string.
fun snapshot(): ByteString
common
fun snapshot(byteCount: Int): ByteString
Returns an immutable copy of the first byteCount bytes of this buffer as a byte string.
fun snapshot(byteCount: Int): ByteString
timeout
Link copied to clipboard
open override fun timeout(): Timeout
Returns the timeout for this source.
common
abstract fun timeout(): Timeout
Returns the timeout for this source.
toString
Link copied to clipboard
common
open fun toString(): String
open override fun toString(): String
Returns a human-readable string that describes the contents of this buffer.
write
Link copied to clipboard
open override fun write(source: ByteBuffer): Int
open override fun write(byteString: ByteString): Buffer
open override fun write(source: Buffer, byteCount: Long)
open override fun write(byteString: ByteString, offset: Int, byteCount: Int): Buffer
open override fun write(byteString: ByteString): Buffer
open override fun write(byteString: ByteString, offset: Int, byteCount: Int): Buffer
common
open override fun write(source: ByteArray): Buffer
Like OutputStream.
open override fun write(source: ByteArray): Buffer
common
open override fun write(source: Source, byteCount: Long): Buffer
Removes byteCount bytes from source and appends them to this sink.
open override fun write(source: Source, byteCount: Long): Buffer
common
abstract fun write(source: Buffer, byteCount: Long)
Removes byteCount bytes from source and appends them to this.
common
open override fun write(source: ByteArray, offset: Int, byteCount: Int): Buffer
Like OutputStream.
open override fun write(source: ByteArray, offset: Int, byteCount: Int): Buffer
writeAll
Link copied to clipboard
open override fun writeAll(source: Source): Long
common
abstract fun writeAll(source: Source): Long
Removes all bytes from source and appends them to this sink.
writeByte
Link copied to clipboard
common
open override fun writeByte(b: Int): Buffer
Writes a byte to this sink.
open override fun writeByte(b: Int): Buffer
writeDecimalLong
Link copied to clipboard
common
open override fun writeDecimalLong(v: Long): Buffer
Writes a long to this sink in signed decimal form (i.e., as a string in base 10).
open override fun writeDecimalLong(v: Long): Buffer
writeHexadecimalUnsignedLong
Link copied to clipboard
common
open override fun writeHexadecimalUnsignedLong(v: Long): Buffer
Writes a long to this sink in hexadecimal form (i.e., as a string in base 16).
open override fun writeHexadecimalUnsignedLong(v: Long): Buffer
writeInt
Link copied to clipboard
common
open override fun writeInt(i: Int): Buffer
Writes a big-endian int to this sink using four bytes.
open override fun writeInt(i: Int): Buffer
writeIntLe
Link copied to clipboard
common
open override fun writeIntLe(i: Int): Buffer
Writes a little-endian int to this sink using four bytes.
open override fun writeIntLe(i: Int): Buffer
writeLong
Link copied to clipboard
common
open override fun writeLong(v: Long): Buffer
Writes a big-endian long to this sink using eight bytes.
open override fun writeLong(v: Long): Buffer
writeLongLe
Link copied to clipboard
common
open override fun writeLongLe(v: Long): Buffer
Writes a little-endian long to this sink using eight bytes.
open override fun writeLongLe(v: Long): Buffer
writeShort
Link copied to clipboard
common
open override fun writeShort(s: Int): Buffer
Writes a big-endian short to this sink using two bytes.
open override fun writeShort(s: Int): Buffer
writeShortLe
Link copied to clipboard
common
open override fun writeShortLe(s: Int): Buffer
Writes a little-endian short to this sink using two bytes.
open override fun writeShortLe(s: Int): Buffer
writeString
Link copied to clipboard
open override fun writeString(string: String, charset: Charset): Buffer
open override fun writeString(string: String, beginIndex: Int, endIndex: Int, charset: Charset): Buffer
writeTo
Link copied to clipboard
fun writeTo(out: OutputStream, byteCount: Long = size): Buffer
Write byteCount bytes from this to out.
writeUtf8
Link copied to clipboard
common
open override fun writeUtf8(string: String): Buffer
Encodes string in UTF-8 and writes it to this sink.
open override fun writeUtf8(string: String): Buffer
common
open override fun writeUtf8(string: String, beginIndex: Int, endIndex: Int): Buffer
Encodes the characters at beginIndex up to endIndex from string in UTF-8 and writes it to this sink.
open override fun writeUtf8(string: String, beginIndex: Int, endIndex: Int): Buffer
writeUtf8CodePoint
Link copied to clipboard
common
open override fun writeUtf8CodePoint(codePoint: Int): Buffer
Encodes codePoint in UTF-8 and writes it to this sink.
open override fun writeUtf8CodePoint(codePoint: Int): Buffer

Properties

buffer
Link copied to clipboard
open override val buffer: Buffer
This source's internal buffer.
open override val buffer: Buffer
size
Link copied to clipboard
var size: Long = 0L