Skip to content

Commit

Permalink
1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tgpholly committed Nov 26, 2024
1 parent ceffc3e commit 05162e0
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 28 deletions.
13 changes: 12 additions & 1 deletion base/ReaderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class ReaderBase {
return Boolean(this.readUByte());
}

public readShortString() {
public readUShortString() {
const length = this.readUByte();
let text = "";

Expand All @@ -62,6 +62,17 @@ export class ReaderBase {
return text;
}

public readShortString() {
const length = this.readByte();
let text = "";

for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readByte());
}

return text;
}

public readBytesAsString(bytesToRead:number) {
let text = "";

Expand Down
20 changes: 20 additions & 0 deletions base/WriterBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,24 @@ export class WriterBase {

return this;
}

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;
}
}
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bufferstuff",
"version": "1.5.1",
"version": "1.6.0",
"description": "A set of utility classes for reading and writing binary data in NodeJS and the browser",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions readers/IReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface IReader {
readByte(): number,
readUByte(): number,
readBool(): boolean,
readUShortString(): string,
readShortString(): string,
readBytesAsString(bytesToRead:number): string,
readShort(): number,
Expand All @@ -19,7 +20,9 @@ export interface IReader {
readULong(): bigint,
readFloat(): number,
readDouble(): number,
readUString(): string,
readString(): string,
readShortsAsString(shortsToRead:number): string,
readUString16(): string,
readString16(): string,
}
28 changes: 25 additions & 3 deletions readers/ReaderBE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ReaderBE extends ReaderBase implements IReader {
}

public readULong() {
const value = this.buffer.readBigUint64BE(this.offset);
const value = this.buffer.readBigUInt64BE(this.offset);
this.offset += 8;
return value;
}
Expand All @@ -53,7 +53,7 @@ export class ReaderBE extends ReaderBase implements IReader {
return value;
}

public readString() {
public readUString() {
const length = this.readUShort();
let text = "";

Expand All @@ -64,7 +64,18 @@ export class ReaderBE extends ReaderBase implements IReader {
return text;
}

public readString16() {
public readString() {
const length = this.readShort();
let text = "";

for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readByte());
}

return text;
}

public readUString16() {
const length = this.readUShort();
let text = "";

Expand All @@ -75,6 +86,17 @@ export class ReaderBE extends ReaderBase implements IReader {
return text;
}

public readString16() {
const length = this.readShort();
let text = "";

for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readUShort());
}

return text;
}

public readShortsAsString(shortsToRead:number) {
let text = "";

Expand Down
28 changes: 25 additions & 3 deletions readers/ReaderLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ReaderLE extends ReaderBase implements IReader {
}

public readULong() {
const value = this.buffer.readBigUint64LE(this.offset);
const value = this.buffer.readBigUInt64LE(this.offset);
this.offset += 8;
return value;
}
Expand All @@ -53,7 +53,7 @@ export class ReaderLE extends ReaderBase implements IReader {
return value;
}

public readString() {
public readUString() {
const length = this.readUShort();
let text:string = "";

Expand All @@ -63,8 +63,19 @@ export class ReaderLE extends ReaderBase implements IReader {

return text;
}

public readString() {
const length = this.readShort();
let text:string = "";

for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readByte());
}

return text;
}

public readString16() {
public readUString16() {
const length = this.readUShort();
let text:string = "";

Expand All @@ -75,6 +86,17 @@ export class ReaderLE extends ReaderBase implements IReader {
return text;
}

public readString16() {
const length = this.readShort();
let text:string = "";

for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readShort());
}

return text;
}

public readShortsAsString(shortsToRead:number) {
let text = "";

Expand Down
6 changes: 5 additions & 1 deletion writers/IWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ export interface IWriter {
writeULong(value:number|bigint): IWriter,
writeFloat(value:number): IWriter,
writeDouble(value:number): IWriter,
writeUShortString(text:string): IWriter,
writeShortString(text:string): IWriter,
writeUString(text:string): IWriter,
writeString(text:string): IWriter,
writeUString16(text:string): IWriter,
writeString16(text:string): IWriter,
writeStringAsShorts(text:string): IWriter
writeStringAsShorts(text:string): IWriter,
writeJavaUTF(text:string): IWriter
}
40 changes: 33 additions & 7 deletions writers/WriterBE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class WriterBE extends WriterBase implements IWriter {
buffer.writeBigUint64BE(value);
this.writeBuffer(buffer);
} else {
this.buffer.writeBigUint64BE(value, this.offset);
this.buffer.writeBigUInt64BE(value, this.offset);
this.offset += 8;
}

Expand Down Expand Up @@ -118,8 +118,8 @@ export class WriterBE extends WriterBase implements IWriter {
return this;
}

public writeShortString(text:string) {
this.writeUByte(text.length);
public writeUString(text:string) {
this.writeUShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
Expand All @@ -129,16 +129,16 @@ export class WriterBE extends WriterBase implements IWriter {
}

public writeString(text:string) {
this.writeUShort(text.length);
this.writeShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
this.writeByte(text.charCodeAt(i));
}

return this;
}

public writeString16(text:string) {
public writeUString16(text:string) {
this.writeUShort(text.length);

for (let i = 0; i < text.length; i++) {
Expand All @@ -148,6 +148,16 @@ export class WriterBE extends WriterBase implements IWriter {
return this;
}

public writeString16(text:string) {
this.writeShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeShort(text.charCodeAt(i));
}

return this;
}

public writeStringAsShorts(text:string) {
let buffer:Buffer;
if (this.resizable) {
Expand All @@ -157,7 +167,23 @@ export class WriterBE extends WriterBase implements IWriter {
}

for (let i = 0; i < text.length; i++) {
buffer.writeUint16BE(text.charCodeAt(i), i);
buffer.writeUInt16BE(text.charCodeAt(i), i);
}

return this;
}

public writeJavaUTF(text: string) {
this.writeUShort(text.length);

for (let i = 0; i < text.length; i++) {
const val = text.charCodeAt(i);
if (val === 0) {
this.writeByte(0xC0);
this.writeByte(0X80);
} else {
this.writeByte(val);
}
}

return this;
Expand Down
30 changes: 23 additions & 7 deletions writers/WriterLE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class WriterLE extends WriterBase implements IWriter {
buffer.writeBigUint64LE(value);
this.writeBuffer(buffer);
} else {
this.buffer.writeBigUint64LE(value, this.offset);
this.buffer.writeBigUInt64LE(value, this.offset);
this.offset += 8;
}

Expand Down Expand Up @@ -117,8 +117,8 @@ export class WriterLE extends WriterBase implements IWriter {
return this;
}

public writeShortString(text:string) {
this.writeUByte(text.length);
public writeUString(text:string) {
this.writeUShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
Expand All @@ -128,16 +128,16 @@ export class WriterLE extends WriterBase implements IWriter {
}

public writeString(text:string) {
this.writeUShort(text.length);
this.writeShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i));
this.writeByte(text.charCodeAt(i));
}

return this;
}

public writeString16(text:string) {
public writeUString16(text:string) {
this.writeUShort(text.length);

for (let i = 0; i < text.length; i++) {
Expand All @@ -147,6 +147,16 @@ export class WriterLE extends WriterBase implements IWriter {
return this;
}

public writeString16(text:string) {
this.writeShort(text.length);

for (let i = 0; i < text.length; i++) {
this.writeShort(text.charCodeAt(i));
}

return this;
}

public writeStringAsShorts(text:string) {
let buffer:Buffer;
if (this.resizable) {
Expand All @@ -156,9 +166,15 @@ export class WriterLE extends WriterBase implements IWriter {
}

for (let i = 0; i < text.length; i++) {
buffer.writeUint16LE(text.charCodeAt(i), i);
buffer.writeUInt16LE(text.charCodeAt(i), i);
}

return this;
}

public writeJavaUTF(text: string) {
throw "Not implemented in Little-Endian Writer";

return this;
}
}

0 comments on commit 05162e0

Please sign in to comment.