Skip to content

Commit

Permalink
Add readRemaining and readRemainingWritten methods
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed May 5, 2024
1 parent 4927953 commit a5a8669
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ declare module "bytewriter" {

// Return a new Buffer containing the bytes written so far
readBuffer(length: number): Buffer
// Read the remaining bytes in the buffer from the current read index to end of underlying buffer
readRemaining(): Buffer
// Read the remaining bytes in the buffer from the current read index to the current write index
readRemainingWritten(): Buffer

// Varints

Expand Down
19 changes: 16 additions & 3 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MAX_ALLOC_SIZE = 1024 * 1024 * 2 // 2MB
const DEFAULT_ALLOC_SIZE = 10000
const DEFAULT_ALLOC_SIZE = 10000 // 10 KB

class ByteStream {
constructor (buffer, maxSize) {
Expand All @@ -19,10 +19,11 @@ class ByteStream {

resizeForWriteIfNeeded (bytes) {
if ((this.writeOffset + bytes) > this.size) {
this.size *= 2
const newBuffer = new Uint8Array(this.size)
const allocSize = this.writeOffset
const newBuffer = new Uint8Array(allocSize)
newBuffer.set(this.buffer)
this.buffer = newBuffer
this.size = this.buffer.length
this.view = new DataView(this.buffer.buffer)
}
if (this.size > this.guardLimit) throw new Error('Buffer size exceeded guard limit')
Expand Down Expand Up @@ -250,6 +251,18 @@ class ByteStream {
return value
}

readRemaining () {
const value = this.buffer.subarray(this.readOffset)
this.readOffset = this.buffer.length
return value
}

readRemainingWritten () {
const value = this.buffer.subarray(0, this.writeOffset)
this.readOffset = this.writeOffset
return value
}

// Varints
// Write a signed varint
writeVarInt (value) {
Expand Down
18 changes: 15 additions & 3 deletions src/node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MAX_ALLOC_SIZE = 1024 * 1024 * 2 // 2MB
const DEFAULT_ALLOC_SIZE = 10000
const DEFAULT_ALLOC_SIZE = 10000 // 10 KB

class ByteStream {
constructor (buffer, maxSize) {
Expand All @@ -13,9 +13,9 @@ class ByteStream {

resizeForWriteIfNeeded (bytes) {
if ((this.writeOffset + bytes) > this.size) {
this.size *= 2
const allocSize = this.size - this.writeOffset
const allocSize = this.writeOffset
this.buffer = Buffer.concat([this.buffer, Buffer.allocUnsafe(allocSize)])
this.size = this.buffer.length
}
// Detect potential writing bugs
if (this.size > this.guardLimit) throw new Error('Buffer size exceeded guard limit')
Expand Down Expand Up @@ -288,6 +288,18 @@ class ByteStream {
return value
}

readRemaining () {
const value = this.buffer.slice(this.readOffset)
this.readOffset = this.buffer.length
return value
}

readRemainingWritten () {
const value = this.buffer.slice(0, this.writeOffset)
this.readOffset = this.writeOffset
return value
}

// Varints

// Write a signed varint
Expand Down
1 change: 1 addition & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('basic tests', () => {
stream.writeBuffer(Buffer.from([0x28, 0x29, 0x2a, 0x2b, 0x2c]))
stream.readUInt8(0x01)
stream.readUInt16BE(0x0203)
console.log('Buffer[24:]', stream.readRemainingWritten(), 'total:', stream.readRemaining())
assert.strictEqual(stream.getBuffer().toString('hex'), '878c94208f9cb4e0b0c1c28408a6c8888102b6e8c881e3858b95283f9df3b63ff3c0ca4283de1b68656c6c6f20776f726c64210028292a2b2c')
assert(ByteWriter.streamsEqual(stream, stream))
})
Expand Down

0 comments on commit a5a8669

Please sign in to comment.