Skip to content

Commit

Permalink
Better explanation for tee()
Browse files Browse the repository at this point in the history
  • Loading branch information
codedread committed Jan 4, 2024
1 parent 004cd87 commit 79c289a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions docs/bitjs.io.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ the byte stream by using `someByteStream.push(nextBytesAsAnArrayBuffer)`.

If you have a need to seek ahead to a different section of the stream of bytes, and want to later
return to where you left off, you should use `tee()` method to make a copy of the ByteStream. This
will let you seek to the appropriate spot to grab some bytes.
will let you seek to the appropriate spot to grab some bytes using the teed stream, while you can
pick up where you left off with the original stream.

```javascript
const byteStream = new ByteStream(someArrayBuffer);
const strLen = byteStream.readNumber(4); // Bytes 0-3.
const strOffset = byteStream.readNumber(4); // Bytes 4-7.
// Grab bytes at that offset...
const description = byteStream.tee().skip(offset).readString(strLen);
const someOtherVal = byteStream.readNumber(4); // Bytes 8-11
const origStream = new ByteStream(someArrayBuffer);
const strLen = origStream.readNumber(4); // Bytes 0-3.
const strOffset = origStream.readNumber(4); // Bytes 4-7.

const teedStream = origStream.tee();
const description = teedStream.skip(strOffset).readString(strLen);

const someOtherVal = origStream.readNumber(4); // Bytes 8-11
```

0 comments on commit 79c289a

Please sign in to comment.