Skip to content

Commit 3866e31

Browse files
committed
Implemented u32.
1 parent 93f078f commit 3866e31

11 files changed

+196
-118
lines changed

src/io/bufferIOBase.ts

+40-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,49 @@
11
import { isBigEndian } from '../util';
22

33
export type BufferIOOptions = {
4-
/**
5-
* @default 0
6-
*/
7-
byteOffset: number;
8-
/**
9-
* @default 'system'
10-
*/
11-
endianness: 'big' | 'little' | 'system';
4+
/**
5+
* @default 0
6+
*/
7+
byteOffset: number;
8+
/**
9+
* @default 'system'
10+
*/
11+
endianness: 'big' | 'little' | 'system';
1212
};
1313

1414
export class BufferIOBase {
15-
protected readonly uint8View: Uint8Array;
16-
protected readonly helperIntView: Int32Array;
17-
protected readonly helperFloatView: Float32Array;
18-
protected readonly helperByteView: Uint8Array;
19-
protected readonly switchEndianness: boolean;
20-
21-
protected byteOffset = 0;
22-
23-
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
24-
if (typeof Buffer !== 'undefined' && buffer instanceof Buffer) {
25-
// Getting rid of the outer shell, which causes the Uint8Array line to create a copy, instead of a view.
26-
buffer = buffer.buffer;
27-
}
28-
29-
this.uint8View = new Uint8Array(buffer, 0);
30-
this.byteOffset = options?.byteOffset ?? 0;
31-
32-
const helperBuffer = new ArrayBuffer(4);
33-
this.helperIntView = new Int32Array(helperBuffer);
34-
this.helperFloatView = new Float32Array(helperBuffer);
35-
this.helperByteView = new Uint8Array(helperBuffer);
36-
37-
const isSystemBigEndian = isBigEndian();
38-
const endianness = options?.endianness ?? 'system';
39-
this.switchEndianness =
40-
(endianness === 'big' && !isSystemBigEndian) ||
41-
(endianness === 'little' && isSystemBigEndian);
42-
}
15+
protected readonly uint8View: Uint8Array;
16+
protected readonly helperInt32View: Int32Array;
17+
protected readonly helperUint32View: Uint32Array;
18+
protected readonly helperFloatView: Float32Array;
19+
protected readonly helperByteView: Uint8Array;
20+
protected readonly switchEndianness: boolean;
21+
22+
protected byteOffset = 0;
4323

44-
get currentByteOffset() {
45-
return this.byteOffset;
24+
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
25+
if (typeof Buffer !== 'undefined' && buffer instanceof Buffer) {
26+
// Getting rid of the outer shell, which causes the Uint8Array line to create a copy, instead of a view.
27+
buffer = buffer.buffer;
4628
}
29+
30+
this.uint8View = new Uint8Array(buffer, 0);
31+
this.byteOffset = options?.byteOffset ?? 0;
32+
33+
const helperBuffer = new ArrayBuffer(4);
34+
this.helperInt32View = new Int32Array(helperBuffer);
35+
this.helperUint32View = new Uint32Array(helperBuffer);
36+
this.helperFloatView = new Float32Array(helperBuffer);
37+
this.helperByteView = new Uint8Array(helperBuffer);
38+
39+
const isSystemBigEndian = isBigEndian();
40+
const endianness = options?.endianness ?? 'system';
41+
this.switchEndianness =
42+
(endianness === 'big' && !isSystemBigEndian) ||
43+
(endianness === 'little' && isSystemBigEndian);
44+
}
45+
46+
get currentByteOffset() {
47+
return this.byteOffset;
48+
}
4749
}

src/io/bufferReader.ts

+38-29
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,52 @@ import { BufferIOBase, BufferIOOptions } from './bufferIOBase';
22
import { ISerialInput } from './types';
33

