Stream

interface Stream : StreamInput, StreamOutput

Represents input and output stream.

Author

Alexander Kornilov (akornilov.82@gmail.com).

Functions

Link copied to clipboard
open fun canRead(bytes: Int): Boolean

Returns true when stream can provide bytes.

Link copied to clipboard
open fun canWrite(bytes: Int): Boolean

Returns true if stream has place for bytes.

Link copied to clipboard
abstract override fun close()
Link copied to clipboard
abstract fun flush()
Link copied to clipboard
open fun forLines(    encoding: StringEncoding = defaultStringEncoding,     byteOrder: ByteOrder = defaultByteOrder,     block: (lines: Sequence<String>) -> Unit)

Calls the block callback giving it a sequence of all the lines in the stream.

Link copied to clipboard
open operator fun plusAssign(value: Byte)

Writes Byte into the stream.

open operator fun plusAssign(value: ByteArray)

Writes ByteArray into the stream.

open operator fun plusAssign(value: Double)

Writes Double into the stream using defaultByteOrder.

open operator fun plusAssign(value: Float)

Writes Float into the stream using defaultByteOrder.

open operator fun plusAssign(value: Int)

Writes Int into the stream using defaultByteOrder.

open operator fun plusAssign(value: Long)

Writes Long into the stream using defaultByteOrder.

open operator fun plusAssign(value: Short)

Writes Short into the stream using defaultByteOrder.

open operator fun plusAssign(value: String)

Writes String into the stream using defaultStringEncoding and defaultByteOrder.

open operator fun plusAssign(value: UInt)

Writes UInt into the stream using defaultByteOrder.

open operator fun plusAssign(value: ULong)

Writes ULong into the stream using defaultByteOrder.

open operator fun plusAssign(value: UShort)

Writes UShort into the stream using defaultByteOrder.

Link copied to clipboard
abstract fun readByte(): Byte

Reads Byte from the stream.

Link copied to clipboard
abstract fun readBytes(    buffer: ByteArray,     size: Int = buffer.size,     offset: Int = 0): Int

Reads bytes size from the stream into the buffer starting from offset.

Link copied to clipboard
open fun readByteUnsigned(): Int

Reads unsigned Byte from the stream.

Link copied to clipboard
open fun readChar(encoding: StringEncoding = defaultStringEncoding, byteOrder: ByteOrder = defaultByteOrder): Int

Reads code point from the stream with specified encoding and byteOrder.

Link copied to clipboard
open fun readDouble(byteOrder: ByteOrder = defaultByteOrder): Double

Reads Double from the stream with specified byteOrder.

Link copied to clipboard
open fun readFloat(byteOrder: ByteOrder = defaultByteOrder): Float

Reads Float from the stream with specified byteOrder.

Link copied to clipboard
open fun readInt(byteOrder: ByteOrder = defaultByteOrder): Int

Reads Int from the stream with specified byteOrder.

abstract fun readInt(    bytes: Int,     signed: Boolean = true,     byteOrder: ByteOrder = defaultByteOrder): Long

Read integer value from the stream with specified bytes count and byteOrder. If signed is true the last bit interprets as a sign. This means that if last bit is 1 the rest of the bits higher than last bit would be filled by 1. The bytes should be in range 1..8.

Link copied to clipboard
open fun readLine(encoding: StringEncoding = defaultStringEncoding, byteOrder: ByteOrder = defaultByteOrder): String

Reads line from the stream with specified encoding and byteOrder.

Link copied to clipboard
open fun readLong(byteOrder: ByteOrder = defaultByteOrder): Long

Reads Long from the stream with specified byteOrder.

abstract fun readLong(    bytes: Int,     signed: Boolean = true,     byteOrder: ByteOrder = defaultByteOrder): BigInteger

Read integer value from the stream as BigInteger with specified bytes count and byteOrder. If signed is true the last bit interprets as a sign.

Link copied to clipboard
open fun readShort(byteOrder: ByteOrder = defaultByteOrder): Short

Reads Short from the stream with specified byteOrder.

Link copied to clipboard
open fun readString(    encoding: StringEncoding = defaultStringEncoding,     length: Int = -1,     byteOrder: ByteOrder = defaultByteOrder): String

Reads string from the stream with specified encoding, byteOrder and length limit.

Link copied to clipboard
open fun readUInt(byteOrder: ByteOrder = defaultByteOrder): UInt

Reads UInt from the stream with specified byteOrder.

Link copied to clipboard
abstract fun readULong(byteOrder: ByteOrder = defaultByteOrder): ULong

Reads ULong from the stream with specified byteOrder.

Link copied to clipboard
open fun readUShort(byteOrder: ByteOrder = defaultByteOrder): UShort

Reads UShort from the stream with specified byteOrder.

Link copied to clipboard
open override fun skip(bytes: Long): Long

Skips bytes in the stream.

Link copied to clipboard
open fun useLines(    encoding: StringEncoding = defaultStringEncoding,     byteOrder: ByteOrder = defaultByteOrder,     block: (lines: Sequence<String>) -> Unit)

Calls the block callback giving it a sequence of all the lines in the stream and closes it once the processing is complete.

Link copied to clipboard
open fun writeBom(encoding: StringEncoding = defaultStringEncoding, byteOrder: ByteOrder = defaultByteOrder)

Writes BOM character into the stream for non-ASCII encoding.

Link copied to clipboard
abstract fun writeByte(value: Byte)

