mirror of
https://github.com/codedread/bitjs
synced 2025-10-06 10:49:55 +02:00
Add counters for number of bytes/bits read in streams since the beginning of time
This commit is contained in:
parent
295413af0b
commit
8c630c170c
2 changed files with 28 additions and 6 deletions
|
@ -38,11 +38,19 @@ bitjs.io.BitStream = class {
|
||||||
this.bytePtr = 0; // tracks which byte we are on
|
this.bytePtr = 0; // tracks which byte we are on
|
||||||
this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7)
|
this.bitPtr = 0; // tracks which bit we are on (can have values 0 through 7)
|
||||||
this.peekBits = rtl ? this.peekBits_rtl : this.peekBits_ltr;
|
this.peekBits = rtl ? this.peekBits_rtl : this.peekBits_ltr;
|
||||||
|
// An ever-increasing number.
|
||||||
|
this.bitsRead_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns how many bites have been read in the stream since the beginning of time.
|
||||||
|
*/
|
||||||
|
getNumBitsRead() {
|
||||||
|
return this.bitsRead_;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns how many bytes are currently in the stream left to be read.
|
* Returns how many bits are currently in the stream left to be read.
|
||||||
*/
|
*/
|
||||||
getNumBitsLeft() {
|
getNumBitsLeft() {
|
||||||
const bitsLeftInByte = 8 - this.bitPtr;
|
const bitsLeftInByte = 8 - this.bitPtr;
|
||||||
|
@ -58,6 +66,7 @@ bitjs.io.BitStream = class {
|
||||||
*/
|
*/
|
||||||
movePointer_(n) {
|
movePointer_(n) {
|
||||||
this.bitPtr += n;
|
this.bitPtr += n;
|
||||||
|
this.bitsRead_ += n;
|
||||||
while (this.bitPtr >= 8) {
|
while (this.bitPtr >= 8) {
|
||||||
this.bitPtr -= 8;
|
this.bitPtr -= 8;
|
||||||
this.bytePtr++;
|
this.bytePtr++;
|
||||||
|
@ -88,8 +97,8 @@ bitjs.io.BitStream = class {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num > this.getNumBitsLeft()) {
|
if (num > this.getNumBitsLeft()) {
|
||||||
throw 'Error! Overflowed the bit stream! n=' + n + ', bytePtr=' + bytePtr +
|
throw 'Error! Overflowed the bit stream! n=' + n + ', bytePtr=' + this.bytePtr +
|
||||||
', bytes.length=' + bytes.length + ', bitPtr=' + bitPtr;
|
', bytes.length=' + this.bytes.length + ', bitPtr=' + this.bitPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const movePointers = opt_movePointers || false;
|
const movePointers = opt_movePointers || false;
|
||||||
|
@ -153,8 +162,8 @@ bitjs.io.BitStream = class {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (num > this.getNumBitsLeft()) {
|
if (num > this.getNumBitsLeft()) {
|
||||||
throw 'Error! Overflowed the bit stream! n=' + n + ', bytePtr=' + bytePtr +
|
throw 'Error! Overflowed the bit stream! n=' + n + ', bytePtr=' + this.bytePtr +
|
||||||
', bytes.length=' + bytes.length + ', bitPtr=' + bitPtr;
|
', bytes.length=' + this.bytes.length + ', bitPtr=' + this.bitPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const movePointers = opt_movePointers || false;
|
const movePointers = opt_movePointers || false;
|
||||||
|
@ -227,7 +236,7 @@ bitjs.io.BitStream = class {
|
||||||
peekBytes(n, opt_movePointers) {
|
peekBytes(n, opt_movePointers) {
|
||||||
const num = parseInt(n, 10);
|
const num = parseInt(n, 10);
|
||||||
if (n !== num || num < 0) {
|
if (n !== num || num < 0) {
|
||||||
throw 'Error! Called peekBytes() with a non-positive integer';
|
throw 'Error! Called peekBytes() with a non-positive integer: ' + n;
|
||||||
} else if (num === 0) {
|
} else if (num === 0) {
|
||||||
return new Uint8Array();
|
return new Uint8Array();
|
||||||
}
|
}
|
||||||
|
@ -260,6 +269,7 @@ bitjs.io.BitStream = class {
|
||||||
|
|
||||||
if (movePointers) {
|
if (movePointers) {
|
||||||
this.bytePtr += num;
|
this.bytePtr += num;
|
||||||
|
this.bitsRead_ += (num * 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -296,6 +306,7 @@ bitjs.io.BitStream = class {
|
||||||
clone.bytePtr = this.bytePtr;
|
clone.bytePtr = this.bytePtr;
|
||||||
clone.bitPtr = this.bitPtr;
|
clone.bitPtr = this.bitPtr;
|
||||||
clone.peekBits = this.peekBits;
|
clone.peekBits = this.peekBits;
|
||||||
|
clone.bitsRead_ = this.bitsRead_;
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,15 @@ bitjs.io.ByteStream = class {
|
||||||
this.bytes = new Uint8Array(ab, offset, length);
|
this.bytes = new Uint8Array(ab, offset, length);
|
||||||
this.ptr = 0;
|
this.ptr = 0;
|
||||||
this.pages_ = [];
|
this.pages_ = [];
|
||||||
|
// An ever-increasing number.
|
||||||
|
this.bytesRead_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns how many bytes have been read in the stream since the beginning of time.
|
||||||
|
*/
|
||||||
|
getNumBytesRead() {
|
||||||
|
return this.bytesRead_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,6 +63,7 @@ bitjs.io.ByteStream = class {
|
||||||
*/
|
*/
|
||||||
movePointer_(n) {
|
movePointer_(n) {
|
||||||
this.ptr += n;
|
this.ptr += n;
|
||||||
|
this.bytesRead_ += n;
|
||||||
while (this.ptr >= this.bytes.length && this.pages_.length > 0) {
|
while (this.ptr >= this.bytes.length && this.pages_.length > 0) {
|
||||||
this.ptr -= this.bytes.length;
|
this.ptr -= this.bytes.length;
|
||||||
this.bytes = this.pages_.shift();
|
this.bytes = this.pages_.shift();
|
||||||
|
@ -262,6 +272,7 @@ bitjs.io.ByteStream = class {
|
||||||
clone.bytes = this.bytes;
|
clone.bytes = this.bytes;
|
||||||
clone.ptr = this.ptr;
|
clone.ptr = this.ptr;
|
||||||
clone.pages_ = this.pages_.slice();
|
clone.pages_ = this.pages_.slice();
|
||||||
|
clone.bytesRead_ = this.bytesRead_;
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue