1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-05 02:19:24 +02:00

Add skip() method to ByteStream

This commit is contained in:
Jeff Schiller 2024-01-04 22:22:26 +09:00
parent ee57144579
commit acaa3a90d9
2 changed files with 68 additions and 0 deletions

View file

@ -66,10 +66,12 @@ export class ByteStream {
this.littleEndian_ = true;
}
/** Big-Endian is sometimes called Motorola-style. */
setBigEndian() {
this.littleEndian_ = false;
}
/** Little-Endian is sometimes called Intel-style. */
setLittleEndian() {
this.littleEndian_ = true;
}
@ -292,6 +294,27 @@ export class ByteStream {
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.
* @param {ArrayBuffer} ab