simple-buffer-cursor
v1.0.3
Published
Read and write to a buffer easily
Downloads
5
Readme
simple-buffer-cursor
Simplify reading and writing to buffers by internally maintaining the position. This library exposes a wrapper for a buffer that uses similar read and write methods to those found in the Node Buffer API.
Usage:
npm install simple-buffer-cursor
const BufferCursor = require('simple-buffer-cursor');
let buffer = BufferCursor(Buffer.alloc(8));
buffer.writeUInt8(1);
buffer.writeUInt16BE(2);
buffer.writeUInt32BE(3);
buffer.writeBytes(Buffer.from([4]));
buffer.position = 0;
console.log(buffer.readUInt8());
console.log(buffer.readUInt16BE());
console.log(buffer.readUInt32BE());
console.log(buffer.readBytes());
API
position: number
- the current buffer cursor positioneof: bool
- whether the current position is at the end of the bufferbuffer: Buffer
- the underlying bufferreadUInt8(): number
- reads 1 bytereadUInt16LE(): number
- reads 2 bytes as 16-bit little-endian unsigned integerreadUInt16BE(): number
- reads 2 bytes as 16-bit big-endian unsigned integerreadUInt32LE(): number
- reads 4 bytes as 32-bit little-endian unsigned integerreadUInt32BE(): number
- reads 4 bytes as 32-bit big-endian unsigned integerreadBytes([len]): Buffer
- reads the optionally specified number of bytes. If no length is supplied, it reads to the end of the buffer.writeUInt8(number): undefined
- writes 1 bytewriteUInt16LE(number): undefined
- writes a number as a 16-bit little-endian unsigned integerwriteUInt16BE(number): undefined
- writes a number as a 16-bit big-endian unsigned integerwriteUInt32LE(number): undefined
- writes a number as a 32-bit little-endian unsigned integerwriteUInt32BE(number): undefined
- writes a number as a 32-bit big-endian unsigned integerwriteBytes(buffer): undefined
- writes the supplied buffer to the cursor buffer