-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathdecoder.js
209 lines (162 loc) · 4.22 KB
/
decoder.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const Long = require('long')
const INT8_SIZE = 1
const INT16_SIZE = 2
const INT32_SIZE = 4
const INT64_SIZE = 8
const MOST_SIGNIFICANT_BIT = 0x80 // 128
const OTHER_BITS = 0x7f // 127
module.exports = class Decoder {
static int32Size() {
return INT32_SIZE
}
static decodeZigZag(value) {
return (value >>> 1) ^ -(value & 1)
}
static decodeZigZag64(longValue) {
return longValue.shiftRightUnsigned(1).xor(longValue.and(Long.fromInt(1)).negate())
}
constructor(buffer) {
this.buffer = buffer
this.offset = 0
}
readInt8() {
const value = this.buffer.readInt8(this.offset)
this.offset += INT8_SIZE
return value
}
readInt16() {
const value = this.buffer.readInt16BE(this.offset)
this.offset += INT16_SIZE
return value
}
canReadInt32() {
return Buffer.byteLength(this.buffer) - this.offset >= INT32_SIZE
}
readInt32() {
const value = this.buffer.readInt32BE(this.offset)
this.offset += INT32_SIZE
return value
}
canReadInt64() {
return Buffer.byteLength(this.buffer) - this.offset >= INT64_SIZE
}
readInt64() {
const lowBits = this.buffer.readInt32BE(this.offset + 4)
const highBits = this.buffer.readInt32BE(this.offset)
const value = new Long(lowBits, highBits)
this.offset += INT64_SIZE
return value
}
readString() {
const byteLength = this.readInt16()
if (byteLength === -1) {
return null
}
const stringBuffer = this.buffer.slice(this.offset, this.offset + byteLength)
const value = stringBuffer.toString('utf8')
this.offset += byteLength
return value
}
readVarIntString() {
const byteLength = this.readVarInt()
if (byteLength === -1) {
return null
}
const stringBuffer = this.buffer.slice(this.offset, this.offset + byteLength)
const value = stringBuffer.toString('utf8')
this.offset += byteLength
return value
}
canReadBytes(length) {
return Buffer.byteLength(this.buffer) - this.offset >= length
}
readBytes() {
const byteLength = this.readInt32()
if (byteLength === -1) {
return null
}
const stringBuffer = this.buffer.slice(this.offset, this.offset + byteLength)
this.offset += byteLength
return stringBuffer
}
readVarIntBytes() {
const byteLength = this.readVarInt()
if (byteLength === -1) {
return null
}
const stringBuffer = this.buffer.slice(this.offset, this.offset + byteLength)
this.offset += byteLength
return stringBuffer
}
readBoolean() {
return this.readInt8() === 1
}
readAll() {
const currentSize = Buffer.byteLength(this.buffer)
const remainingBytes = currentSize - this.offset
const newBuffer = Buffer.alloc(remainingBytes)
this.buffer.copy(newBuffer, 0, this.offset, currentSize)
return newBuffer
}
readArray(reader) {
const length = this.readInt32()
if (length === -1) {
return []
}
const array = []
for (let i = 0; i < length; i++) {
array.push(reader(this))
}
return array
}
readVarIntArray(reader) {
const length = this.readVarInt()
if (length === -1) {
return []
}
const array = []
for (let i = 0; i < length; i++) {
array.push(reader(this))
}
return array
}
async readArrayAsync(reader) {
const length = this.readInt32()
if (length === -1) {
return []
}
const array = []
for (let i = 0; i < length; i++) {
array.push(await reader(this))
}
return array
}
readVarInt() {
let currentByte
let result = 0
let i = 0
do {
currentByte = this.buffer[this.offset++]
result += (currentByte & OTHER_BITS) << i
i += 7
} while (currentByte >= MOST_SIGNIFICANT_BIT)
return Decoder.decodeZigZag(result)
}
readVarLong() {
let currentByte
let result = Long.fromInt(0)
let i = 0
do {
currentByte = this.buffer[this.offset++]
result = result.add(Long.fromInt(currentByte & OTHER_BITS).shiftLeft(i))
i += 7
} while (currentByte >= MOST_SIGNIFICANT_BIT)
return Decoder.decodeZigZag64(result)
}
slice(size) {
return new Decoder(this.buffer.slice(this.offset, this.offset + size))
}
forward(size) {
this.offset += size
}
}