mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Add some docs for ByteStream
This commit is contained in:
parent
cb95ddc3ef
commit
05cbcdc561
2 changed files with 26 additions and 4 deletions
|
@ -3,9 +3,13 @@
|
|||
This package includes stream objects for reading and writing binary data at the bit and byte level:
|
||||
BitStream, ByteStream.
|
||||
|
||||
Streams are given an ArrayBuffer of bytes and keeps track of where in the stream you are. As you
|
||||
read through the stream, the pointer is advanced through the buffer. If you need to peek at a number
|
||||
without advancing the pointer, use the `peek` methods.
|
||||
|
||||
## BitStream
|
||||
|
||||
A bit stream is a way to read a variable number of bits from a series of bytes. This useful for
|
||||
A bit stream is a way to read a variable number of bits from a series of bytes. This is useful for
|
||||
parsing certain protocols (for example pkzip or rar algorithm). Note that the order of reading
|
||||
bits can go from least-to-most significant bit, or the reverse.
|
||||
|
||||
|
@ -30,3 +34,23 @@ bstream.readBits(6); // (blue) 0b010010 = 18
|
|||
bstream.readBits(5); // (red) 0b11000 = 24
|
||||
bstream.readBits(8); // (green) 0b10110100 = 180
|
||||
```
|
||||
|
||||
## ByteStream
|
||||
|
||||
A ByteStream is a convenient way to read numbers and ASCII strings from a set of bytes. For example,
|
||||
interpreting 2 bytes in the stream as a number is done by calling `someByteStream.readNumber(2)`. By
|
||||
default, the byte stream is considered Little Endian, but can be toggled at any point using
|
||||
`someByteStream.setBigEndian()` and toggled back with `someByteStream.setLittleEndian()`.
|
||||
|
||||
By default, numbers are unsigned, but `peekSignedNumber(n)` and `readSignedNumber(n)` exist for signed numbers.
|
||||
|
||||
```javascript
|
||||
const byteStream = new ByteStream(someArrayBuffer);
|
||||
byteStream.setBigEndian();
|
||||
byteStream.readNumber(2); // skip two bytes.
|
||||
// Interpret next 2 bytes as the string length.
|
||||
const strLen = byteStream.readNumber(2);
|
||||
// Read in the rest of the ASCII string.
|
||||
const someString = byteStream.readNumber(strLen);
|
||||
...
|
||||
```
|
|
@ -195,9 +195,7 @@ export class GifParser extends EventTarget {
|
|||
*/
|
||||
version;
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} ab
|
||||
*/
|
||||
/** @param {ArrayBuffer} ab */
|
||||
constructor(ab) {
|
||||
super();
|
||||
this.bstream = new ByteStream(ab);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue