-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
Currently this library seems to only work with fixed size buffers, with no way to expand them. Creating a new stream with a larger buffer and copying the old data can work, but in some comtexts this isn't viable (such as if the stream is passed around to many functions by reference, and those functions need to possibly grow the size of the buffer)
I'm currently using this implementation and it seems to work well for my use case:
ensureSize(size) {
const bitsNeeded = size * 8;
const bitsAvailable = this.remaining;
if (bitsAvailable >= bitsNeeded) {
return;
}
const bitsToAdd = bitsNeeded - bitsAvailable;
const bytesToAdd = Math.ceil(bitsToAdd / 8);
const oldBuffer = this.#view.buffer;
const oldView = new Uint8Array(oldBuffer);
const oldLength = oldView.length;
const newBuffer = new ArrayBuffer(oldLength + bytesToAdd);
const newView = new Uint8Array(newBuffer);
newView.set(oldView);
this.#view = new BitView(newBuffer);
this.#view.bigEndian = this.bigEndian;
}Which only adds the required number of bytes such that size will fit
rain1
Metadata
Metadata
Assignees
Labels
No labels