Writes Byte into the stream.

Link copied to clipboard
open fun writeBytes(    buffer: ByteArray,     size: Int = buffer.size,     offset: Int = 0)

Writes size bytes of buffer starting from offset into the stream.

Link copied to clipboard
open fun writeChar(    value: Int,     encoding: StringEncoding = defaultStringEncoding,     byteOrder: ByteOrder = defaultByteOrder)

Writes code point value with specified byteOrder into the stream.

Link copied to clipboard
open fun writeDouble(value: Double, byteOrder: ByteOrder = defaultByteOrder)

Writes Double with specified byteOrder into the stream.

Link copied to clipboard
open fun writeFloat(value: Float, byteOrder: ByteOrder = defaultByteOrder)

Writes Float with specified byteOrder into the stream.

Link copied to clipboard
open fun writeInt(value: Int, byteOrder: ByteOrder = defaultByteOrder)

Writes Int with specified byteOrder into the stream.

open fun writeInt(    value: Long,     bytes: Int,     byteOrder: ByteOrder = defaultByteOrder)

Writes value with size in bytes with specified byteOrder into the stream. The bytes should be in 1..8 range.

Link copied to clipboard
open fun writeLine(    value: String = "",     encoding: StringEncoding = defaultStringEncoding,     byteOrder: ByteOrder = defaultByteOrder): Int

Writes line value with specified encoding and byteOrder into the stream.

Link copied to clipboard
open fun writeLong(value: Long, byteOrder: ByteOrder = defaultByteOrder)

Writes Long with specified byteOrder into the stream.

open fun writeLong(    value: BigInteger,     bytes: Int,     byteOrder: ByteOrder = defaultByteOrder)

Writes value with size in bytes with specified byteOrder into the stream.

Link copied to clipboard
open fun writeShort(value: Short, byteOrder: ByteOrder = defaultByteOrder)

Writes Short with specified byteOrder into the stream.

Link copied to clipboard
open fun writeString(    value: String,     encoding: StringEncoding = defaultStringEncoding,     startIndex: Int = 0,     size: Int = -1,     byteOrder: ByteOrder = defaultByteOrder,     needTerminator: Boolean = true): Int

Writes string into the stream.

Link copied to clipboard
open fun writeUInt(value: UInt, byteOrder: ByteOrder = defaultByteOrder)

Writes UInt with specified byteOrder into the stream.

Link copied to clipboard
open fun writeULong(value: ULong, byteOrder: ByteOrder = defaultByteOrder)

Writes ULong with specified byteOrder into the stream.

Link copied to clipboard
open fun writeUShort(value: UShort, byteOrder: ByteOrder = defaultByteOrder)

Writes UShort with specified byteOrder into the stream.

Properties

Link copied to clipboard
open val bytesAvailable: Long

Contains available bytes for read/write and the negative value if available bytes is not available at moment. Also, this property take into account limit value if it has non-negative value and isSupportLimit is true.

Link copied to clipboard
open val canFetchMore: StreamInput.FetchHint

Optional hint for fetch ability (maybe useful for streams where size isn't available).

Link copied to clipboard
abstract override var defaultByteOrder: ByteOrder

Default byte order of the stream.

Link copied to clipboard
abstract override var defaultStringEncoding: StringEncoding

Default string encoding of the stream.

Link copied to clipboard
abstract val isClosed: Boolean

Indicate that stream is closed.

Link copied to clipboard
open val isEof: Boolean

Indicates that end of stream reached.

Link copied to clipboard
abstract val isFixedSize: Boolean

Indicates that stream has fixed size.

Link copied to clipboard
abstract val isNetwork: Boolean

Indicates that stream is network based (pipe, socket etc).

Link copied to clipboard
abstract val isReadable: Boolean

Indicates that stream is readable.

Link copied to clipboard
abstract val isSeekable: Boolean

Indicates that position of the stream can be changed.

Link copied to clipboard
abstract val isSupportLimit: Boolean

Indicates that stream support validation of the limit field.

Link copied to clipboard
abstract val isWritable: Boolean

Indicates that stream is writable.

Link copied to clipboard
abstract override var limit: Long

The limit position of the stream for read and write. If someone try to read or write out of this bound (position >= limit) the LimitOutOfBoundsException should be thrown.

Link copied to clipboard
abstract override var position: Long

Current position can be changed for seekable streams.

Link copied to clipboard
abstract var readBufferSize: Int

Read buffer size in bytes for streams which support read buffering. Has negative value if buffering is not supported.

Link copied to clipboard
abstract var readTimeout: Int

Read timeout for special streams (e.g. socket). Has negative value if timeout is not supported.

Link copied to clipboard
abstract val size: Long

Current stream size in bytes or negative value if size isn't available at moment.

Link copied to clipboard
abstract var writeBufferSize: Int

Write buffer size in bytes for streams which support write buffering. Has negative value if buffering is not supported.

Link copied to clipboard
abstract var writeTimeout: Int

Write timeout for special streams (e.g. socket). Has negative value if timeout is not supported.

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
inline fun Stream.setLimit(position: Long, limit: Long)

Sets current stream position and limit. e.g. after setLimit(10, 5) the stream position will be 10 and limit 15.

Link copied to clipboard
fun Stream.tryDetectBom(): Boolean

Tries to detect BOM signature in current position and if success updates Stream.defaultByteOrder and Stream.defaultStringEncoding.