44
export class BufferReader extends BufferIOBase implements ISerialInput {
5-
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
6-
super(buffer, options);
5+
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
6+
super(buffer, options);
7+
}
8+
9+
private copyInputToHelper(bytes: number) {
10+
for (let i = 0; i < bytes; ++i) {
11+
this.helperByteView[this.switchEndianness ? bytes - 1 - i : i] =
12+
this.uint8View[this.byteOffset++];
713
}
14+
}
815

9-
readBool() {
10-
return this.uint8View[this.byteOffset++] !== 0;
11-
}
16+
readBool() {
17+
return this.uint8View[this.byteOffset++] !== 0;
18+
}
1219

13-
readByte() {
14-
return this.uint8View[this.byteOffset++];
15-
}
20+
readByte() {
21+
return this.uint8View[this.byteOffset++];
22+
}
1623

17-
readFloat() {
18-
for (let i = 0; i < 4; ++i) {
19-
this.helperByteView[this.switchEndianness ? (3-i) : i] = this.uint8View[this.byteOffset++];
20-
}
24+
readFloat32() {
25+
this.copyInputToHelper(4);
2126

22-
return this.helperFloatView[0];
23-
}
27+
return this.helperFloatView[0];
28+
}
2429

25-
readInt() {
26-
for (let i = 0; i < 4; ++i) {
27-
this.helperByteView[this.switchEndianness ? (3-i) : i] = this.uint8View[this.byteOffset++];
28-
}
30+
readInt32() {
31+
this.copyInputToHelper(4);
2932

30-
return this.helperIntView[0];
31-
}
33+
return this.helperInt32View[0];
34+
}
35+
36+
readUint32() {
37+
this.copyInputToHelper(4);
3238

33-
readString() {
34-
let contents = '';
39+
return this.helperUint32View[0];
40+
}
3541

36-
let char = String.fromCharCode(this.uint8View[this.byteOffset++]);
37-
while (char !== '\0') {
38-
contents += char;
39-
char = String.fromCharCode(this.uint8View[this.byteOffset++]);
40-
}
42+
readString() {
43+
let contents = '';
4144

42-
return contents;
45+
let char = String.fromCharCode(this.uint8View[this.byteOffset++]);
46+
while (char !== '\0') {
47+
contents += char;
48+
char = String.fromCharCode(this.uint8View[this.byteOffset++]);
4349
}
44-
}
50+
51+
return contents;
52+
}
53+
}

src/io/bufferReaderWriter.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('BufferWriter/BufferReader', () => {
2020

2121
// Writing the ints
2222
for (const int of intList) {
23-
writer.writeInt(int);
23+
writer.writeInt32(int);
2424
}
2525

2626
// Expecting specific buffer offset
@@ -29,7 +29,7 @@ describe('BufferWriter/BufferReader', () => {
2929
// Reading the ints
3030
const reader = new BufferReader(buffer);
3131
for (let i = 0; i < intList.length; ++i) {
32-
expect(reader.readInt()).to.equal(intList[i]);
32+
expect(reader.readInt32()).to.equal(intList[i]);
3333
}
3434
});
3535

@@ -48,16 +48,16 @@ describe('BufferWriter/BufferReader', () => {
4848

4949
// Writing the ints
5050
for (const float of floatList) {
51-
writer.writeFloat(float);
51+
writer.writeFloat32(float);
5252
}
5353

5454
// Expecting specific buffer offset
5555
expect(writer.currentByteOffset).to.equal(floatList.length * 4);
5656

57-
// Reading the ints
57+
// Reading the floats
5858
const reader = new BufferReader(buffer);
5959
for (let i = 0; i < floatList.length; ++i) {
60-
expect(reader.readFloat()).to.be.closeTo(floatList[i], 0.001);
60+
expect(reader.readFloat32()).to.be.closeTo(floatList[i], 0.001);
6161
}
6262
});
6363
});

src/io/bufferWriter.ts

+35-25
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,48 @@ import { BufferIOBase, BufferIOOptions } from './bufferIOBase';
22
import { ISerialOutput } from './types';
33

