Buffer
open class Buffer
Represents a buffer with read and write positions.
Concurrent unsafe: the same memory could be shared between different instances of Buffer however you can’t read/write using the same Buffer instance from different threads.
Constructors
Buffer(memory: Memory) Represents a buffer with read and write positions. |
Properties
var User data: could be a session, connection or anything useful |
|
val capacity: Int Buffer’s capacity (including reserved startGap and endGap. Value for released buffer is unspecified. |
|
val endGap: Int Number of bytes reserved in the end. |
|
var limit: Int Write position limit. No bytes could be written ahead of this limit. When the limit is less than the capacity
then this means that there are reserved bytes in the end (endGap). Such a reserved space in the end could be used
to write size, hash and so on. Also it is useful when several buffers are connected into a chain and some
primitive value (e.g. |
|
val memory: Memory |
|
var readPosition: Int Current read position. It is always non-negative and will never run ahead of the writePosition. It is usually greater or equal to startGap reservation. This position is affected by discard, rewind, resetForRead, resetForWrite, reserveStartGap and reserveEndGap. |
|
val readRemaining: Int Number of bytes available for reading. |
|
var startGap: Int Start gap is a reserved space in the beginning. The reserved space is usually used to write a packet length in the case when it’s not known before the packet constructed. |
|
var writePosition: Int Current write position. It is always non-negative and will never run ahead of the limit. It is always greater or equal to the readPosition. |
|
val writeRemaining: Int Size of the free space available for writing in bytes. |
Functions
Discard count readable bytes. |
|
open fun duplicate(): Buffer Create a new Buffer instance pointing to the same memory and having the same positions. |
|
fun readByte(): Byte Read the next byte or fail with EOFException if it’s not available. The returned byte is marked as consumed. |
|
Reserve endGap bytes in the end. Could move readPosition and writePosition to reserve space but only when no bytes were written or all written bytes are marked as consumed (were read or discarded). |
|
Reserve startGap bytes in the beginning. May move readPosition and writePosition if no bytes available for reading. |
|
open fun reset(): Unit Clear buffer’s state: read/write positions, gaps and so on. Byte content is not cleaned-up. |
|
fun resetForRead(): Unit Marks the whole buffer available for read and no for write |
|
fun resetForWrite(): Unit Marks all capacity writable except the start gap reserved before. The end gap reservation is discarded. Marks up to limit bytes of the buffer available for write and no bytes for read. It does respect startGap already reserved. All extra bytes after the specified limit are considered as endGap. |
|
Rewind readPosition backward to make count bytes available for reading again. |
|
open fun toString(): String |
|
fun tryPeekByte(): Int Peek the next unsigned byte or return |
|
fun tryReadByte(): Int Read the next unsigned byte or return |
|
Write a byte value at writePosition (incremented when written successfully). |
Companion Object Properties
val Empty: Buffer The empty buffer singleton: it has zero capacity for read and write. |
|
const val ReservedSize: Int Number of bytes usually reserved in the end of chunk when several instances of io.ktor.utils.io.core.internal.ChunkBuffer are connected into a chain (usually inside of ByteReadPacket or BytePacketBuilder) |
Extension Properties
Extension Functions
fun Buffer. fun Buffer. |
|
Decodes all the bytes to utf8 applying every character on consumer until or consumer return |
|
Write byte value repeated the specified times. |
|
For every byte from this buffer invokes block function giving it as parameter. |
|
Push back n bytes: only possible if there were at least n bytes read before this operation. |
|
Apply block of code with buffer’s memory providing read range indices. The returned value of block lambda should return number of bytes to be marked as consumed. No read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage. |
|
fun Buffer.readAvailable( fun Buffer.readAvailable( Read available for read bytes to the destination array range starting at array offset and length bytes. If less than length bytes available then less bytes will be copied and the corresponding number will be returned as result. fun Buffer.readAvailable( fun Buffer.readAvailable( fun Buffer.readAvailable( fun Buffer.readAvailable( fun Buffer.readAvailable( Read available for read bytes to the destination array range starting at array offset and length elements. If less than length elements available then less elements will be copied and the corresponding number will be returned as result (possibly zero). Read at most length available bytes to the dst buffer or fun Buffer.readAvailable( |
|
fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( |
|
Read the specified number of bytes specified (optional, read all remaining by default) |
|
fun Buffer. |
|
Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
Read a floating point number or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
fun Buffer.readFully(dst: ByteBuffer, length: Int): Unit fun Buffer.readFully( fun Buffer.readFully( Read from this buffer to the destination array to offset and length bytes. fun Buffer.readFully( fun Buffer.readFully( fun Buffer.readFully( fun Buffer.readFully( fun Buffer.readFully( Read from this buffer to the destination array to offset and length bytes. Numeric values are interpreted in the network byte order (Big Endian). Read at most length bytes from this buffer to the dst buffer. fun Buffer.readFully(destination: ByteBuffer): Unit Read buffer’s content to the destination buffer moving it’s position. |
|
fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( |
|
Read an integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
Read a long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
Read a short integer or fail if no bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
fun Buffer. |
|
fun Buffer.readUByte(): <ERROR CLASS> Read an unsigned byte or fail if no bytes available for reading. |
|
fun Buffer.readUInt(): <ERROR CLASS> Read an unsigned integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
fun Buffer.readULong(): <ERROR CLASS> Read an unsigned long integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
fun Buffer.readUShort(): <ERROR CLASS> Read an unsigned short integer or fail if not enough bytes available for reading. The numeric value is decoded in the network order (Big Endian). |
|
Peek the next unsigned byte or return |
|
Apply block of code with buffer’s memory providing write range indices. The returned value of block lambda should return number of bytes were written. o read/write functions on this buffer should be called inside of block otherwise an undefined behaviour may occur including data damage. |
|
fun Buffer. |
|
Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write a floating point number or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
fun Buffer.writeFully( fun Buffer.writeFully( Write the whole source array range staring at offset and having the specified bytes length. fun Buffer.writeFully( fun Buffer.writeFully( fun Buffer.writeFully( fun Buffer.writeFully( Write the whole source array range staring at offset and having the specified items length. Numeric values are interpreted in the network byte order (Big Endian). Write all readable bytes from src to this buffer. Fails if not enough space available to write all bytes. Write at most length readable bytes from src to this buffer. Fails if not enough space available to write all bytes. fun Buffer.writeFully(source: ByteBuffer): Unit Write source buffer content moving it’s position. |
|
fun Buffer.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( |
|
Write an integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write a long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write a short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write an unsigned byte or fail if not enough space available for writing. |
|
Write an unsigned integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write an unsigned long integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
Write an unsigned short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
Inheritors
open class ChunkBuffer : Buffer |