UNPKG

@adguard/agtree

Version:
55 lines (54 loc) 2.2 kB
/** * @file Core ByteBuffer implementation for handling binary data in chunks. */ /** * Core ByteBuffer implementation for handling binary data in chunks. * This class allows for efficient byte storage and manipulation by organizing data into chunks * and providing methods to read and write bytes. */ export declare class ByteBuffer { /** * The size of each chunk in bytes (32 KB). */ static readonly CHUNK_SIZE = 32768; /** * An array of Uint8Array chunks that make up the buffer. */ protected chunks: Uint8Array[]; /** * The total number of chunks in the buffer. */ protected chunksLength: number; /** * Constructs a new ByteBuffer instance. * * @param chunks Optional array of chunks to initialize the ByteBuffer with. * @param cloneChunks Flag indicating if the chunks should be cloned. For performance reasons, * its default value is `false`. If the original chunks are guaranteed not to change, * leave this flag as `false` to avoid unnecessary copying. */ constructor(chunks?: Uint8Array[], cloneChunks?: boolean); /** * Ensures that the buffer has enough capacity to accommodate a given position. * This method adjusts the `chunks` array size to ensure it can hold the specified position. * * @param position The position to ensure capacity for. */ protected ensureCapacity(position: number): void; /** * Writes a byte to the buffer at the specified position. * If the position is outside of the buffer's current size, the buffer is resized to accommodate it. * * @param position The position at which to write the byte. * @param value The byte value to write (0-255). */ protected writeByte(position: number, value: number): void; /** * Reads a byte from the specified position in the buffer. * Returns `undefined` if the position is outside of the buffer's current size. * * @param position The position from which to read the byte. * @returns The read byte value, or `undefined` if the position is out of bounds. */ protected readByte(position: number): number | undefined; }