Skip to content

Commit fcf78c9

Browse files
committed
feat(loki-buffer): add example
1 parent 30f4a56 commit fcf78c9

File tree

3 files changed

+79
-81
lines changed

3 files changed

+79
-81
lines changed

packages/loki-buffer/src/ByteArray.ts

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { logError } from './log';
2-
31
const BIG: number = 4294967296;
42

53
type RDecodeType = { decode(bytes: ByteArray): void };
@@ -19,11 +17,11 @@ type REleType = ByteType | RVoNewType | RVoCreateType;
1917
type WEleType = ByteType | WVoType;
2018

2119
export class ByteArray {
22-
private _len_ = 0;
20+
private _len = 0;
2321

24-
public _pos_ = 0;
22+
private _pos = 0;
2523

26-
private _byteView_: Uint8Array = null as never;
24+
private _byteView: Uint8Array = null as never;
2725

2826
powTwo = false;
2927

@@ -42,31 +40,31 @@ export class ByteArray {
4240
private resizeBuffer(len: number): void {
4341
try {
4442
var newByteView: any = new Uint8Array(len);
45-
if (this._byteView_ != null) {
46-
if (this._byteView_.length <= len) newByteView.setarr(this._byteView_);
47-
else newByteView.setarr(this._byteView_.subarray(0, len));
43+
if (this._byteView != null) {
44+
if (this._byteView.length <= len) newByteView.setarr(this._byteView);
45+
else newByteView.setarr(this._byteView.subarray(0, len));
4846
}
49-
this._byteView_ = newByteView;
47+
this._byteView = newByteView;
5048
this._data_ = new DataView(newByteView.buffer);
5149
} catch (err: any) {
5250
throw '__resizeBuffer err:' + len;
5351
}
5452
}
5553

5654
setUint8Array(data: Uint8Array): void {
57-
this._byteView_ = data;
55+
this._byteView = data;
5856
this._data_ = new DataView(data.buffer);
59-
this._len_ = data.byteLength;
60-
this._pos_ = 0;
57+
this._len = data.byteLength;
58+
this._pos = 0;
6159
}
6260

6361
ensureWrite(lengthToEnsure: number): void {
64-
if (this._len_ < lengthToEnsure) this._len_ = lengthToEnsure;
62+
if (this._len < lengthToEnsure) this._len = lengthToEnsure;
6563
}
6664

6765
clear(): void {
68-
this._pos_ = 0;
69-
this._len_ = 0;
66+
this._pos = 0;
67+
this._len = 0;
7068
}
7169

7270
_get(pos: number): number {
@@ -78,47 +76,47 @@ export class ByteArray {
7876
}
7977

8078
_byteAt_(index: number): number {
81-
return this._byteView_[index];
79+
return this._byteView[index];
8280
}
8381

8482
_byteSet_(index: number, value: number): void {
8583
this.ensureWrite(index + 1);
86-
this._byteView_[index] = value;
84+
this._byteView[index] = value;
8785
}
8886

8987
readBoolean(): boolean {
9088
return this.readByte() !== 0;
9189
}
9290

9391
readUByte(): number {
94-
return this._data_.getUint8(this._pos_++);
92+
return this._data_.getUint8(this._pos++);
9593
}
9694

9795
readByte(): number {
98-
return this._data_.getInt8(this._pos_++);
96+
return this._data_.getInt8(this._pos++);
9997
}
10098

10199
readUInt(): number {
102-
const uInt = this._data_.getUint32(this._pos_);
103-
this._pos_ += 4;
100+
const uInt = this._data_.getUint32(this._pos);
101+
this._pos += 4;
104102
return Math.floor(uInt);
105103
}
106104

107105
readInt(): number {
108-
const tInt = this._data_.getInt32(this._pos_);
109-
this._pos_ += 4;
106+
const tInt = this._data_.getInt32(this._pos);
107+
this._pos += 4;
110108
return tInt;
111109
}
112110

113111
readShort(): number {
114-
const short = this._data_.getInt16(this._pos_);
115-
this._pos_ += 2;
112+
const short = this._data_.getInt16(this._pos);
113+
this._pos += 2;
116114
return short;
117115
}
118116

119117
readUShort(): number {
120-
const value = this._data_.getUint16(this._pos_);
121-
this._pos_ += 2;
118+
const value = this._data_.getUint16(this._pos);
119+
this._pos += 2;
122120
return value;
123121
}
124122

@@ -129,14 +127,14 @@ export class ByteArray {
129127
}
130128

131129
readFloat(): number {
132-
const float = this._data_.getInt32(this._pos_) / 1000;
133-
this._pos_ += 4;
130+
const float = this._data_.getInt32(this._pos) / 1000;
131+
this._pos += 4;
134132
return float;
135133
}
136134

137135
readDouble(): number {
138-
const double = this._data_.getFloat64(this._pos_);
139-
this._pos_ += 8;
136+
const double = this._data_.getFloat64(this._pos);
137+
this._pos += 8;
140138
return double;
141139
}
142140

@@ -150,34 +148,34 @@ export class ByteArray {
150148

151149
readUTFBytes(len = -1): string {
152150
let value = '';
153-
const max: number = this._pos_ + len;
151+
const max: number = this._pos + len;
154152
let c: number, c2: number, c3: number;
155153
const f = String.fromCharCode;
156-
while (this._pos_ < max) {
157-
c = this._data_.getUint8(this._pos_++);
154+
while (this._pos < max) {
155+
c = this._data_.getUint8(this._pos++);
158156
if (c < 0x80) {
159157
if (c !== 0) {
160158
value += f(c);
161159
}
162160
} else if (c < 0xe0) {
163161
value += f(
164-
((c & 0x3f) << 6) | (this._data_.getUint8(this._pos_++) & 0x7f)
162+
((c & 0x3f) << 6) | (this._data_.getUint8(this._pos++) & 0x7f)
165163
);
166164
} else if (c < 0xf0) {
167-
c2 = this._data_.getUint8(this._pos_++);
165+
c2 = this._data_.getUint8(this._pos++);
168166
value += f(
169167
((c & 0x1f) << 12) |
170168
((c2 & 0x7f) << 6) |
171-
(this._data_.getUint8(this._pos_++) & 0x7f)
169+
(this._data_.getUint8(this._pos++) & 0x7f)
172170
);
173171
} else {
174-
c2 = this._data_.getUint8(this._pos_++);
175-
c3 = this._data_.getUint8(this._pos_++);
172+
c2 = this._data_.getUint8(this._pos++);
173+
c3 = this._data_.getUint8(this._pos++);
176174
value += f(
177175
((c & 0x0f) << 18) |
178176
((c2 & 0x7f) << 12) |
179177
((c3 << 6) & 0x7f) |
180-
(this._data_.getUint8(this._pos_++) & 0x7f)
178+
(this._data_.getUint8(this._pos++) & 0x7f)
181179
);
182180
}
183181
}
@@ -190,44 +188,44 @@ export class ByteArray {
190188
}
191189

192190
writeUByte(value: number): void {
193-
this.ensureWrite(this._pos_ + 1);
194-
this._data_.setUint8(this._pos_, value);
195-
this._pos_ += 1;
191+
this.ensureWrite(this._pos + 1);
192+
this._data_.setUint8(this._pos, value);
193+
this._pos += 1;
196194
}
197195

198196
writeByte(value: number): void {
199-
this.ensureWrite(this._pos_ + 1);
200-
this._data_.setInt8(this._pos_, value);
201-
this._pos_ += 1;
197+
this.ensureWrite(this._pos + 1);
198+
this._data_.setInt8(this._pos, value);
199+
this._pos += 1;
202200
}
203201

204202
writeInt(value: number): void {
205-
this.ensureWrite(this._pos_ + 4);
206-
this._data_.setInt32(this._pos_, value);
207-
this._pos_ += 4;
203+
this.ensureWrite(this._pos + 4);
204+
this._data_.setInt32(this._pos, value);
205+
this._pos += 4;
208206
}
209207

210208
writeUInt(value: number): void {
211-
this.ensureWrite(this._pos_ + 4);
212-
this._data_.setUint32(this._pos_, value);
213-
this._pos_ += 4;
209+
this.ensureWrite(this._pos + 4);
210+
this._data_.setUint32(this._pos, value);
211+
this._pos += 4;
214212
}
215213

216214
writeShort(value: number): void {
217-
this.ensureWrite(this._pos_ + 2);
218-
this._data_.setInt16(this._pos_, value);
219-
this._pos_ += 2;
215+
this.ensureWrite(this._pos + 2);
216+
this._data_.setInt16(this._pos, value);
217+
this._pos += 2;
220218
}
221219

222220
writeUShort(value: number): void {
223-
this.ensureWrite(this._pos_ + 2);
224-
this._data_.setUint16(this._pos_, value);
225-
this._pos_ += 2;
221+
this.ensureWrite(this._pos + 2);
222+
this._data_.setUint16(this._pos, value);
223+
this._pos += 2;
226224
}
227225

228226
writeLong(value: number): void {
229227
if (value != 0 && (value > Number.MAX_VALUE || value < Number.MIN_VALUE)) {
230-
logError('writeLong error -- Out of bounds');
228+
console.log('writeLong error -- Out of bounds');
231229
}
232230
let head: number;
233231
let end: number;
@@ -248,16 +246,16 @@ export class ByteArray {
248246
}
249247

250248
writeFloat(value: number): void {
251-
this.ensureWrite(this._pos_ + 4);
249+
this.ensureWrite(this._pos + 4);
252250
value = value * 1000;
253-
this._data_.setInt32(this._pos_, value);
254-
this._pos_ += 4;
251+
this._data_.setInt32(this._pos, value);
252+
this._pos += 4;
255253
}
256254

257255
writeDouble(value: number): void {
258-
this.ensureWrite(this._pos_ + 8);
259-
this._data_.setFloat64(this._pos_, value);
260-
this._pos_ += 8;
256+
this.ensureWrite(this._pos + 8);
257+
this._data_.setFloat64(this._pos, value);
258+
this._pos += 8;
261259
}
262260

263261
writeString(value: string): void {
@@ -266,7 +264,7 @@ export class ByteArray {
266264
}
267265

268266
writeUTFBytes(value: string): void {
269-
this.ensureWrite(this._pos_ + value.length * 4);
267+
this.ensureWrite(this._pos + value.length * 4);
270268
for (let i = 0, sz: number = value.length; i < sz; i++) {
271269
const c: number = value.charCodeAt(i);
272270
if (c <= 0x7f) {
@@ -285,7 +283,7 @@ export class ByteArray {
285283
this.writeByte(0x80 | (c & 63));
286284
}
287285
}
288-
this._len_ = this._pos_;
286+
this._len = this._pos;
289287
}
290288

291289
static _getUTFBytesCount(value: string): number {
@@ -401,10 +399,10 @@ export class ByteArray {
401399
writeArrayBuffer(arraybuffer: ArrayBuffer, offset = 0, length = 0): void {
402400
if (offset < 0 || length < 0) throw new Error('writeArrayBuffer error');
403401
const rlen = length === 0 ? arraybuffer.byteLength - offset : length;
404-
this.ensureWrite(this._pos_ + rlen);
402+
this.ensureWrite(this._pos + rlen);
405403
const uint8Array = new Uint8Array(arraybuffer);
406-
this._byteView_.set(uint8Array.subarray(offset, offset + rlen), this._pos_);
407-
this._pos_ += rlen;
404+
this._byteView.set(uint8Array.subarray(offset, offset + rlen), this._pos);
405+
this._pos += rlen;
408406
}
409407
}
410408

packages/loki-buffer/src/log.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/loki-buffer/test/blah.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ test('blah', () => {
77
const writer = new ByteArray(writerBuf);
88
writer.writeBoolean(true);
99
writer.writeByte(1);
10+
writer.writeUByte(1);
1011
writer.writeDouble(2.1);
1112
writer.writeFloat(3.1);
1213
writer.writeInt(4);
14+
writer.writeUInt(4);
1315
writer.writeShort(5);
16+
writer.writeUShort(6);
1417
writer.writeBoolean(true);
1518
writer.writeLong(123);
1619
writer.writeString('hhh');
20+
const ids = [1, 1];
21+
writer.writeList(ids, 4, 4);
1722

1823
writer._set(0, 1);
1924
expect(writer._get(0)).toBe(1);
@@ -24,13 +29,19 @@ test('blah', () => {
2429
const reader = new ByteArray(readerBuf);
2530
expect(reader.readBoolean()).toBe(true);
2631
expect(reader.readByte()).toBe(1);
32+
expect(reader.readUByte()).toBe(1);
2733
expect(reader.readDouble()).toBe(2.1);
2834
expect(reader.readFloat()).toBe(3.1);
2935
expect(reader.readInt()).toBe(4);
36+
expect(reader.readUInt()).toBe(4);
3037
expect(reader.readShort()).toBe(5);
38+
expect(reader.readUShort()).toBe(6);
3139
expect(reader.readBoolean()).toBe(true);
3240
expect(reader.readLong()).toBe(123);
3341
expect(reader.readString()).toBe('hhh');
42+
const arr = reader.readList(4, 4);
43+
expect(arr[0]).toBe(1);
44+
expect(arr[1]).toBe(1);
3445
});
3546

3647
test.todo('support float!');

0 commit comments

Comments
 (0)