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;
|
|
|
|
}
|
|
|
|
|
2023-11-07 00:14:03 +00:00
|
|
|
public get writeOffset() {
|
|
|
|
return this.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get length() {
|
|
|
|
return this.buffer.length;
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:47:02 +01:00
|
|
|
public toBuffer() {
|
|
|
|
return this.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public toString() {
|
|
|
|
return this.buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeBuffer(buffer:Buffer) {
|
2023-10-24 12:44:11 +01:00
|
|
|
this.buffer = getBufferClass().concat([this.buffer, buffer], this.buffer.length + buffer.length);
|
2024-11-26 15:22:51 +00:00
|
|
|
this.offset += buffer.length;
|
2023-04-28 16:47:02 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeUint8Array(array:Uint8Array) {
|
2023-10-24 12:44:11 +01:00
|
|
|
this.writeBuffer(getBufferClass().from(array));
|
2023-04-28 16:47:02 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeByte(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 12:44:11 +01:00
|
|
|
const buffer = getBufferClass().alloc(1);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeInt8(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeInt8(value, this.offset);
|
|
|
|
this.offset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeUByte(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 12:44:11 +01:00
|
|
|
const buffer = getBufferClass().alloc(1);
|
2023-04-28 16:47:02 +01:00
|
|
|
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;
|
|
|
|
}
|
2024-01-01 22:23:16 +00:00
|
|
|
|
|
|
|
public writeStringAsBytes(text:string) {
|
|
|
|
let buffer;
|
|
|
|
if (this.resizable) {
|
|
|
|
buffer = getBufferClass().alloc(text.length);
|
|
|
|
} else {
|
|
|
|
buffer = this.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
buffer.writeUInt8(text.charCodeAt(i), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2024-11-26 15:08:21 +00:00
|
|
|
|
|
|
|
public writeUShortString(text:string) {
|
|
|
|
this.writeUByte(text.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
this.writeUByte(text.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeShortString(text:string) {
|
|
|
|
this.writeByte(text.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
this.writeByte(text.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2023-04-28 16:47:02 +01:00
|
|
|
}
|