npm-netbuffer

1.0.8 • Public • Published

NetBuffer

NetBuffer is a helper library to write/read binary data

Features:

  • Grows the Buffer size as needed
  • Useful string operations. (Null terminating strings)
  • the buffer offset will automatically increase after each reading and writing so no need to handle it manually
  • Allows inserting values at specific offset in the Buffer
  • Allows reading values at specific offset in the Buffer
  • Compress and decompress the buffer with zlib library

Instalation

npm install npm-netbuffer --save  

Usage

const buffer = require('npm-netbuffer');  
let netBuffer = new buffer(2);  
netBuffer.writeU8(255); // or buff.writeExt(buff.u8, 255);  
netBuffer.writeS8(-127); // or buff.writeExt(buff.s8, -127);  
  
console.log("Bytes:", netBuffer.bytes, "Size:", netBuffer.size, "Offset:", netBuffer.tell);  
  
netBuffer.seek = 0; // Reset current offset to 0  
let value_1 = netBuffer.readU8(); // or buff.readExt(buff.u8);  
let value_2 = netBuffer.readS8(); // or buff.readExt(buff.s8);  
  
console.log(value_1, value_2);  
Output : Bytes: <Buffer ff 81> Size: 2 Offset: 2  
Output : 255 -127  

API

netBuffer(size)

Constructor: initialize with a size.

netBuffer numeric methods

readInt8, readInt16LE, readInt32LE, readUInt8, readUInt16LE, readUInt32LE, writeInt8, writeInt16LE, writeInt32LE, writeUInt8, writeUInt16LE, writeUInt32LE

.seek

Set the buffer offset
Exmaple : netBuffer.seek = 2;

.tell

Get the buffer current offset
Example : let currentOffset = netBuffer.tell;

.size

Get the buffer size
Example : let currentOffset = netBuffer.size;

.sizeof(type)

type: The type of data that is to be checked.
Return the size (in bytes) of any of the given data constants(listed here)

.resize(size)

Resize the buffer to be the size (in bytes) that you specify.

.copy(offset, size, dest_buffer, dest_offset)

offset : The data offset to start copying from (in bytes).
size : The size of the data to copy (in bytes).
dest_buffer : The index of the buffer to copy to.
dest_offset : The offset position to copy the data to (in bytes).

This function can be used to copy a segment (or all) of the data stored in the buffer to another.

.compress()

With this function you can compress all of the buffer using zlib compression.

.decompress()

With this function you can decompress the buffer using zlib compression.

Write

.writeS8(value) Write signed 8bit integer.
.writeU8(value) Write unsigned 8bit integer.
.writeS16(value) Write signed 16bit integer.
.writeU16(value) Write unsigned 16bit integer.
.writeS32(value) Write signed 32bit integer.
.writeU32(value) Write unsigned 32bit integer.
.writeString(value) Write a string of any size, finalized with a null terminating character.
.writeText(value) Write a string of any size, without the final null terminating character.
.writeExt(value, type) Write data to the buffer with a value and given type (listed here).

Read

.readS8()Read signed 8bit integer.
.readU8()Read unsigned 8bit integer.
.readS16()Read signed 16bit integer.
.readU16()Read unsigned 16bit integer.
.readS32()Read signed 32bit integer.
.readU32()Read unsigned 32bit integer.
.readString()Read a string of any size.
.readtext()Read a string of any size, without the final null terminating character.
.readExt(type) Read data from the buffer with a given type (listed here).

BufferTypes

.s8

A signed, 8bit integer. This can be a positive or negative value from -128 to 127 (0 is classed as positive).

.u8

An unsigned, 8bit integer. This is a positive value from 0 to 255.

.s16

A signed, 16bit integer. This can be a positive or negative value from -32,768 to 32,767 (0 is classed as positive).

.u16

An unsigned, 16bit integer. This is a positive value from 0 - 65,535.

.s32

A signed, 32bit integer. This can be a positive or negative value from -2,147,483,648 to 2,147,483,647 (0 is classed as positive).

.u32

An unsigned, 32bit integer. This is a positive value from 0 to 4,294,967,295.

.string

A string of any size, finalized with a null terminating character.

.text

A string of any size, without the final null terminating character.

author :

Ali Jahandideh BlindDragon2016@gmail.com

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i npm-netbuffer

      Weekly Downloads

      1

      Version

      1.0.8

      License

      Unlicense

      Unpacked Size

      29.3 kB

      Total Files

      9

      Last publish

      Collaborators

      • aminprm