A binary encoder/decoder aimed at achieving optimal space utilization.
npm install binbonenode-binbone
=================
Node.js(io.js) implemention of binbone, A binary encode specification aimed at achieving optimal space utilization.



bash
npm i binbone -S
`Usage
- Use Block. Block can be use as both an encoder and a decoder.`javascript
Block = require("binbone");
block = new Block(1024); // args are the same as a QueueBufferblock.writeArray([1, 2, 3]);
block.writeUInt("123456789012345"); // Big integer(use jsbn)
block.readArray();
block.readUInt();
`- Use encoder/decoder.
Directly:
`javascript
Encoder = require("binbone").Encoder;
encodeBlock = new Encoder();encodeBlock.writeInt(123);
`Specify a Buffer for data:
`javascript
binbone = require('binbone');
buf = new binbone.QueueBuffer();
buf.writeUInt16BE(12);
decoder = new binbone.Decoder(buf);
decoder.readUInt({length: 2});
`API
$3
- #### constructor (outputBlock)
constructor
- param:
outputBlock { _FlexBuffer_ } An BinboneBlock Object
- #### writeTo (outputBlock)
Reset data block
- param:
outputBlock { _FlexBuffer_ } An BinboneBlock Object
- #### writeByte (value = 0)
Write a byte.
- param:
value { _number=0_ } byte value
- return: { _number_ }
length to write (always 1)
- #### writeBoolean (value) (alias: writeBool)
Write a boolean value.
- param:
value { _boolean_ } boolean value
- return: { _number_ }
length to write (always 1)
- #### writeUInt (num = 0 | string, opts = {}) (alias: writeLength, writeSign)
Write an unsigned integer, using variable-length coding.
- param:
num { _number=0 | string_ } integer, use string for any big integer
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of integer (1, 2, 4, 8)
- return: { _number_ }
length to write
- #### writeInt (opts = {}) (alias: writeLong)
Write an signed integer, using zig-zag variable-length coding.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of integer (1, 2, 4, 8)
- return: { _number_ }
length to write
- #### writeFloat (value = 0)
Write a float.
- param:
value { _number=0_ } float point number
- return: { _number_ }
length to write (always 4)
- #### writeDouble (value = 0)
Write a double.
- param:
value { _number=0_ } float point number
- return: { _number_ }
length to write (always 8)
- #### writeBytes (values, opts = {})
Write bytes.
- param:
values { _Array | Buffer_ } bytes
- param:
opts { _Object={}_ } options
- option:
length { _number_ } number of bytes
- return: { _number_ }
length to write
- #### writeString (str, opts = {})
Write a string.
- param:
str { _string_ } string
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of string
- return: { _number_ }
length to write
- #### writeMap (map = {}, opts = {})
Write a map.
- param:
map { _Object | Map = {}_ } key-value map
- param:
opts { _Object={}_ } options
- option:
length { _number_ } size of map
- option:
keyType { _string|Object_ } type of key [required]
- option:
valueType { _string|Object_ } type of value [required]
- return: { _number_ }
length to write
- #### writeArray (arr = [], opts = {})
Write an array of data.
- param:
arr { _Array=[]_ } Array
- param:
opts { _Object={}_ } options
- option:
length { _number_ } length of array
- option:
valueType { _string|Object_ } type of array item
- return: { _number_ }
length to write
- #### writeObject (obj = {}, opts = {})
Write an object.
- param:
obj { _Object={}_ } object
- param:
opts { _Object={}_ } options
- option:
length { _number_ } size of object
- option:
valueType { _string|Object_ } type of object value
- return: { _number_ }
length to write
$3
- #### constructor (inputBlock)
constructor
- param:
inputBlock { _QueueBuffer_ } An QueueBuffer Object
- #### readFrom (inputBlock)
Reset data block
- param:
inputBlock { _QueueBuffer_ } An QueueBuffer Object
- #### readByte ()
Read a single byte.
- return: { _number_ }
byte
- #### skipByte ()
Skip a single byte.
- #### readBoolean () (alias: readBool)
Read boolean value.
- return: { _Boolean_ }
boolean value
- #### skipBoolean () (alias: skipBool)
skip a boolean value.
- #### readUInt (opts = {}) (alias: readLength, readSign)
Read an unsigned integer.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of integer (1, 2, 4, 8)
- return: { _number|string_ }
integer, string for big integer
- #### skipUInt (opts = {}) (alias: skipLength, skipSign)
Skip an unsigned integer.
- param:
opts { _Object={}_ } options (see readUint)
- #### readInt (opts = {}) (alias: readLong)
Read an signed integer.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of integer (1, 2, 4, 8)
- return: { _number|string_ }
integer, string for big integer
- #### skipInt (opts = {}) (alias: skipLong)
Skip a signed integer.
- param:
opts { _Object={}_ } options(see readInt)
- #### readFloat ()
Read a float.
- return: { _Number_ }
float number
- #### skipFloat ()
Skip a float.
- #### readDouble ()
Read a double.
- return: { _number_ }
double number
- #### skipDouble ()
Skip a double.
- #### readBytes (opts = {})
Read bytes.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } number of bytes
- return: { _Buffer_ }
bytes
- #### skipBytes (opts = {})
Skip bytes
- param:
opts { _Object={}_ } options(see readBytes)
- #### readString (opts = {})
Read a string.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } byte length of string
- return: { _string_ }
string
- #### skipString (opts = {})
Skip a string.
- param:
opts { _Object={}_ } options(see readString)
- #### readMap (opts = {})
Read a map.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } size of map
- option:
keyType { _string|Object_ } type of key[required]
- option:
valueType { _string|Object_ } type of value[required]
- return: { _Map(es6)|Object(else)_ }
map
- #### skipMap (opts = {})
Skip a map.
- param:
opts { _Object={}_ } options(see readMap)
- #### readArray (opts = {})
Read an array.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } length of array
- option:
valueType { _string|Object_ } type of array item
- return: { _Array_ }
array
- #### skipArray (opts = {})
Skip an array.
- param:
opts { _Object={}_ } options(see readArray)
- #### readObject (opts = {})
Read an object.
- param:
opts { _Object={}_ } options
- option:
length { _number_ } size of object
- option:
valueType { _string|Object_ } type of object value
- return: { _Object_ }
object
- #### skipObject (opts = {})
Skip an array.
- param:
opts { _Object={}_ } options(see readObject)
Test
`
npm test
``