bufferStuff/base/WriterBase.ts

71 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-10-24 12:04:07 +01:00
// Copyright (c) Holly Stubbs (tgpholly) - Licensed under MIT
// Check LICENSE in repository root for more information.
2023-10-24 12:04:32 +01:00
import { getBufferClass } from "./BufferShim";
2023-04-28 16:47:02 +01:00
export class WriterBase {
public buffer:Buffer;
public offset:number;
public readonly resizable:boolean;
public constructor(size:number = 0) {
2023-10-24 12:04:32 +01:00
this.buffer = getBufferClass().alloc(size);
2023-04-28 16:47:02 +01:00
this.offset = 0;
this.resizable = size === 0;
}
public toBuffer() {
return this.buffer;
}
public toString() {
return this.buffer.toString();
}
public writeBuffer(buffer:Buffer) {
this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length);
return this;
}
public writeUint8Array(array:Uint8Array) {
this.writeBuffer(Buffer.from(array));
return this;
}
public writeByte(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(1);
buffer.writeInt8(value);
this.writeBuffer(buffer);
} else {
this.buffer.writeInt8(value, this.offset);
this.offset++;
}
return this;
}
public writeUByte(value:number) {
if (this.resizable) {
const buffer = Buffer.alloc(1);
buffer.writeUInt8(value);
this.writeBuffer(buffer);
} else {
this.buffer.writeUInt8(value, this.offset);
this.offset++;
}
return this;
}
public writeBool(value:boolean|number) {
if (typeof(value) === "number") {
value = Boolean(value);
}
this.writeUByte(value ? 1 : 0);
return this;
}
}