44
export class BufferWriter extends BufferIOBase implements ISerialOutput {
5-
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
6-
super(buffer, options);
7-
}
5+
constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
6+
super(buffer, options);
7+
}
88

9-
writeBool(value: boolean) {
10-
this.uint8View[this.byteOffset++] = value ? 1 : 0;
11-
}
9+
private copyHelperToOutput(bytes: number) {
10+
for (let i = 0; i < bytes; ++i)
11+
this.uint8View[this.byteOffset++] =
12+
this.helperByteView[this.switchEndianness ? bytes - 1 - i : i];
13+
}
1214

13-
writeByte(value: number) {
14-
this.uint8View[this.byteOffset++] = Math.floor(value) % 256;
15-
}
15+
writeBool(value: boolean) {
16+
this.uint8View[this.byteOffset++] = value ? 1 : 0;
17+
}
1618

17-
writeInt(value: number) {
18-
this.helperIntView[0] = Math.floor(value);
19+
writeByte(value: number) {
20+
this.uint8View[this.byteOffset++] = Math.floor(value) % 256;
21+
}
1922

20-
for (let i = 0; i < 4; ++i)
21-
this.uint8View[this.byteOffset++] = this.helperByteView[this.switchEndianness ? (3-i) : i];
22-
}
23+
writeInt32(value: number) {
24+
this.helperInt32View[0] = Math.floor(value);
2325

24-
writeFloat(value: number) {
25-
this.helperFloatView[0] = value;
26+
this.copyHelperToOutput(4);
27+
}
2628

27-
for (let i = 0; i < 4; ++i)
28-
this.uint8View[this.byteOffset++] = this.helperByteView[this.switchEndianness ? (3-i) : i];
29-
}
29+
writeUint32(value: number) {
30+
this.helperUint32View[0] = Math.floor(value);
31+
32+
this.copyHelperToOutput(4);
33+
}
3034

31-
writeString(value: string) {
32-
for (let i = 0; i < value.length; ++i) {
33-
this.uint8View[this.byteOffset++] = value.charCodeAt(i);
34-
}
35+
writeFloat32(value: number) {
36+
this.helperFloatView[0] = value;
3537

36-
// Extra null character
37-
this.uint8View[this.byteOffset++] = 0;
38+
this.copyHelperToOutput(4);
39+
}
40+
41+
writeString(value: string) {
42+
for (let i = 0; i < value.length; ++i) {
43+
this.uint8View[this.byteOffset++] = value.charCodeAt(i);
3844
}
45+
46+
// Extra null character
47+
this.uint8View[this.byteOffset++] = 0;
48+
}
3949
}

src/io/types.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
export interface ISerialInput {
22
readBool(): boolean;
33
readByte(): number;
4-
readInt(): number;
5-
readFloat(): number;
4+
readInt32(): number;
5+
readUint32(): number;
6+
readFloat32(): number;
67
readString(): string;
78
readonly currentByteOffset: number;
89
}
910

1011
export interface ISerialOutput {
1112
writeBool(value: boolean): void;
1213
writeByte(value: number): void;
13-
writeInt(value: number): void;
14-
writeFloat(value: number): void;
14+
writeInt32(value: number): void;
15+
writeUint32(value: number): void;
16+
writeFloat32(value: number): void;
1517
writeString(value: string): void;
1618
readonly currentByteOffset: number;
1719
}

src/structure/array.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class ArraySchema<T> extends Schema<T[]> {
1818
}
1919

2020
write(output: ISerialOutput, values: T[]): void {
21-
output.writeInt(values.length);
21+
output.writeUint32(values.length);
2222

2323
for (const value of values) {
2424
this.elementType.write(output, value);
@@ -28,7 +28,7 @@ export class ArraySchema<T> extends Schema<T[]> {
2828
read(input: ISerialInput): T[] {
2929
const array = [];
3030

31-
const len = input.readInt();
31+
const len = input.readUint32();
3232

3333
for (let i = 0; i < len; ++i) {
3434
array.push(this.elementType.read(input));

0 commit comments

Comments
 (0)