Package io.ktor.utils.io.core
Types
abstract class AbstractInput : Input The default abstract base class implementing Input interface. |
|
abstract class AbstractOutput : Appendable, Output The default Output implementation. |
|
open class Buffer Represents a buffer with read and write positions. |
|
enum expect class ByteOrder |
|
class BytePacketBuilder : BytePacketBuilderPlatformBase A builder that provides ability to build byte packets with no knowledge of it’s size. Unlike Java’s ByteArrayOutputStream it doesn’t copy the whole content every time it’s internal buffer overflows but chunks buffers instead. Packet building via build function is O(1) operation and only does instantiate a new ByteReadPacket. Once a byte packet has been built via build function call, the builder could be reused again. You also can discard all written bytes via reset or release. Please note that an instance of builder need to be terminated either via build function invocation or via release call otherwise it will cause byte buffer leak so that may have performance impact. |
|
abstract class |
|
abstract class |
|
class ByteReadPacket : ByteReadPacketPlatformBase, Input Read-only immutable byte packet. Could be consumed only once however it does support copy that doesn’t copy every byte but creates a new view instead. Once packet created it should be either completely read (consumed) or released via release. |
|
abstract class |
|
abstract class |
|
expect interface Closeable |
|
expect interface Input : Closeable Usually shouldn’t be implemented directly. Inherit AbstractInput instead. |
|
expect class A read-write facade to actual buffer of fixed size. Multiple views could share the same actual buffer. Concurrent unsafe. The only concurrent-safe operation is release. In most cases ByteReadPacket and BytePacketBuilder should be used instead. |
|
expect interface Output : Appendable, Closeable This shouldn’t be implemented directly. Inherit AbstractOutput instead. |
Annotations
annotation class ExperimentalIoApi API marked with this annotation is experimental and could be changed |
Exceptions
class BufferLimitExceededException : Exception |
|
expect class EOFException : IOException |
|
class InsufficientSpaceException : Exception |
Extensions for External Classes
Properties
expect val PACKET_MAX_COPY_SIZE: Int |
|
For streaming input it should be Input.endOfInput instead. val ByteReadPacket.isEmpty: Boolean |
|
For streaming input there is no reliable way to detect it without triggering bytes population from the underlying source. Consider using Input.endOfInput or use ByteReadPacket instead. val ByteReadPacket.isNotEmpty: Boolean |
Functions
expect fun BytePacketBuilder( |
|
expect fun ByteReadPacket( fun ByteReadPacket( fun ByteReadPacket( |
|
expect fun String( Create an instance of String from the specified bytes range starting at offset and bytes length interpreting characters in the specified charset. |
|
fun Buffer. fun Buffer. fun Output.append( fun Output.append( |
|
fun buildPacket( Build a byte packet in block lambda. Creates a temporary builder and releases it in case of failure |
|
Copy all bytes to the output. Depending on actual input and output implementation it could be zero-copy or copy byte per byte. All regular types such as ByteReadPacket, BytePacketBuilder, AbstractInput and AbstractOutput are always optimized so no bytes will be copied. |
|
Discard all remaining bytes. |
|
Discard exactly n bytes or fail if not enough bytes in the input to be discarded. |
|
Discards bytes until delimiter occurred |
|
Discards bytes until of of the specified delimiters delimiter1 or delimiter2 occurred |
|
Write byte value repeated the specified times. |
|
For every byte from this buffer invokes block function giving it as parameter. For every byte from this input invokes block function giving it as parameter. |
|
fun ChunkBuffer. |
|
fun Input. fun Input. Copy at least min but up to max bytes to the specified destination buffer from this input
skipping offset bytes. If there are not enough bytes available to provide min bytes then
it simply return number of available bytes with no exception so the returned value need
to be checked.
It is safe to specify |
|
fun <R> BytePacketBuilder.preview( Creates a temporary packet view of the packet being build without discarding any bytes from the builder.
This is similar to |
|
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 IoBuffer. fun Input.readAvailable( fun Input.readAvailable( fun Input.readAvailable( fun Input.readAvailable( fun Buffer.readAvailable( 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 ByteReadPacket.readAvailable(dst: ByteBuffer): Int Read at most |
|
fun Input.readAvailableLittleEndian( fun Input.readAvailableLittleEndian( fun Input.readAvailableLittleEndian( fun Input.readAvailableLittleEndian( fun Input.readAvailableLittleEndian( fun Input.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( fun Buffer.readAvailableLittleEndian( |
|
fun Input. fun Input. fun Input. |
|
fun ByteReadPacket.readByteBuffer( Read exactly n (optional, read all remaining by default) bytes to a newly allocated byte buffer |
|
Read the specified number of bytes specified (optional, read all remaining by default) fun ByteReadPacket.readBytes( Read exactly n bytes (consumes all remaining if n is not specified but up to Int.MAX_VALUE bytes). Does fail if not enough bytes remaining. Reads exactly n bytes from the input or fails if not enough bytes available. Reads all remaining bytes from the input |
|
Reads at least min but no more than max bytes from the input to a new byte array |
|
fun ByteReadPacket. fun AbstractInput. fun ByteReadPacketBase. 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 IoBuffer. fun Input.readFully( fun Input.readFully( fun Input.readFully( fun Input.readFully( 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 ByteReadPacket.readFully(dst: ByteBuffer): Int Read exactly |
|
fun Input.readFullyLittleEndian( fun Input.readFullyLittleEndian( fun Input.readFullyLittleEndian( fun Input.readFullyLittleEndian( fun Input.readFullyLittleEndian( fun Input.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( fun Buffer.readFullyLittleEndian( |
|
fun Input. fun Input. fun Input. |
|
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 ByteReadPacket. fun Input. fun Input. Reads at most max characters decoding bytes with specified decoder. Extra character bytes will remain unconsumed fun Input.readText( Reads at most max characters decoding bytes with specified charset. Extra character bytes will remain unconsumed |
|
Read exactly n characters interpreting bytes in the specified charset. |
|
Read exactly the specified number of bytes interpreting bytes in the specified charset (optional, UTF-8 by default). Read exactly bytesCount interpreting bytes in the specified charset (optional, UTF-8 by default). |
|
Read exactly charactersCount characters interpreting bytes in the specified charset. |
|
fun Buffer.readUByte(): <ERROR CLASS> Read an unsigned byte or fail if no bytes available for reading. fun IoBuffer. fun Input.readUByte(): <ERROR CLASS> |
|
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 IoBuffer. fun Input.readUInt(): <ERROR CLASS> |
|
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 IoBuffer. fun Input.readULong(): <ERROR CLASS> |
|
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). fun IoBuffer. fun Input.readUShort(): <ERROR CLASS> |
|
fun ByteReadPacket.readUTF8Line( Read a string line considering optionally specified estimate but up to optional limit characters length
(does fail once limit exceeded) or return |
|
fun Input.readUTF8LineTo( Read UTF-8 line and append all line characters to out except line endings. Does support CR, LF and CR+LF |
|
Reads UTF-8 characters until one of the specified delimiters found, limit exceeded or end of stream encountered |
|
fun Input.readUTF8UntilDelimiterTo( fun Input.readUTF8UntilDelimiterTo( Reads UTF-8 characters to out buffer until one of the specified delimiters found, limit exceeded or end of stream encountered fun Input. |
|
fun Input.readUntilDelimiter( Copies to dst array at offset at most length bytes or until the specified delimiter occurred. Copies to dst output until the specified delimiter occurred. |
|
fun Input.readUntilDelimiters( Copies to dst array at offset at most length bytes or until one of the specified delimiters delimiter1 or delimiter2 occurred. Copies to dst output until one of the specified delimiters delimiter1 or delimiter2 occurred. |
|
fun IoBuffer.release(pool: ObjectPool<IoBuffer>): Unit releases buffer view and returns it to the pool if there are no more usages. Based on simple ref-counting so it is very fragile. |
|
fun ChunkBuffer.remainingAll(): Long Summarize remainings of all elements of the chain |
|
fun BytePacketBuilder.reset(): Unit Discard all written bytes and prepare to build another packet. |
|
Invoke block function for every chunk until end of input or block function return |
|
Invoke block function for every chunk until end of input or block function return zero block function returns number of bytes required to read next primitive and shouldn’t require too many bytes at once otherwise it could fail with an exception. It is not guaranteed that every chunk will have fixed size but it will be always at least requested bytes length. block function should never release provided buffer and should not write to it otherwise an undefined behaviour could be observed |
|
Peek the next unsigned byte or return |
|
Invoke block function with a temporary Buffer instance of the specified size in bytes. The provided instance shouldn’t be captured and used outside of the block otherwise an undefined behaviour may occur including crash and/or data corruption. fun <R> withBuffer( Invoke block function with a temporary Buffer instance taken from the specified pool. Depending on the pool it may be safe or unsafe to capture and use the provided buffer outside of the block. Usually it is always recommended to NOT capture an instance outside. |
|
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 BytePacketBuilder. Write bytes directly to packet builder’s segment. Generally shouldn’t be used in user’s code and useful for efficient integration. 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 IoBuffer. fun Output.writeFully( fun Output.writeFully( fun Output.writeFully( fun Output.writeFully( fun Output.writeFully(bb: ByteBuffer): Unit 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 BytePacketBuilder. Write all src buffer remaining bytes and change it’s position accordingly |
|
fun Output.writeFullyLittleEndian( fun Buffer.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.writeFullyLittleEndian( fun Output.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). |
|
fun Output.writePacket(packet: ByteReadPacket): Unit |
|
Write a short integer or fail if not enough space available for writing. The numeric value is encoded in the network order (Big Endian). |
|
fun Output. Writes text characters in range [fromIndex .. toIndex) with the specified encoder fun Output.writeText( fun Output.writeText( Writes text characters in range [fromIndex .. toIndex) with the specified charset |
|
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). |
|
Append number of chunks invoking block function while the returned value is true. Depending on the output underlying implementation it could invoke block function with the same buffer several times however it is guaranteed that it is always non-empty. |
|
Append number of chunks invoking block function while the returned value is positive. If returned value is positive then it will be invoked again with a buffer having at least requested number of bytes space (could be the same buffer as before if it complies to the restriction). |