mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Add skip() method to ByteStream
This commit is contained in:
parent
ee57144579
commit
acaa3a90d9
2 changed files with 68 additions and 0 deletions
|
@ -66,10 +66,12 @@ export class ByteStream {
|
||||||
this.littleEndian_ = true;
|
this.littleEndian_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Big-Endian is sometimes called Motorola-style. */
|
||||||
setBigEndian() {
|
setBigEndian() {
|
||||||
this.littleEndian_ = false;
|
this.littleEndian_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Little-Endian is sometimes called Intel-style. */
|
||||||
setLittleEndian() {
|
setLittleEndian() {
|
||||||
this.littleEndian_ = true;
|
this.littleEndian_ = true;
|
||||||
}
|
}
|
||||||
|
@ -292,6 +294,27 @@ export class ByteStream {
|
||||||
return strToReturn;
|
return strToReturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skips n bytes in the stream.
|
||||||
|
* @param {number} n The number of bytes to skip. Must be a positive integer.
|
||||||
|
*/
|
||||||
|
skip(n) {
|
||||||
|
const num = parseInt(n, 10);
|
||||||
|
if (n !== num || num < 0) {
|
||||||
|
throw 'Error! Called skip() with a non-positive integer';
|
||||||
|
} else if (num === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalBytesLeft = this.getNumBytesLeft();
|
||||||
|
if (num > totalBytesLeft) {
|
||||||
|
throw 'Error! Overflowed the byte stream while skip()! n=' + num +
|
||||||
|
', ptr=' + this.ptr + ', bytes.length=' + this.getNumBytesLeft();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.movePointer_(n);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Feeds more bytes into the back of the stream.
|
* Feeds more bytes into the back of the stream.
|
||||||
* @param {ArrayBuffer} ab
|
* @param {ArrayBuffer} ab
|
||||||
|
|
|
@ -202,6 +202,51 @@ describe('bitjs.io.ByteStream', () => {
|
||||||
expect(str).equals('ABCDEFGHIJKL');
|
expect(str).equals('ABCDEFGHIJKL');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('skip()', () => {
|
||||||
|
/** @type {ByteStream} */
|
||||||
|
let stream;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
for (let i = 0; i < 4; ++i) array[i] = i;
|
||||||
|
stream = new ByteStream(array.buffer);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skips bytes', () => {
|
||||||
|
stream.skip(2);
|
||||||
|
expect(stream.getNumBytesRead()).equals(2);
|
||||||
|
expect(stream.getNumBytesLeft()).equals(2);
|
||||||
|
expect(stream.readNumber(1)).equals(2);
|
||||||
|
expect(stream.readNumber(1)).equals(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip(0) has no effect', () => {
|
||||||
|
stream.skip(0);
|
||||||
|
expect(stream.getNumBytesRead()).equals(0);
|
||||||
|
expect(stream.getNumBytesLeft()).equals(4);
|
||||||
|
expect(stream.readNumber(1)).equals(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip() with negative throws an error', () => {
|
||||||
|
expect(() => stream.skip(-1)).throws();
|
||||||
|
expect(stream.getNumBytesLeft()).equals(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip() past the end throws an error', () => {
|
||||||
|
expect(() => stream.skip(4)).does.not.throw();
|
||||||
|
expect(stream.getNumBytesLeft()).equals(0);
|
||||||
|
expect(() => stream.skip(1)).throws();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('skip() works correct across pages of bytes', () => {
|
||||||
|
const extraArr = new Uint8Array(4);
|
||||||
|
for (let i = 0; i < 4; ++i) extraArr[i] = i + 4;
|
||||||
|
stream.push(extraArr.buffer);
|
||||||
|
expect(stream.readNumber(1)).equals(0);
|
||||||
|
stream.skip(5);
|
||||||
|
expect(stream.readNumber(1)).equals(6);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
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);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue