Skip to content

Commit

Permalink
Added a getData() method to ByteBuffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Feb 4, 2024
1 parent 1852dc6 commit abcf593
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added

- archive: Support semantic methods for subscribing to unarchive events (onExtract), [Issue #47](https://github.com/codedread/bitjs/issues/47).
- io: Added a getData() method to ByteBuffer to retrieve a copy of the bytes that have been written.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion io/bitstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
*
* Bit reading always proceeds from the first byte in the buffer, to the
* second byte, and so on. The MTL flag controls which bit is considered
* first *inside* the byte.
* first *inside* the byte. The default is least-to-most direction.
*
* An Example for how Most-To-Least vs Least-to-Most mode works:
*
Expand Down
18 changes: 18 additions & 0 deletions io/bytebuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ export class ByteBuffer {
this.data = new Uint8Array(numBytes);

/**
* Points to the byte that will next be written.
* @type {number}
* @public
*/
this.ptr = 0;
}

/**
* Returns an exact copy of all the data that has been written to the ByteBuffer.
* @returns {Uint8Array}
*/
getData() {
const dataCopy = new Uint8Array(this.ptr);
dataCopy.set(this.data.subarray(0, this.ptr));
return dataCopy;
}

/**
* @param {number} b The byte to insert.
*/
insertByte(b) {
if (this.ptr + 1 > this.data.byteLength) {
throw `Cannot insert a byte, the buffer is full.`;
}

// TODO: throw if byte is invalid?
this.data[this.ptr++] = b;
}
Expand All @@ -49,6 +63,10 @@ export class ByteBuffer {
* @param {Array.<number>|Uint8Array|Int8Array} bytes The bytes to insert.
*/
insertBytes(bytes) {
if (this.ptr + bytes.length > this.data.byteLength) {
throw `Cannot insert ${bytes.length} bytes, the buffer is full.`;
}

// TODO: throw if bytes is invalid?
this.data.set(bytes, this.ptr);
this.ptr += bytes.length;
Expand Down
6 changes: 3 additions & 3 deletions io/bytestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ export class ByteStream {
this.littleEndian_ = true;
}

/** @returns {boolean} Whether the stream is little-endian. */
/** @returns {boolean} Whether the stream is little-endian (least significant byte is first). */
isLittleEndian() {
return this.littleEndian_;
}

/**
* Big-Endian is sometimes called Motorola-style.
* Big-Endian means the most significant byte is first. it is sometimes called Motorola-style.
* @param {boolean=} val The value to set. If not present, the stream is set to big-endian.
*/
setBigEndian(val = true) {
this.littleEndian_ = !val;
}

/**
* Little-Endian is sometimes called Intel-style.
* Little-Endian means the least significant byte is ifrst. is sometimes called Intel-style.
* @param {boolean=} val The value to set. If not present, the stream is set to little-endian.
*/
setLittleEndian(val = true) {
Expand Down
25 changes: 25 additions & 0 deletions tests/io-bytebuffer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,35 @@ describe('bitjs.io.ByteBuffer', () => {
buffer = new ByteBuffer(4);
});

describe('getData()', () => {
it('returns an empty array when nothing has been written', () => {
expect(buffer.getData().byteLength).equals(0);
});

it('is sized correctly', () => {
buffer.insertByte(42);
buffer.insertByte(81);
const data = buffer.getData();
expect(data.byteLength).equals(2);
expect(data[0]).equals(42);
expect(data[1]).equals(81);
});
});

it('throws when initialized incorrectly', () => {
expect(() => new ByteBuffer()).throws();
});

describe('Buffer overflow', () => {
it('insertByte() throws when buffer exceeded', () => {
buffer.insertBytes([0, 2, 4, 6]);
expect(() => buffer.insertByte(1)).throws();
});
it('insertBytes() throws when buffer exceeded', () => {
expect(() => buffer.insertBytes([0, 2, 4, 6, 8])).throws();
});
});

it('insertByte()', () => {
buffer.insertByte(192);
expect(buffer.ptr).equals(1);
Expand Down

0 comments on commit abcf593

Please sign in to comment.