/** * An abstract class that wraps a "bytes" value, such as a Buffer value in Node.js or an ArrayBuffer in the browser. * Its purpose is to create a layer of compatibility for code that should be used both in Node.js and in the browser. */ export abstract class BytesView { /** * The constructor of [[BytesView]] * @param _buffer The buffer that should be wrapped. */ constructor(protected _buffer: T) {} /** * Returns the buffer. * @returns The buffer. */ get buffer(): T { return this._buffer; } /** * Sets the buffer. * @param value The buffer that will be assigned. */ set buffer(value: T) { this._buffer = value; } /** * Returns the length of the buffer. * @returns The length of the buffer. */ abstract get length(): number; /** * Returns a new [[BytesView]] instance given a string or an array of numbers. * @param value The value that will be converted into a [[BytesView]] instance. * @returns A new [[BytesView]] instance. */ public from(value: string | number[]): BytesView { return typeof value === 'string' ? this.fromString(value) : this.fromArray(value); } /** * Returns a new [[BytesView]] instance given a string. * @param value The value that will be converted into a [[BytesView]] instance. * @returns A new [[BytesView]] instance. */ protected abstract fromString(str: string): BytesView; /** * Returns a new [[BytesView]] instance given an array of numbers. * @param arr The array that will be converted into a [[BytesView]] instance. * @returns A new [[BytesView]] instance. */ protected abstract fromArray(arr: number[]): BytesView; public abstract alloc(sizeInBytes: number): BytesView; public abstract slice(start?: number, end?: number): BytesView; public abstract readUint8(offset?: number): number; public abstract readUint16LE(offset?: number): number; public abstract readUint16BE(offset?: number): number; public abstract readUint32LE(offset?: number): number; public abstract readUint32BE(offset?: number): number; public abstract readInt8(offset?: number): number; public abstract readInt16LE(offset?: number): number; public abstract readInt16BE(offset?: number): number; public abstract readInt32LE(offset?: number): number; public abstract readInt32BE(offset?: number): number; public abstract write(value: string, offset?: number): number; public abstract writeUint8(value: number, offset?: number): number; public abstract toString(): string; }