Skip to content

okio / okio / Buffer

Buffer

(js, jvm, native) 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.

Types

Name Summary
(jvm) UnsafeCursor A handle to the underlying data in a buffer. This handle is unsafe because it does not enforce its own invariants. Instead, it assumes a careful user who has studied Okio’s implementation details and their consequences.class UnsafeCursor :Closeable

Constructors

Name Summary
(js, jvm, native) <init> A collection of bytes in memory.<init>()

Properties

Name Summary
(js, jvm, native) buffer val buffer:Buffer
(js, jvm, native) size var size:Long

Functions

Name Summary
(jvm) buffer Returns this source’s internal buffer.fun buffer():Buffer
(js, jvm, native) clear Discards all bytes in this buffer. Calling this method when you’re done with a buffer will return its segments to the pool.fun clear():Unit
(jvm) clone Returns a deep copy of this buffer.fun clone():Buffer
(js, jvm, native) close fun close():Unit
(js, jvm, native) completeSegmentByteCount 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.fun completeSegmentByteCount():Long
(js, jvm, native) copy Returns a deep copy of this buffer.fun copy():Buffer
copyTo Copy byteCount bytes from this, starting at offset, to out.(jvm) fun copyTo(out:OutputStream, offset:Long= 0L, byteCount:Long= size - offset):Buffer
(js, jvm) fun copyTo(out:Buffer, offset:Long= 0L, byteCount:Long):Buffer
(native) fun copyTo(out:Buffer, offset:Long, byteCount:Long):Buffer
Overload of copyTo with byteCount = size - offset, work around for https://youtrack.jetbrains.com/issue/KT-30847(js, jvm) fun copyTo(out:Buffer, offset:Long= 0L):Buffer
(native) fun copyTo(out:Buffer, offset:Long):Buffer
(js, jvm, native) emit fun emit():Buffer
(js, jvm, native) emitCompleteSegments fun emitCompleteSegments():Buffer
(js, jvm, native) equals fun equals(other:Any?):Boolean
(js, jvm, native) exhausted fun exhausted():Boolean
(js, jvm, native) flush fun flush():Unit
(js, jvm, native) get Returns the byte at pos.operator fun get(pos:Long):Byte
(jvm) getByte fun ~~getByte~~(index:Long):Byte
(js, jvm, native) hashCode fun hashCode():Int
(jvm) hmacSha1 Returns the 160-bit SHA-1 HMAC of this buffer.fun hmacSha1(key:ByteString):ByteString
(jvm) hmacSha256 Returns the 256-bit SHA-256 HMAC of this buffer.fun hmacSha256(key:ByteString):ByteString
(jvm) hmacSha512 Returns the 512-bit SHA-512 HMAC of this buffer.fun hmacSha512(key:ByteString):ByteString
(js, jvm, native) indexOf fun indexOf(b:Byte):Long
fun indexOf(b:Byte, fromIndex:Long, toIndex:Long):Long
fun indexOf(bytes:ByteString):Long
fun indexOf(bytes:ByteString, 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.fun indexOf(b:Byte, fromIndex:Long):Long
(js, jvm, native) indexOfElement fun indexOfElement(targetBytes:ByteString):Long
fun indexOfElement(targetBytes:ByteString, fromIndex:Long):Long
(jvm) inputStream Returns an input stream that reads from this source.fun inputStream():InputStream
(jvm) isOpen fun isOpen():Boolean
(jvm) md5 Returns the 128-bit MD5 hash of this buffer.fun md5():ByteString
(jvm) outputStream Returns an output stream that writes to this sink.fun outputStream():OutputStream
(js, jvm, native) peek fun peek():BufferedSource
(js, jvm, native) rangeEquals fun rangeEquals(offset:Long, bytes:ByteString):Boolean
fun rangeEquals(offset:Long, bytes:ByteString, bytesOffset:Int, byteCount:Int):Boolean
read (jvm) fun read(sink:ByteBuffer):Int
(js, jvm, native) fun read(sink:ByteArray):Int
(js, jvm, native) fun read(sink:ByteArray, offset:Int, byteCount:Int):Int
(js, jvm, native) fun read(sink:Buffer, byteCount:Long):Long
(js, jvm, native) readAll fun readAll(sink:Sink):Long
(jvm) readAndWriteUnsafe fun readAndWriteUnsafe(unsafeCursor:Buffer.UnsafeCursor= UnsafeCursor()):Buffer.UnsafeCursor
(js, jvm, native) readByte fun readByte():Byte
(js, jvm, native) readByteArray fun readByteArray():ByteArray
fun readByteArray(byteCount:Long):ByteArray
(js, jvm, native) readByteString fun readByteString():ByteString
fun readByteString(byteCount:Long):ByteString
(js, jvm, native) readDecimalLong fun readDecimalLong():Long
(jvm) readFrom Read and exhaust bytes from input into this.fun readFrom(input:InputStream):Buffer
Read byteCount bytes from input into this.fun readFrom(input:InputStream, byteCount:Long):Buffer
(js, jvm, native) readFully fun readFully(sink:Buffer, byteCount:Long):Unit
fun readFully(sink:ByteArray):Unit
(js, jvm, native) readHexadecimalUnsignedLong fun readHexadecimalUnsignedLong():Long
(js, jvm, native) readInt fun readInt():Int
(js, jvm, native) readIntLe fun readIntLe():Int
(js, jvm, native) readLong fun readLong():Long
(js, jvm, native) readLongLe fun readLongLe():Long
(js, jvm, native) readShort fun readShort():Short
(js, jvm, native) readShortLe fun readShortLe():Short
(jvm) readString Removes all bytes from this, decodes them as charset, and returns the string.fun readString(charset:Charset):String
Removes byteCount bytes from this, decodes them as charset, and returns the string.fun readString(byteCount:Long, charset:Charset):String
(jvm) readUnsafe fun readUnsafe(unsafeCursor:Buffer.UnsafeCursor= UnsafeCursor()):Buffer.UnsafeCursor
(js, jvm, native) readUtf8 fun readUtf8():String
fun readUtf8(byteCount:Long):String
(js, jvm, native) readUtf8CodePoint fun readUtf8CodePoint():Int
(js, jvm, native) readUtf8Line fun readUtf8Line():String?
(js, jvm, native) readUtf8LineStrict fun readUtf8LineStrict():String
fun readUtf8LineStrict(limit:Long):String
(js, jvm, native) request fun request(byteCount:Long):Boolean
(js, jvm, native) require fun require(byteCount:Long):Unit
select (native) fun select(options: <ERROR CLASS>):Int
(js, jvm) fun select(options:Options):Int
(jvm) sha1 Returns the 160-bit SHA-1 hash of this buffer.fun sha1():ByteString
(jvm) sha256 Returns the 256-bit SHA-256 hash of this buffer.fun sha256():ByteString
(jvm) sha512 Returns the 512-bit SHA-512 hash of this buffer.fun sha512():ByteString
(jvm) size fun ~~size~~():Long
(js, jvm, native) skip Discards byteCount bytes from the head of this buffer.fun skip(byteCount:Long):Unit
(js, jvm, native) snapshot Returns an immutable copy of this buffer as a byte string.fun snapshot():ByteString
Returns an immutable copy of the first byteCount bytes of this buffer as a byte string.fun snapshot(byteCount:Int):ByteString
(js, jvm, native) timeout fun timeout():Timeout
toString Returns a human-readable string that describes the contents of this buffer. Typically this is a string like [text=Hello] or [hex=0000ffff].(js, jvm) fun toString():String
(native) fun toString(): <ERROR CLASS>
write (jvm) fun write(source:ByteBuffer):Int
(js, jvm, native) fun write(byteString:ByteString):Buffer
(js, jvm) fun write(byteString:ByteString, offset:Int, byteCount:Int):Buffer
(native) fun write(byteString:ByteString, offset:Int, byteCount:Int): <ERROR CLASS>
(js, jvm, native) fun write(source:ByteArray):Buffer
(js, jvm, native) fun write(source:ByteArray, offset:Int, byteCount:Int):Buffer
(js, jvm, native) fun write(source:Source, byteCount:Long):Buffer
(js, jvm, native) fun write(source:Buffer, byteCount:Long):Unit
(js, jvm, native) writeAll fun writeAll(source:Source):Long
(js, jvm, native) writeByte fun writeByte(b:Int):Buffer
(js, jvm, native) writeDecimalLong fun writeDecimalLong(v:Long):Buffer
(js, jvm, native) writeHexadecimalUnsignedLong fun writeHexadecimalUnsignedLong(v:Long):Buffer
(js, jvm, native) writeInt fun writeInt(i:Int):Buffer
(js, jvm, native) writeIntLe fun writeIntLe(i:Int):Buffer
(js, jvm, native) writeLong fun writeLong(v:Long):Buffer
(js, jvm, native) writeLongLe fun writeLongLe(v:Long):Buffer
(js, jvm, native) writeShort fun writeShort(s:Int):Buffer
(js, jvm, native) writeShortLe fun writeShortLe(s:Int):Buffer
(jvm) writeString fun writeString(string:String, charset:Charset):Buffer
fun writeString(string:String, beginIndex:Int, endIndex:Int, charset:Charset):Buffer
(jvm) writeTo Write byteCount bytes from this to out.fun writeTo(out:OutputStream, byteCount:Long= size):Buffer
(js, jvm, native) writeUtf8 fun writeUtf8(string:String):Buffer
fun writeUtf8(string:String, beginIndex:Int, endIndex:Int):Buffer
(js, jvm, native) writeUtf8CodePoint fun writeUtf8CodePoint(codePoint:Int):Buffer

Inherited Functions

Name Summary
(jvm) buffer Returns this source’s internal buffer.abstract fun ~~buffer~~():Buffer
(jvm) inputStream Returns an input stream that reads from this source.abstract fun inputStream():InputStream
(jvm) outputStream Returns an output stream that writes to this sink.abstract fun outputStream():OutputStream
(jvm) readString Removes all bytes from this, decodes them as charset, and returns the string.abstract fun readString(charset:Charset):String
Removes byteCount bytes from this, decodes them as charset, and returns the string.abstract fun readString(byteCount:Long, charset:Charset):String
(jvm) writeString abstract fun writeString(string:String, charset:Charset):BufferedSink
abstract fun writeString(string:String, beginIndex:Int, endIndex:Int, charset:Charset):BufferedSink

Extension Functions

Name Summary
(jvm) deflate Returns an DeflaterSink that DEFLATE-compresses data to this Sink while writing.funSink.deflate(deflater:Deflater= Deflater()):DeflaterSink
(jvm) inflate Returns an InflaterSource that DEFLATE-decompresses this Source while reading.funSource.inflate(inflater:Inflater= Inflater()):InflaterSource