ByteReadPacket

class ByteReadPacket : 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.

Constructors

Link copied to clipboard
fun ByteReadPacket(head: ChunkBuffer, pool: ObjectPool<ChunkBuffer>)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun canRead(): Boolean
Link copied to clipboard
open override fun close()

Close input including the underlying source. All pending bytes will be discarded. It is not recommended to invoke it with read operations in-progress concurrently.

Link copied to clipboard
fun copy(): ByteReadPacket

Returns a copy of the packet. The original packet and the copy could be used concurrently. Both need to be either completely consumed or released via release

Link copied to clipboard
fun discard(n: Int): Int

Discards at most n bytes

fun discard(n: Long): Long

Discard at most n bytes

Link copied to clipboard
fun discardExact(n: Int)

Discards exactly n bytes or fails with EOFException

Link copied to clipboard
fun hasBytes(n: Int): Boolean
Link copied to clipboard
fun peekTo(buffer: ChunkBuffer): Int

fun peekTo(destination: Memory, destinationOffset: Long, offset: Long = 0, min: Long = 1, max: Long = Long.MAX_VALUE): Long

Try to 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 after skipping offset bytes then it will trigger the underlying source reading first and after that will simply copy available bytes even if EOF encountered so min is not a requirement but a desired number of bytes. It is safe to specify max greater than the destination free space. min shouldn't be bigger than the destination free space. This function could trigger the underlying source reading that may lead to blocking I/O. It is allowed to specify too big offset so in this case this function will always return 0 after prefetching all underlying bytes but note that it may lead to significant memory consumption. This function usually copy more bytes than min (unless max = min) but it is not guaranteed. When 0 is returned with offset = 0 then it makes sense to check endOfInput.

Link copied to clipboard
fun readByte(): Byte

Read the next upcoming byte

Link copied to clipboard
fun readText(min: Int = 0, max: Int = Int.MAX_VALUE): String

Read a string at last min and at most max characters length

fun readText(out: Appendable, min: Int = 0, max: Int = Int.MAX_VALUE): Int

Read at least min and at most max characters and append them to out

Link copied to clipboard
fun readTextExact(exactCharacters: Int): String

Read a string exactly exactCharacters length

fun readTextExact(out: Appendable, exactCharacters: Int)

Read exactly exactCharacters characters and append them to out

Link copied to clipboard
fun release()

Release packet. After this function invocation the packet becomes empty. If it has been copied via ByteReadPacket.copy then the copy should be released as well.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
fun tryPeek(): Int

Returns next byte (unsigned) or -1 if no more bytes available

Properties

Link copied to clipboard
val endOfInput: Boolean

It is true when it is known that no more bytes will be available. When it is false then this means that it is not known yet or there are available bytes. Please note that false value doesn't guarantee that there are available bytes so readByte() may fail.

Link copied to clipboard
val pool: ObjectPool<ChunkBuffer>
Link copied to clipboard
val remaining: Long

Number of bytes available for read

Extensions

Link copied to clipboard
fun ByteReadPacket.inputStream(): InputStream

Creates InputStream adapter to the packet

Link copied to clipboard
val ByteReadPacket.isEmpty: Boolean
Link copied to clipboard
val ByteReadPacket.isNotEmpty: Boolean
Link copied to clipboard
fun ByteReadPacket.readArrayBuffer(n: Int = remaining.coerceAtMostMaxIntOrFail("Unable to make a new ArrayBuffer: packet is too big")): ArrayBuffer

Read exactly n bytes to a new array buffer instance

Link copied to clipboard
fun ByteReadPacket.readAvailable(dst: ByteBuffer): Int

Read at most dst.remaining() bytes to the specified dst byte buffer and change its position accordingly

fun ByteReadPacket.readAvailable(dst: <ERROR CLASS><<ERROR CLASS>>, limit: Int): Int
fun ByteReadPacket.readAvailable(dst: <ERROR CLASS><<ERROR CLASS>>, limit: Long): Long

Read at most limit bytes to the specified dst address

Link copied to clipboard
fun ByteReadPacket.readByteBuffer(n: Int = remaining.coerceAtMostMaxIntOrFail("Unable to make a ByteBuffer: packet is too big"), direct: Boolean = false): ByteBuffer

Read exactly n (optional, read all remaining by default) bytes to a newly allocated byte buffer

Link copied to clipboard
fun ByteReadPacket.readBytes(n: Int = remaining.coerceAtMostMaxIntOrFail("Unable to convert to a ByteArray: packet is too big")): ByteArray

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.

Link copied to clipboard
inline fun ByteReadPacket.readDirect(size: Int, block: (ByteBuffer) -> Unit)
Link copied to clipboard
fun ByteReadPacket.readerUTF8(): Reader

Creates Reader from the byte packet that decodes UTF-8 characters

Link copied to clipboard
fun ByteReadPacket.readFully(dst: ByteBuffer): Int

Read exactly dst.remaining() bytes to the specified dst byte buffer and change its position accordingly

fun ByteReadPacket.readFully(dst: <ERROR CLASS><<ERROR CLASS>>, size: Int): Int
fun ByteReadPacket.readFully(dst: <ERROR CLASS><<ERROR CLASS>>, size: Long): Long

Read exactly size bytes to the specified dst address

Link copied to clipboard
fun ByteReadPacket.readText(encoding: String, max: Int = Int.MAX_VALUE): String
fun ByteReadPacket.readText(encoding: String = "UTF-8", out: Appendable, max: Int = Int.MAX_VALUE): Int
fun ByteReadPacket.readText(decoder: CharsetDecoder, out: Appendable, max: Int = Int.MAX_VALUE): Int
Link copied to clipboard
fun ByteReadPacket.readUTF8Line(estimate: Int = 16, limit: Int = Int.MAX_VALUE): String?

Read a string line considering optionally specified estimate but up to optional limit characters length (does fail once limit exceeded) or return null if the packet is empty