binbone
v0.2.0
Published
A binary encoder/decoder aimed at achieving optimal space utilization.
Downloads
3
Readme
node-binbone
Node.js(io.js) implemention of binbone, A binary encode specification aimed at achieving optimal space utilization.
Installation
npm i binbone -S
Usage
- Use Block. Block can be use as both an encoder and a decoder.
Block = require("binbone");
block = new Block(1024); // args are the same as a QueueBuffer
block.writeArray([1, 2, 3]);
block.writeUInt("123456789012345"); // Big integer(use [jsbn](https://github.com/andyperlitch/jsbn))
block.readArray();
block.readUInt();
- Use encoder/decoder.
Directly:
Encoder = require("binbone").Encoder;
encodeBlock = new Encoder();
encodeBlock.writeInt(123);
Specify a Buffer for data:
binbone = require('binbone');
buf = new binbone.QueueBuffer();
buf.writeUInt16BE(12);
decoder = new binbone.Decoder(buf);
decoder.readUInt({length: 2});
API
Encoder
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
Decoder
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
TODO
- Record type
License
MIT@Jingchen Zhao