Skip to content

Commit 053d4bc

Browse files
committed
Fix bug with tee() so that endianness is also copied to teed stream.
1 parent 79c289a commit 053d4bc

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

io/bytestream.js

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

69+
/** @returns {boolean} Whether the stream is little-endian. */
70+
isLittleEndian() {
71+
return this.littleEndian_;
72+
}
73+
6974
/** Big-Endian is sometimes called Motorola-style. */
7075
setBigEndian() {
7176
this.littleEndian_ = false;
@@ -78,13 +83,15 @@ export class ByteStream {
7883

7984
/**
8085
* Returns how many bytes have been read in the stream since the beginning of time.
86+
* @returns {number}
8187
*/
8288
getNumBytesRead() {
8389
return this.bytesRead_;
8490
}
8591

8692
/**
8793
* Returns how many bytes are currently in the stream left to be read.
94+
* @returns {number}
8895
*/
8996
getNumBytesLeft() {
9097
const bytesInCurrentPage = (this.bytes.byteLength - this.ptr);
@@ -334,6 +341,10 @@ export class ByteStream {
334341

335342
/**
336343
* Creates a new ByteStream from this ByteStream that can be read / peeked.
344+
* Note that the teed stream is a disconnected copy. If you push more bytes to the original
345+
* stream, the copy does not get them.
346+
* TODO: Assess whether the above causes more bugs than it avoids. (It would feel weird to me if
347+
* the teed stream shared some state with the original stream.)
337348
* @returns {ByteStream} A clone of this ByteStream.
338349
*/
339350
tee() {
@@ -342,6 +353,7 @@ export class ByteStream {
342353
clone.ptr = this.ptr;
343354
clone.pages_ = this.pages_.slice();
344355
clone.bytesRead_ = this.bytesRead_;
356+
clone.littleEndian_ = this.littleEndian_;
345357
return clone;
346358
}
347359
}

tests/io-bytestream.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ describe('bitjs.io.ByteStream', () => {
255255
it('Tee', () => {
256256
for (let i = 0; i < 4; ++i) array[i] = 65 + i;
257257
const stream = new ByteStream(array.buffer);
258+
// Set non-default endianness.
259+
stream.setBigEndian(true);
258260

259261
const anotherArray = new Uint8Array(4);
260262
for (let i = 0; i < 4; ++i) anotherArray[i] = 69 + i;
@@ -265,5 +267,6 @@ describe('bitjs.io.ByteStream', () => {
265267
teed.readBytes(5);
266268
expect(stream.getNumBytesLeft()).equals(8);
267269
expect(teed.getNumBytesLeft()).equals(3);
270+
expect(teed.isLittleEndian()).equals(stream.isLittleEndian());
268271
});
269272
});

0 commit comments

Comments
 (0)