-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathByteReadingBuffer.kt
80 lines (65 loc) · 2 KB
/
ByteReadingBuffer.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright 2017-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.serialization.efficientBinaryFormat
class ByteReadingBuffer(val buffer: ByteArray) {
private var next = 0
private fun nextByte(): Int {
return buffer[next++].toInt() and 0xff
}
private fun nextByteL(): Long {
return buffer[next++].toLong() and 0xffL
}
operator fun get(pos: Int): Byte {
if(pos !in 0..<buffer.size) { throw IndexOutOfBoundsException("Position $pos out of range") }
return buffer[pos]
}
fun readByte(): Byte {
return buffer[next++]
}
fun readShort(): Short {
return (nextByte() or (nextByte() shl 8)).toShort()
}
fun readInt(): Int {
return nextByte() or
(nextByte() shl 8) or
(nextByte() shl 16) or
(nextByte() shl 24)
}
fun readLong(): Long {
return nextByteL() or
(nextByteL() shl 8) or
(nextByteL() shl 16) or
(nextByteL() shl 24) or
(nextByteL() shl 32) or
(nextByteL() shl 40) or
(nextByteL() shl 48) or
(nextByteL() shl 56)
}
fun readFloat(): Float {
return Float.fromBits(readInt())
}
fun readDouble(): Double {
val l = readLong()
return Double.fromBits(l)
}
fun readChar(): Char {
return (nextByte() or (nextByte() shl 8)).toChar()
}
fun readString(): String {
val len = readInt()
val chars = CharArray(len) { readChar() }
return chars.concatToString()
}
fun readString(consumeChunk: (String) -> Unit) {
val len = readInt()
var remaining = len
while (remaining > 1024) {
remaining -= 1024
val chunk = CharArray(1024) { readChar() }
consumeChunk(chunk.concatToString())
}
val chars = CharArray(remaining) { readChar() }
consumeChunk(chars.concatToString())
}
}