Skip to content

Commit

Permalink
Extract read/write utf8-string into Buffalo (Koenkk#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbb authored Apr 6, 2021
1 parent 6d7adca commit 2119f46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/buffalo/buffalo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ class Buffalo {
return value;
}

public readUtf8String(length: number): string {
const value = this.buffer.toString('utf8', this.position, this.position + length);
this.position += length;
return value;
}

public writeUtf8String(value: string): void {
this.position += this.buffer.write(value, this.position, 'utf8');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public write(type: string, value: Value, options: Options): void {
if (type === 'UINT8') {
Expand Down
12 changes: 4 additions & 8 deletions src/zcl/buffaloZcl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,27 @@ class BuffaloZcl extends Buffalo {

return value;
} else {
const value = this.buffer.toString('utf8', this.position, this.position + length);
this.position += length;
return value;
return this.readUtf8String(length);
}
}

private writeCharStr(value: string | number[]): void {
if (typeof value === 'string') {
this.writeUInt8(value.length);
this.position += this.buffer.write(value, this.position, 'utf8');
this.writeUtf8String(value);
} else {
this.writeBuffer(value, value.length);
}
}

private readLongCharStr(): string {
const length = this.readUInt16();
const value = this.buffer.toString('utf8', this.position, this.position + length);
this.position += length;
return value;
return this.readUtf8String(length);
}

private writeLongCharStr(value: string): void {
this.writeUInt16(value.length);
this.position += this.buffer.write(value, this.position, 'utf8');
this.writeUtf8String(value);
}

private writeOctetStr(value: number[]): void {
Expand Down

0 comments on commit 2119f46

Please sign in to comment.