Scoop API

scoop:scoop.std:0.1.0

src/bytes/byte_builder.scoop

Selected production source · src/bytes/byte_builder.scoop

1package scoop.bytes
3/**
4 * A growable in-memory byte accumulator. Byte-array conversions return independent copies; clear
5 * resets the logical size for reuse.
6 */
7public class ByteArrayBuilder public constructor() {
8 private var storage: MutableArray<Byte> = MutableArray<Byte>(0L) { _ -> 0.toByte() }
9 private var count: Long = 0L
10 /**
11 * The number of accumulated bytes.
12 */
13 public val size: Long get() = count
15 private fun reserve(extra: Long) {
16 if (extra > Long.MAX_VALUE - count) throw ArithmeticException()
17 val required = count + extra
18 if (required <= storage.size) return
19 val grown = if (storage.size == 0L) 8L
20 else if (storage.size > Long.MAX_VALUE / 2L) Long.MAX_VALUE else storage.size * 2L
21 val capacity = if (required > grown) required else grown
22 val old = storage
23 val length = count
24 storage = MutableArray<Byte>(capacity) { index -> if (index < length) old[index] else 0.toByte() }
25 }
27 /**
28 * Appends the supplied byte or byte sequence and returns this builder.
29 */
30 public fun append(value: Byte): ByteArrayBuilder {
31 reserve(1L)
32 storage[count] = value
33 count += 1L
34 return this
35 }
37 /**
38 * Appends the supplied byte or byte sequence and returns this builder.
39 */
40 public fun append(value: List<Byte>): ByteArrayBuilder {
41 val length = value.size
42 if (length > Long.MAX_VALUE - count) throw ArithmeticException()
43 val snapshot = Array<Byte>(length) { index -> value[index] }
44 reserve(snapshot.size)
45 var index = 0L
46 while (index < snapshot.size) { storage[count + index] = snapshot[index]; index += 1L }
47 count += snapshot.size
48 return this
49 }
51 /**
52 * Removes all accumulated bytes while retaining reusable storage.
53 */
54 public fun clear() {
55 var index = 0L
56 while (index < count) { storage[index] = 0.toByte(); index += 1L }
57 count = 0L
58 }
60 /**
61 * Copies the accumulated bytes into a read-only array.
62 */
63 public fun toByteArray(): Array<Byte> = Array<Byte>(count) { index -> storage[index] }
64 /**
65 * Copies the accumulated bytes into an independent mutable array.
66 */
67 public fun toMutableByteArray(): MutableArray<Byte> = MutableArray<Byte>(count) { index -> storage[index] }
69 private fun write(value: ULong, width: Long, order: ByteOrder) {
70 reserve(width)
71 var index = 0L
72 while (index < width) {
73 val shift = (if (order == ByteOrder.BigEndian) width - index - 1L else index) * 8L
74 storage[count + index] = (value shr shift).toByte()
75 index += 1L
76 }
77 count += width
78 }
80 /**
81 * Appends one Int8 value as one byte.
82 */
83 public fun writeInt8(value: Int8) { append(value) }
84 /**
85 * Appends one UInt8 value as one byte.
86 */
87 public fun writeUInt8(value: UInt8) { append(value.toByte()) }
88 /**
89 * Appends one Int16 value in the selected byte order.
90 */
91 public fun writeInt16(value: Int16, order: ByteOrder) { write(value.toULong(), 2L, order) }
92 /**
93 * Appends one UInt16 value in the selected byte order.
94 */
95 public fun writeUInt16(value: UInt16, order: ByteOrder) { write(value.toULong(), 2L, order) }
96 /**
97 * Appends one Int32 value in the selected byte order.
98 */
99 public fun writeInt32(value: Int32, order: ByteOrder) { write(value.toULong(), 4L, order) }
100 /**
101 * Appends one UInt32 value in the selected byte order.
102 */
103 public fun writeUInt32(value: UInt32, order: ByteOrder) { write(value.toULong(), 4L, order) }
104 /**
105 * Appends one Int64 value in the selected byte order.
106 */
107 public fun writeInt64(value: Int64, order: ByteOrder) { write(value.toULong(), 8L, order) }
108 /**
109 * Appends one UInt64 value in the selected byte order.
110 */
111 public fun writeUInt64(value: UInt64, order: ByteOrder) { write(value, 8L, order) }
112 /**
113 * Appends the IEEE 754 bit representation of a Float value in the selected byte order.
114 */
115 public fun writeFloat(value: Float, order: ByteOrder) { writeInt32(value.toBits(), order) }
116 /**
117 * Appends the IEEE 754 bit representation of a Double value in the selected byte order.
118 */
119 public fun writeDouble(value: Double, order: ByteOrder) { writeInt64(value.toBits(), order) }