Skip to content

Commit abcf593

Browse files
committed
Added a getData() method to ByteBuffer.
1 parent 1852dc6 commit abcf593

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
77
### Added
88

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

1112
### Changed
1213

io/bitstream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
2020
*
2121
* Bit reading always proceeds from the first byte in the buffer, to the
2222
* second byte, and so on. The MTL flag controls which bit is considered
23-
* first *inside* the byte.
23+
* first *inside* the byte. The default is least-to-most direction.
2424
*
2525
* An Example for how Most-To-Least vs Least-to-Most mode works:
2626
*

io/bytebuffer.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,31 @@ export class ByteBuffer {
3030
this.data = new Uint8Array(numBytes);
3131

3232
/**
33+
* Points to the byte that will next be written.
3334
* @type {number}
3435
* @public
3536
*/
3637
this.ptr = 0;
3738
}
3839

40+
/**
41+
* Returns an exact copy of all the data that has been written to the ByteBuffer.
42+
* @returns {Uint8Array}
43+
*/
44+
getData() {
45+
const dataCopy = new Uint8Array(this.ptr);
46+
dataCopy.set(this.data.subarray(0, this.ptr));
47+
return dataCopy;
48+
}
3949

4050
/**
4151
* @param {number} b The byte to insert.
4252
*/
4353
insertByte(b) {
54+
if (this.ptr + 1 > this.data.byteLength) {
55+
throw `Cannot insert a byte, the buffer is full.`;
56+
}
57+
4458
// TODO: throw if byte is invalid?
4559
this.data[this.ptr++] = b;
4660
}
@@ -49,6 +63,10 @@ export class ByteBuffer {
4963
* @param {Array.<number>|Uint8Array|Int8Array} bytes The bytes to insert.
5064
*/
5165
insertBytes(bytes) {
66+
if (this.ptr + bytes.length > this.data.byteLength) {
67+
throw `Cannot insert ${bytes.length} bytes, the buffer is full.`;
68+
}
69+
5270
// TODO: throw if bytes is invalid?
5371
this.data.set(bytes, this.ptr);
5472
this.ptr += bytes.length;

io/bytestream.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@ export class ByteStream {
6666
this.littleEndian_ = true;
6767
}
6868

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

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

8282
/**
83-
* Little-Endian is sometimes called Intel-style.
83+
* Little-Endian means the least significant byte is ifrst. is sometimes called Intel-style.
8484
* @param {boolean=} val The value to set. If not present, the stream is set to little-endian.
8585
*/
8686
setLittleEndian(val = true) {

tests/io-bytebuffer.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,35 @@ describe('bitjs.io.ByteBuffer', () => {
1818
buffer = new ByteBuffer(4);
1919
});
2020

21+
describe('getData()', () => {
22+
it('returns an empty array when nothing has been written', () => {
23+
expect(buffer.getData().byteLength).equals(0);
24+
});
25+
26+
it('is sized correctly', () => {
27+
buffer.insertByte(42);
28+
buffer.insertByte(81);
29+
const data = buffer.getData();
30+
expect(data.byteLength).equals(2);
31+
expect(data[0]).equals(42);
32+
expect(data[1]).equals(81);
33+
});
34+
});
35+
2136
it('throws when initialized incorrectly', () => {
2237
expect(() => new ByteBuffer()).throws();
2338
});
2439

40+
describe('Buffer overflow', () => {
41+
it('insertByte() throws when buffer exceeded', () => {
42+
buffer.insertBytes([0, 2, 4, 6]);
43+
expect(() => buffer.insertByte(1)).throws();
44+
});
45+
it('insertBytes() throws when buffer exceeded', () => {
46+
expect(() => buffer.insertBytes([0, 2, 4, 6, 8])).throws();
47+
});
48+
});
49+
2550
it('insertByte()', () => {
2651
buffer.insertByte(192);
2752
expect(buffer.ptr).equals(1);

0 commit comments

Comments
 (0)