Skip to content

Commit

Permalink
💥 separate the read and get and the write and put methods of the Buff…
Browse files Browse the repository at this point in the history
…erStream

✨ support BufferInputStream and BufferOutputStream
  • Loading branch information
XiYang6666 committed Apr 19, 2024
1 parent e1131be commit 48c1b01
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
70 changes: 46 additions & 24 deletions src/bufferStream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataType } from "./dataType";

export class BufferStream {
private innerBuffer: Buffer;
export abstract class BaseBufferStream {
protected innerBuffer: Buffer;
get buffer() {
return this.innerBuffer;
}
Expand All @@ -13,28 +13,50 @@ export class BufferStream {
constructor(buffer: Buffer = Buffer.alloc(0)) {
this.innerBuffer = buffer;
}
}

export class BufferInputStream extends BaseBufferStream {
read(length: number): Buffer {
const value = this.innerBuffer.subarray(0, length);
this.innerBuffer = this.innerBuffer.subarray(length);
return value;
}

get<T>(type: DataType<T>): T {
const [value, offset] = type.read(this.innerBuffer, 0);
this.innerBuffer = this.innerBuffer.subarray(offset);
return value;
}
}

export class BufferOutputStream extends BaseBufferStream {
write(buffer: Buffer) {
this.innerBuffer = Buffer.concat([this.innerBuffer, buffer]);
}

put<T>(type: DataType<T>, value: T): void {
this.innerBuffer = Buffer.concat([this.innerBuffer, type.write(value)]);
}
}

export class BufferStream extends BaseBufferStream implements BufferInputStream, BufferOutputStream {
read(length: number): Buffer {
const value = this.innerBuffer.subarray(0, length);
this.innerBuffer = this.innerBuffer.subarray(length);
return value;
}

get<T>(type: DataType<T>): T {
const [value, offset] = type.read(this.innerBuffer, 0);
this.innerBuffer = this.innerBuffer.subarray(offset);
return value;
}

write(buffer: Buffer) {
this.innerBuffer = Buffer.concat([this.innerBuffer, buffer]);
}

read<T>(type: DataType<T>): T;
read(length: number): Buffer;
read<T>(type_or_length: DataType<T> | number): T | Buffer {
if (typeof type_or_length === "number") {
const value = this.innerBuffer.subarray(0, type_or_length);
this.innerBuffer = this.innerBuffer.subarray(type_or_length);
return value;
} else {
const [value, offset] = type_or_length.read(this.innerBuffer, 0);
this.innerBuffer = this.innerBuffer.subarray(offset);
return value;
}
}

write<T>(type: DataType<T>, value: T): void;
write(buffer: Buffer): void;
write<T>(type_or_buffer: DataType<T> | Buffer, value?: T) {
if (type_or_buffer instanceof Buffer) {
this.innerBuffer = Buffer.concat([this.innerBuffer, type_or_buffer]);
} else {
this.innerBuffer = Buffer.concat([this.innerBuffer, type_or_buffer.write(value as T)]);
}
put<T>(type: DataType<T>, value: T): void {
this.innerBuffer = Buffer.concat([this.innerBuffer, type.write(value)]);
}
}
4 changes: 2 additions & 2 deletions test/bufferStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ test("Test the read and write of bufferStream", () => {
console.time(`BufferStream reads and writes ${length} times`);

for (let i of array) {
bs.write(BaseTypes.VarInt32, i);
bs.put(BaseTypes.VarInt32, i);
}

const newArray: number[] = [];
for (let i = 0; i < length; i++) {
newArray.push(bs.read(BaseTypes.VarInt32));
newArray.push(bs.get(BaseTypes.VarInt32));
}

console.timeEnd(`BufferStream reads and writes ${length} times`);
Expand Down

0 comments on commit 48c1b01

Please sign in to comment.