mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Fix bug with tee() so that endianness is also copied to teed stream.
This commit is contained in:
parent
79c289afa1
commit
053d4bc127
2 changed files with 15 additions and 0 deletions
|
@ -66,6 +66,11 @@ export class ByteStream {
|
||||||
this.littleEndian_ = true;
|
this.littleEndian_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {boolean} Whether the stream is little-endian. */
|
||||||
|
isLittleEndian() {
|
||||||
|
return this.littleEndian_;
|
||||||
|
}
|
||||||
|
|
||||||
/** Big-Endian is sometimes called Motorola-style. */
|
/** Big-Endian is sometimes called Motorola-style. */
|
||||||
setBigEndian() {
|
setBigEndian() {
|
||||||
this.littleEndian_ = false;
|
this.littleEndian_ = false;
|
||||||
|
@ -78,6 +83,7 @@ export class ByteStream {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns how many bytes have been read in the stream since the beginning of time.
|
* Returns how many bytes have been read in the stream since the beginning of time.
|
||||||
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
getNumBytesRead() {
|
getNumBytesRead() {
|
||||||
return this.bytesRead_;
|
return this.bytesRead_;
|
||||||
|
@ -85,6 +91,7 @@ export class ByteStream {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns how many bytes are currently in the stream left to be read.
|
* Returns how many bytes are currently in the stream left to be read.
|
||||||
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
getNumBytesLeft() {
|
getNumBytesLeft() {
|
||||||
const bytesInCurrentPage = (this.bytes.byteLength - this.ptr);
|
const bytesInCurrentPage = (this.bytes.byteLength - this.ptr);
|
||||||
|
@ -334,6 +341,10 @@ export class ByteStream {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new ByteStream from this ByteStream that can be read / peeked.
|
* Creates a new ByteStream from this ByteStream that can be read / peeked.
|
||||||
|
* Note that the teed stream is a disconnected copy. If you push more bytes to the original
|
||||||
|
* stream, the copy does not get them.
|
||||||
|
* TODO: Assess whether the above causes more bugs than it avoids. (It would feel weird to me if
|
||||||
|
* the teed stream shared some state with the original stream.)
|
||||||
* @returns {ByteStream} A clone of this ByteStream.
|
* @returns {ByteStream} A clone of this ByteStream.
|
||||||
*/
|
*/
|
||||||
tee() {
|
tee() {
|
||||||
|
@ -342,6 +353,7 @@ export class ByteStream {
|
||||||
clone.ptr = this.ptr;
|
clone.ptr = this.ptr;
|
||||||
clone.pages_ = this.pages_.slice();
|
clone.pages_ = this.pages_.slice();
|
||||||
clone.bytesRead_ = this.bytesRead_;
|
clone.bytesRead_ = this.bytesRead_;
|
||||||
|
clone.littleEndian_ = this.littleEndian_;
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -255,6 +255,8 @@ describe('bitjs.io.ByteStream', () => {
|
||||||
it('Tee', () => {
|
it('Tee', () => {
|
||||||
for (let i = 0; i < 4; ++i) array[i] = 65 + i;
|
for (let i = 0; i < 4; ++i) array[i] = 65 + i;
|
||||||
const stream = new ByteStream(array.buffer);
|
const stream = new ByteStream(array.buffer);
|
||||||
|
// Set non-default endianness.
|
||||||
|
stream.setBigEndian(true);
|
||||||
|
|
||||||
const anotherArray = new Uint8Array(4);
|
const anotherArray = new Uint8Array(4);
|
||||||
for (let i = 0; i < 4; ++i) anotherArray[i] = 69 + i;
|
for (let i = 0; i < 4; ++i) anotherArray[i] = 69 + i;
|
||||||
|
@ -265,5 +267,6 @@ describe('bitjs.io.ByteStream', () => {
|
||||||
teed.readBytes(5);
|
teed.readBytes(5);
|
||||||
expect(stream.getNumBytesLeft()).equals(8);
|
expect(stream.getNumBytesLeft()).equals(8);
|
||||||
expect(teed.getNumBytesLeft()).equals(3);
|
expect(teed.getNumBytesLeft()).equals(3);
|
||||||
|
expect(teed.isLittleEndian()).equals(stream.isLittleEndian());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue