Buffer write and read utilities.
CleverBuffer adds functionality that is missing from the node Buffer class
- Keeps track of the offset for you
- One time specification of endian-ness and whether to assert on buffer length
- 64 bit integer support. We use ref under the hood for our 64 bit numbers
npm install clever-buffer
NOTE: Examples below in coffeescript
{ CleverBufferReader } = require 'clever-buffer'
- existingBuffer Buffer
- options
- offset Number, Optional, Default: 0
- noAssert Boolean, Optional, Default: true
- bigEndian Boolean, Optional, Default: false
Allocates a new CleverBufferReader with an internal buffer of the specified existingBuffer
reader = new CleverBufferReader existingBuffer, {offset:0, noAssert: true, bigEndian: false}
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 8bit Integer from the current offset
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
console.log reader.getUInt8() # 255
console.log reader.getUInt8() # 2
- offset Number, Optional, Default: current buffer position
Returns a Signed 8bit Integer from the current offset
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
console.log reader.getInt8() # -1
console.log reader.getInt8() # 2
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 16bit Integer from the current offset.
buf = new Buffer [0xFF, 0xFF, 0x02, 0x00]
reader = new CleverBufferReader buf
console.log reader.getUInt16() # 65535
console.log reader.getUInt16() # 2
- offset Number, Optional, Default: current buffer position
Returns a Signed 16bit Integer from the current offset
buf = new Buffer [0xFF, 0xFF, 0x02, 0x00]
reader = new CleverBufferReader buf
console.log reader.getInt16() # -1
console.log reader.getInt16() # 2
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 32bit Integer from the current offset.
buf = new Buffer [0xFF, 0xFF, 0xFF, 0xFF]
reader = new CleverBufferReader buf
console.log reader.getUInt32() # 4294967295
- offset Number, Optional, Default: current buffer position
Returns a Signed 32bit Integer from the current offset
buf = new Buffer [0xFF, 0xFF, 0xFF, 0xFF]
reader = new CleverBufferReader buf
console.log reader.getInt32() # -1
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 64bit Integer from the current offset.
The value will be returned as a string
buf = new Buffer [0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF]
reader = new CleverBufferReader buf
console.log reader.getUInt64() # "18446744073709551615"
- offset Number, Optional, Default: current buffer position
Returns a Signed 64bit Integer from the current offset
The value will be returned as a string
buf = new Buffer [0xFF, 0xFF, 0xFF, 0xFF,0xFF, 0xFF, 0xFF, 0xFF]
reader = new CleverBufferReader buf
console.log reader.getInt64() # "-1"
- options Optional
- length Number, Optional, Default: 0
- offset Number, Optional, Default: current offset. If an offset is specified the current offset will not be incremented
- encoding String, Optional, Default: 'utf-8'
Returns utf-8 encoded string of specified length
buf = new Buffer [0x48, 0x45, 0x4C, 0x4C, 0x4F]
reader = new CleverBufferReader buf
console.log reader.getString(length:5) # "HELLO"
{ CleverBufferWriter } = require 'clever-buffer'
- existingBuffer Buffer
- options
- offset Number, Optional, Default: 0
- noAssert Boolean, Optional, Default: true
- bigEndian Boolean, Optional, Default: false
Allocates a new CleverBufferWriter with an internal buffer of the specified existingBuffer
writer = new CleverBufferWriter existingBuffer, {offset:0, noAssert: true, bigEndian: false}
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 8bit Integer to the specified/current offset
buf = new Buffer 2
writer = new CleverBufferWriter buf
writer.writeUInt8 255
writer.writeUInt8 2
console.log buf # [0xFF, 0x02]
- value Number
- offset Number, Optional, Default: current buffer position
Writes an signed 8bit Integer to the specified/current offset
buf = new Buffer 2
writer = new CleverBufferWriter buf
writer.writeInt8 -1
writer.writeInt8 2
console.log buf # [0xFF, 0x02]
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 16bit Integer to the specified/current offset
buf = new Buffer 4
writer = new CleverBufferWriter buf
writer.writeUInt16 65535
writer.writeUInt16 2
console.log buf # [0xFF, 0xFF, 0x02, 0x00]
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Signed 16bit Integer to the specified/current offset
buf = new Buffer 4
writer = new CleverBufferWriter buf
writer.writeInt16 -1
writer.writeInt16 2
console.log buf # [0xFF, 0xFF, 0x02, 0x00]
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 32bit Integer to the specified/current offset
buf = new Buffer 4
writer = new CleverBufferWriter buf
writer.writeUInt32 4294967295
console.log buf # [0xFF, 0xFF, 0xFF, 0xFF]
- value Number
- offset Number, Optional, Default: current buffer position
Writes an signed 32bit Integer to the specified/current offset
buf = new Buffer 4
writer = new CleverBufferWriter buf
writer.writeInt32 -1
console.log buf # [0xFF, 0xFF, 0xFF, 0xFF]
- value String
- options Optional
- length Number, Optional, number of bytes to write. Note this can be greater than
value.length
for non ASCII encodings. If not specified, will default to the right number of bytes for the chosen encoding. - offset Number, Optional, Default: current offset. If an offset is specified the current offset will not be incremented
- encoding String, Optional, Default: 'utf-8'
- length Number, Optional, number of bytes to write. Note this can be greater than
- returns the number of bytes written
Writes string to the buffer
buf = new Buffer 8
writer = new CleverBufferWriter buf
len = writer.writeString "héllo"
console.log len, buf # 6, [0x68, 0xc3, 0xa9, 0x6c, 0x6c, 0x6f, 0x00, 0x00]
The following is common to CleverBufferReader and CleverBufferWriter (The examples only show reader)
Gets the current offset of the buffer
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
console.log reader.getOffset() # 0
reader.getUInt8()
console.log reader.getOffset() # 1
reader.getUInt8()
console.log reader.getOffset() # 2
- bytes Number
Skips the current offset forward the specified bytes amount
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
console.log reader.getOffset() # 0
reader.skip 2
console.log reader.getOffset() # 2
- offset Number
Skips to the specified offset
buf = new Buffer [0xFF, 0xFF, 0xFF, 0x01]
reader = new CleverBufferReader buf
console.log reader.getOffset() # 0
reader.skipTo 3
console.log reader.getOffset() # 3
Will return the underlying buffer so you can perform actions directly on it
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
console.log reader.getBuffer()[1] # 2
Will return a buffer slice from the start of the buffer to the current offset
buf = new Buffer [0xFF, 0x02]
reader = new CleverBufferReader buf
reader.getUInt8()
console.log reader.trim() # [0xFF]
console.log buf # [0xFF, 0x02]
npm test