UNPKG

@kayahr/text-encoding

Version:
48 lines 1.38 kB
/* * Copyright (C) 2021 Klaus Reimer <k@ailis.de> * See LICENSE.md for licensing information. */ /** Special value returned when end of the buffer has been hit. */ export const END_OF_BUFFER = -1; /** * A buffer backed by by a dynamic byte array. Bytes can be read to or written from the buffer. */ export class ByteBuffer { /** The bytes in the buffer. */ bytes; /** * Creates a new byte buffer backed by the given byte array. * * @param bytes - Array of initial bytes in the buffer. */ constructor(bytes) { // Reversed as push/pop is more efficient than shift/unshift this.bytes = Array.from(bytes).reverse(); } /** * Checks if end of buffer has been hit. * * @returns True if end of buffer, false if not. */ isEndOfBuffer() { return this.bytes.length === 0; } /** * Reads the next byte from the buffer and returns it. END_OF_BUFFER is returned when there are no more bytes to * read. * * @returns The read byte or END_OF_BUFFER when no more bytes to read. */ read() { return this.bytes.pop() ?? END_OF_BUFFER; } /** * Writes the specified bytes to the buffer. * * @param bytes - The bytes to write. */ write(...bytes) { this.bytes.push(...bytes); } } //# sourceMappingURL=ByteBuffer.js.map