Scoop API

scoop:scoop.std:0.1.0

src/bytes/byte_reader.scoop

Selected production source · src/bytes/byte_reader.scoop

1package scoop.bytes
3/**
4 * A cursor over the supplied byte list, with explicit byte order for multibyte values. Reads
5 * require enough remaining bytes and advance the cursor.
6 */
7public class ByteArrayReader public constructor(input: List<Byte>) {
8 private val bytes: Array<Byte> = Array<Byte>(input.size) { index -> input[index] }
9 private var cursor: Long = 0L
10 /**
11 * The total number of bytes in the input.
12 */
13 public val size: Long get() = bytes.size
14 /**
15 * The current zero-based byte cursor.
16 */
17 public val position: Long get() = cursor
18 /**
19 * The number of bytes available from the cursor to the end.
20 */
21 public val remaining: Long get() = bytes.size - cursor
23 private fun requireAvailable(count: Long) {
24 if (count < 0L) throw IllegalArgumentException(Some("negative byte count"))
25 if (count > remaining) throw IndexOutOfBoundsException()
26 }
28 /**
29 * Moves to an absolute byte position between zero and the input size, inclusive.
30 */
31 public fun seek(position: Long) {
32 if (position < 0L || position > bytes.size) throw IndexOutOfBoundsException()
33 cursor = position
34 }
36 /**
37 * Advances by a nonnegative count within the remaining input.
38 */
39 public fun skip(count: Long) {
40 requireAvailable(count)
41 cursor += count
42 }
44 /**
45 * Copies the requested nonnegative number of bytes and advances the cursor; insufficient input
46 * throws.
47 */
48 public fun readBytes(count: Long): Array<Byte> {
49 requireAvailable(count)
50 val start = cursor
51 val result = Array<Byte>(count) { index -> bytes[start + index] }
52 cursor += count
53 return result
54 }
56 private fun read(width: Long, order: ByteOrder): ULong {
57 requireAvailable(width)
58 var result = 0uL
59 var index = 0L
60 while (index < width) {
61 val offset = if (order == ByteOrder.BigEndian) index else width - index - 1L
62 result = (result shl 8L) or bytes[cursor + offset].toUByte().toULong()
63 index += 1L
64 }
65 cursor += width
66 return result
67 }
69 /**
70 * Reads and advances past one Int8 value as one byte. Insufficient remaining input throws.
71 */
72 public fun readInt8(): Int8 = read(1L, ByteOrder.BigEndian).toByte()
73 /**
74 * Reads and advances past one UInt8 value as one byte. Insufficient remaining input throws.
75 */
76 public fun readUInt8(): UInt8 = read(1L, ByteOrder.BigEndian).toUByte()
77 /**
78 * Reads and advances past one Int16 value in the selected byte order. Insufficient remaining
79 * input throws.
80 */
81 public fun readInt16(order: ByteOrder): Int16 = read(2L, order).toShort()
82 /**
83 * Reads and advances past one UInt16 value in the selected byte order. Insufficient remaining
84 * input throws.
85 */
86 public fun readUInt16(order: ByteOrder): UInt16 = read(2L, order).toUShort()
87 /**
88 * Reads and advances past one Int32 value in the selected byte order. Insufficient remaining
89 * input throws.
90 */
91 public fun readInt32(order: ByteOrder): Int32 = read(4L, order).toInt()
92 /**
93 * Reads and advances past one UInt32 value in the selected byte order. Insufficient remaining
94 * input throws.
95 */
96 public fun readUInt32(order: ByteOrder): UInt32 = read(4L, order).toUInt()
97 /**
98 * Reads and advances past one Int64 value in the selected byte order. Insufficient remaining
99 * input throws.
100 */
101 public fun readInt64(order: ByteOrder): Int64 = read(8L, order).toLong()
102 /**
103 * Reads and advances past one UInt64 value in the selected byte order. Insufficient remaining
104 * input throws.
105 */
106 public fun readUInt64(order: ByteOrder): UInt64 = read(8L, order)
107 /**
108 * Reads and advances past the IEEE 754 bit representation of a Float value in the selected
109 * byte order. Insufficient remaining input throws.
110 */
111 public fun readFloat(order: ByteOrder): Float = Float.fromBits(readInt32(order))
112 /**
113 * Reads and advances past the IEEE 754 bit representation of a Double value in the selected
114 * byte order. Insufficient remaining input throws.
115 */
116 public fun readDouble(order: ByteOrder): Double = Double.fromBits(readInt64(order))