clever-buffer
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
Installation
npm install clever-buffer
NOTE: Examples below in javascript
Reader Usage
Requiring the reader in your project
CleverBufferReader = ;
new CleverBufferReader existingBuffer, [options]
- 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
var reader = existingBuffer offset: 0 noAssert: true bigEndian: false;
reader.getUInt8([offset])
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 8bit Integer from the current offset
var buf = Buffer;var reader = buf;console; // 255console; // 2
reader.getInt8([offset])
- offset Number, Optional, Default: current buffer position
Returns a Signed 8bit Integer from the current offset
var buf = Buffer;var reader = buf;console; // -1console; // 2
reader.getUInt16([offset])
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 16bit Integer from the current offset.
var buf = Buffer;var reader = buf;console; // 65535console; // 2
reader.getInt16([offset])
- offset Number, Optional, Default: current buffer position
Returns a Signed 16bit Integer from the current offset
var buf = Buffer;var reader = buf;console; # -1console; # 2
reader.getUInt32([offset])
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 32bit Integer from the current offset.
var buf = Buffer;var reader = buf;console; // 4294967295
reader.getInt32([offset])
- offset Number, Optional, Default: current buffer position
Returns a Signed 32bit Integer from the current offset
var buf = Buffer;var reader = buf;console; // -1
reader.getUInt64([offset])
- offset Number, Optional, Default: current buffer position
Returns an Unsigned 64bit Integer from the current offset.
The value will be returned as a string
var buf = Buffer;var reader = buf;console; // "18446744073709551615"
reader.getInt64([offset])
- offset Number, Optional, Default: current buffer position
Returns a Signed 64bit Integer from the current offset
The value will be returned as a string
var buf = Buffer;var reader = buf;console; // "-1"
reader.getString([options])
- 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
var buf = Buffer;var reader = buf;console; // "HELLO"
Writer Usage
Requiring the writer in your project
CleverBufferWriter = ;
new CleverBufferWriter existingBuffer, [options]
- 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
var writer = existingBuffer offset: 0 noAssert: true bigEndian: false;
writer.writeUInt8(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 8bit Integer to the specified/current offset
var buf = Buffer;var writer = buf;writer;writer;console; // [0xFF, 0x02]
writer.writeInt8(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an signed 8bit Integer to the specified/current offset
var buf = Buffer;var writer = buf;writer;writer;console; // [0xFF, 0x02]
writer.writeUInt16(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 16bit Integer to the specified/current offset
var buf = Buffer;var writer = buf;writer;writer;console; // [0xFF, 0xFF, 0x02, 0x00]
writer.writeInt16(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Signed 16bit Integer to the specified/current offset
buf = Buffer;writer = buf;writer;writer;console; // [0xFF, 0xFF, 0x02, 0x00]
writer.writeUInt32(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an Unsigned 32bit Integer to the specified/current offset
var buf = Buffer;var writer = buf;writer;console; // [0xFF, 0xFF, 0xFF, 0xFF]
writer.writeInt32(value, [offset])
- value Number
- offset Number, Optional, Default: current buffer position
Writes an signed 32bit Integer to the specified/current offset
var buf = Buffer;var writer = buf;writer;console; // [0xFF, 0xFF, 0xFF, 0xFF]
writer.writeString(value, [options])
- 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
var buf = Buffer;var writer = buf;var len = writer;console; // 6, [0x68, 0xc3, 0xa9, 0x6c, 0x6c, 0x6f, 0x00, 0x00]
Common Functionality
The following is common to CleverBufferReader and CleverBufferWriter (The examples only show reader)
reader.getOffset()
Gets the current offset of the buffer
var buf = Buffer;var reader = buf;console; // 0reader;console; // 1reader;console; // 2
reader.skip(bytes)
- bytes Number
Skips the current offset forward the specified bytes amount
var buf = Buffer;var reader = buf;console; // 0reader;console; // 2
reader.skipTo(offset)
- offset Number
Skips to the specified offset
var buf = Buffer;var reader = buf;console; // 0reader;console; // 3
reader.getBuffer()
Will return the underlying buffer so you can perform actions directly on it
var buf = Buffer;var reader = buf;console; // 2
reader.trim()
Will return a buffer slice from the start of the buffer to the current offset
var buf = Buffer;var reader = buf;reader;console; // [0xFF]console; // [0xFF, 0x02]
Testing
npm test