buffer-stream-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

buffer-stream-js

Easy to use Uint8Array stream for read and write.

Example

import {BufferStream} from "buffer-stream-js";
 
//Create BufferStream
let writeStream = BufferStream.alloc(1024);
//Write some data
writeStream.writeUInt8(34);
writeStream.writeFloatBE(5.234);
writeStream.writePackedUtf8String("Hello world!");
//Get result as Uint8Array
let result = writeStream.getLeftBuffer();
 
//Create BufferStream from Uint8Array
let readStream = new BufferStream(result);
//Read data
console.log(readStream.readUint8());
console.log(readStream.readFloatBE());
console.log(readStream.readPackedUtf8String());

Documentation

//Create BufferStream from ArrayBuffer or Uint8Array
new BufferStream(bufferArrayBuffer | ArrayBufferView);
 
//Creae with allocated memory
BufferStream.alloc(sizenumber)BufferStream;
 
//Read data and move cursor
bufferStream.read(lengthnumber)Uint8Array
bufferStream.readInt8()number
bufferStream.readInt16LE()number
bufferStream.readInt16BE()number
bufferStream.readInt32LE()number
bufferStream.readInt32BE()number
bufferStream.readUInt8()number
bufferStream.readUInt16LE()number
bufferStream.readUInt16BE()number
bufferStream.readUInt32LE()number
bufferStream.readUInt32BE()number
bufferStream.readFloatLE()number
bufferStream.readFloatBE()number
bufferStream.readDoubleLE()number
bufferStream.readDoubleBE()number
 
//Write data and move cursor
bufferStream.write(dataArrayBuffer | ArrayBufferView | number[])number
bufferStream.writeInt8(datanumber)void
bufferStream.writeInt16LE(datanumber)void
bufferStream.writeInt16BE(datanumber)void
bufferStream.writeInt32LE(datanumber)void
bufferStream.writeInt32BE(datanumber)void
bufferStream.writeUInt8(datanumber)void
bufferStream.writeUInt16LE(datanumber)void
bufferStream.writeUInt16BE(datanumber)void
bufferStream.writeUInt32LE(datanumber)void
bufferStream.writeUInt32BE(datanumber)void
bufferStream.writeFloatLE(datanumber)void
bufferStream.writeFloatBE(datanumber)void
bufferStream.writeDoubleLE(datanumber)void
bufferStream.writeDoubleBE(datanumber)void
 
//Read & write boolean and move cursor, false is 0, true is 1 (one-byte)
bufferStream.readBoolean()boolean
bufferStream.writeBoolean(valueboolean)void
 
//Read & write dynamic size int
bufferStream.readInt()number
bufferStream.writeInt(datanumber)number
 
//Read & write dynamic size unsigned int
bufferStream.readUInt()number
bufferStream.writeUInt(datanumber)number
 
//Read & write packed data, first packed data has dynamic uint which indicates data length, and then the data
bufferStream.readPacked()Uint8Array
bufferStream.writePacked(dataArrayBuffer | ArrayBufferView | number[])number
 
//Read & write utf8 string
bufferStream.readUtf8String(lengthnumber)string
bufferStream.writeUtf8String(datastring)number
 
//Read & write packed utf8 string, first packed data has dynamic uint which indicates data length, and then the data
bufferStream.readPackedUtf8String()string
bufferStream.writePackedUtf8String(datastring)number
 
//Get buffers
 
//Gives you left part of stream
bufferStream.getLeftBuffer()Uint8Array
//Gives you unread part of stream
bufferStream.getRightBuffer()Uint8Array
//Gives you unread part of stream as utf8 string
bufferStream.getRightBufferAsUtf8String()string
 
//Utils methods
 
//Move cursor
bufferStream.move(offsetnumber)void
//Check if there is given number of bytes to read
bufferStream.canRead(bytesnumber)boolean
//The same as canRead but throws expection if not
bufferStream.checkToRead(bytesnumber)void
//Returns not yet read number of bytes
bufferStream.toRead()number
 
//Static methods
static readInt8(dataArrayBuffer | ArrayBufferView)number;
static readInt16LE(dataArrayBuffer | ArrayBufferView)number;
static readInt16BE(dataArrayBuffer | ArrayBufferView)number;
static readInt32LE(dataArrayBuffer | ArrayBufferView)number;
static readInt32BE(dataArrayBuffer | ArrayBufferView)number;
static readUInt8(dataArrayBuffer | ArrayBufferView)number;
static readUInt16LE(dataArrayBuffer | ArrayBufferView)number;
static readUInt16BE(dataArrayBuffer | ArrayBufferView)number;
static readUInt32LE(dataArrayBuffer | ArrayBufferView)number;
static readUInt32BE(dataArrayBuffer | ArrayBufferView)number;
static readFloatLE(dataArrayBuffer | ArrayBufferView)number;
static readFloatBE(dataArrayBuffer | ArrayBufferView)number;
static readDoubleLE(dataArrayBuffer | ArrayBufferView)number;
static readDoubleBE(dataArrayBuffer | ArrayBufferView)number;
static writeInt8(datanumber)Uint8Array;
static writeInt16LE(datanumber)Uint8Array;
static writeInt16BE(datanumber)Uint8Array;
static writeInt32LE(datanumber)Uint8Array;
static writeInt32BE(datanumber)Uint8Array;
static writeUInt8(datanumber)Uint8Array;
static writeUInt16LE(datanumber)Uint8Array;
static writeUInt16BE(datanumber)Uint8Array;
static writeUInt32LE(datanumber)Uint8Array;
static writeUInt32BE(datanumber)Uint8Array;
static writeFloatLE(datanumber)Uint8Array;
static writeFloatBE(datanumber)Uint8Array;
static writeDoubleLE(datanumber)Uint8Array;
static writeDoubleBE(datanumber)Uint8Array;
static readBoolean(dataArrayBuffer | ArrayBufferView)boolean;
static writeBoolean(valueboolean)Uint8Array;
static readInt(dataArrayBuffer | ArrayBufferView)number;
static writeInt(datanumber)Uint8Array;
static readUInt(dataArrayBuffer | ArrayBufferView)number;
static writeUInt(datanumber)Uint8Array;
static readPacked(dataArrayBuffer | ArrayBufferView)Uint8Array;
static writePacked(dataArrayBuffer | ArrayBufferView | number[])Uint8Array;
static readUtf8String(dataArrayBuffer | ArrayBufferView, length?: number)string;
static writeUtf8String(datastring)Uint8Array;
static readPackedUtf8String(dataArrayBuffer | ArrayBufferView)string;
static writePackedUtf8String(datastring)Uint8Array;

License

The MIT License (MIT)

Readme

Keywords

none

Package Sidebar

Install

npm i buffer-stream-js

Weekly Downloads

17

Version

1.0.0

License

MIT

Unpacked Size

29.8 kB

Total Files

5

Last publish

Collaborators

  • fabeuluci