mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: paperdave <paperdave@users.noreply.github.com> Co-authored-by: Jarred-Sumner <Jarred-Sumner@users.noreply.github.com>
48 lines
920 B
TypeScript
48 lines
920 B
TypeScript
import { td } from "../shared";
|
|
|
|
export class DataViewReader {
|
|
view: DataView;
|
|
cursor: number;
|
|
|
|
constructor(view: DataView, cursor: number = 0) {
|
|
this.view = view;
|
|
this.cursor = cursor;
|
|
}
|
|
|
|
u32() {
|
|
const value = this.view.getUint32(this.cursor, true);
|
|
this.cursor += 4;
|
|
return value;
|
|
}
|
|
|
|
u16() {
|
|
const value = this.view.getUint16(this.cursor, true);
|
|
this.cursor += 2;
|
|
return value;
|
|
}
|
|
|
|
u8() {
|
|
const value = this.view.getUint8(this.cursor);
|
|
this.cursor += 1;
|
|
return value;
|
|
}
|
|
|
|
stringWithLength(byteLength: number) {
|
|
const str = td.decode(this.view.buffer.slice(this.cursor, this.cursor + byteLength));
|
|
this.cursor += byteLength;
|
|
return str;
|
|
}
|
|
|
|
string32() {
|
|
return this.stringWithLength(this.u32());
|
|
}
|
|
|
|
hasMoreData() {
|
|
return this.cursor < this.view.byteLength;
|
|
}
|
|
|
|
rest() {
|
|
return this.view.buffer.slice(this.cursor);
|
|
}
|
|
}
|