BitStream

class BitStream(stream: Stream) : Stream

Bit access over Stream.

Author

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

Constructors

Link copied to clipboard
fun BitStream(input: StreamInput)

Constructs stream from StreamInput

Link copied to clipboard
fun BitStream(output: StreamOutput)

Constructs stream from StreamOutput

Link copied to clipboard
fun BitStream(stream: Stream)

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
open override fun close()

Closes the stream (include underlying stream as well).

Link copied to clipboard
open override fun flush()

Flushes write cache into underlying stream.

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
operator fun plusAssign(value: Boolean)

Writes bit as += operation.

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
fun readBigInteger(bits: Int, signed: Boolean = false): BigInteger

Reads arbitrary number of bits from the stream. If signed is true the last bit is interpreted as indicator of sign. Bits order is sequential that means BigEndian byte order for the integers which contain more then one byte.

Link copied to clipboard
fun readBit(): Boolean

Reads one bit from the stream.

Link copied to clipboard
fun readBits(bits: Int, signed: Boolean = false): Long

Reads specified bits from the stream. The bits should be in range 1..64. If signed is true the last bit is interpreted as indicator of sign. Bits order is sequential that means BigEndian byte order for the integers which contain more then one byte.

Link copied to clipboard
open override fun readByte(): Byte

Reads Byte from the stream.

Link copied to clipboard
open override fun readBytes(    buffer: ByteArray,     size: Int,     offset: Int): Int

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

Link copied to clipboard
open override 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.

open override fun readInt(    bytes: Int,     signed: Boolean,     byteOrder: ByteOrder): 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.

open override fun readLong(    bytes: Int,     signed: Boolean,     byteOrder: ByteOrder): 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
fun readUBits(bits: Int): ULong

Reads specified bits from the stream. The bits should be in range 1..64. Bits order is sequential that means BigEndian byte order for the integers which contain more then one byte.

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

Reads UInt from the stream with specified byteOrder.

Link copied to clipboard
open override fun readULong(byteOrder: ByteOrder): 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
fun skip(bits: Int): Boolean

Skips bits in the stream.

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
fun write(value: Boolean)

Writes one bit to the stream.

fun write(value: BigInteger, bits: Int)

Writes arbitrary number of bits from the BigInteger. The bits are written in natural order that means BigEndian byte order for integers which have more than one byte.

fun write(value: Long, bits: Int)

Writes bits from the Long. The bits should be in 1..64. The bits are written in natural order that means BigEndian byte order for integers which have more than one byte.

fun write(value: ULong, bits: Int)

Writes bits from the ULong. The bits should be in 1..64. The bits are written in natural order that means BigEndian byte order for integers which have more than one byte.

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
open override 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
var bitPosition: Long

Current absolute bit position in the stream.

Link copied to clipboard
open override 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
open override var defaultByteOrder: ByteOrder

Default byte order of the stream.

Link copied to clipboard
open override var defaultStringEncoding: StringEncoding

Default string encoding of the stream.

Link copied to clipboard
open override 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
open override val isFixedSize: Boolean

Indicates that stream has fixed size.

Link copied to clipboard
open override val isNetwork: Boolean

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

Link copied to clipboard
open override val isReadable: Boolean

Indicates that stream is readable.

Link copied to clipboard
open override val isSeekable: Boolean

Indicates that position of the stream can be changed.

Link copied to clipboard
open override val isSupportLimit: Boolean = false

Indicates that stream support validation of the limit field.

Link copied to clipboard
open override val isWritable: Boolean

Indicates that stream is writable.

Link copied to clipboard
open 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
var offset: Int

Bit offset in current byte (0..7).

Link copied to clipboard
open override var position: Long

Current position can be changed for seekable streams.

Link copied to clipboard
open override 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
open override var readTimeout: Int

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

Link copied to clipboard
open override val size: Long

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

Link copied to clipboard
open override 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
open override var writeTimeout: Int

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