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-04-28 16:47:02 +01:00
|
|
|
import { IWriter } from "./IWriter";
|
2023-07-28 10:25:08 +01:00
|
|
|
import { WriterBase } from "../base/WriterBase";
|
2023-10-24 17:05:20 +01:00
|
|
|
import { getBufferClass } from "../base/BufferShim";
|
2023-04-28 16:47:02 +01:00
|
|
|
|
|
|
|
export class WriterBE extends WriterBase implements IWriter {
|
|
|
|
public writeShort(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(2);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeInt16BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeInt16BE(value, this.offset);
|
|
|
|
this.offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeUShort(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(2);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeUInt16BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeUInt16BE(value, this.offset);
|
|
|
|
this.offset += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeInt(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(4);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeInt32BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeInt32BE(value, this.offset);
|
|
|
|
this.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeUInt(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(4);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeUInt32BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeUInt32BE(value, this.offset);
|
|
|
|
this.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeLong(value:number|bigint) {
|
|
|
|
if (typeof(value) !== "bigint") {
|
|
|
|
value = BigInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(8);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeBigInt64BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeBigInt64BE(value, this.offset);
|
|
|
|
this.offset += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeULong(value:number|bigint) {
|
|
|
|
if (typeof(value) !== "bigint") {
|
|
|
|
value = BigInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(8);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeBigUint64BE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
2024-11-26 15:08:21 +00:00
|
|
|
this.buffer.writeBigUInt64BE(value, this.offset);
|
2023-04-28 16:47:02 +01:00
|
|
|
this.offset += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeFloat(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(4);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeFloatBE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeFloatBE(value, this.offset);
|
|
|
|
this.offset += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeDouble(value:number) {
|
|
|
|
if (this.resizable) {
|
2023-10-24 17:05:20 +01:00
|
|
|
const buffer = getBufferClass().alloc(8);
|
2023-04-28 16:47:02 +01:00
|
|
|
buffer.writeDoubleBE(value);
|
|
|
|
this.writeBuffer(buffer);
|
|
|
|
} else {
|
|
|
|
this.buffer.writeDoubleBE(value, this.offset);
|
|
|
|
this.offset += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-11-26 15:08:21 +00:00
|
|
|
public writeUString(text:string) {
|
|
|
|
this.writeUShort(text.length);
|
2023-04-28 16:47:02 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
this.writeUByte(text.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeString(text:string) {
|
2024-11-26 15:08:21 +00:00
|
|
|
this.writeShort(text.length);
|
2023-04-28 16:47:02 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
2024-11-26 15:08:21 +00:00
|
|
|
this.writeByte(text.charCodeAt(i));
|
2023-04-28 16:47:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2023-05-02 10:10:47 +01:00
|
|
|
|
2024-11-26 15:08:21 +00:00
|
|
|
public writeUString16(text:string) {
|
2023-05-02 10:10:47 +01:00
|
|
|
this.writeUShort(text.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
this.writeUShort(text.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2024-01-01 22:23:16 +00:00
|
|
|
|
2024-11-26 15:08:21 +00:00
|
|
|
public writeString16(text:string) {
|
|
|
|
this.writeShort(text.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
this.writeShort(text.charCodeAt(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-01-01 22:23:16 +00:00
|
|
|
public writeStringAsShorts(text:string) {
|
|
|
|
let buffer:Buffer;
|
|
|
|
if (this.resizable) {
|
|
|
|
buffer = getBufferClass().alloc(text.length * 2);
|
|
|
|
} else {
|
|
|
|
buffer = this.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
2024-11-26 15:08:21 +00:00
|
|
|
buffer.writeUInt16BE(text.charCodeAt(i), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public writeJavaUTF(text: string) {
|
2024-11-26 15:22:51 +00:00
|
|
|
const dataWriter = new WriterBE();
|
2024-11-26 15:08:21 +00:00
|
|
|
|
|
|
|
for (let i = 0; i < text.length; i++) {
|
|
|
|
const val = text.charCodeAt(i);
|
|
|
|
if (val === 0) {
|
2024-11-26 15:22:51 +00:00
|
|
|
dataWriter.writeByte(0xC0);
|
|
|
|
dataWriter.writeByte(0X80);
|
2024-11-26 15:08:21 +00:00
|
|
|
} else {
|
2024-11-26 15:22:51 +00:00
|
|
|
dataWriter.writeByte(val);
|
2024-11-26 15:08:21 +00:00
|
|
|
}
|
2024-01-01 22:23:16 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 15:22:51 +00:00
|
|
|
this.writeUShort(text.length);
|
|
|
|
this.writeBuffer(dataWriter.toBuffer());
|
|
|
|
|
2024-01-01 22:23:16 +00:00
|
|
|
return this;
|
|
|
|
}
|
2023-04-28 16:47:02 +01:00
|
|
|
}
|