byte

Input Buffer and Output Buffer, just like Java ByteBuffer

npm install byte
33 downloads in the last week
87 downloads in the last month

byte

Build Status Coverage Status Dependency Status

NPM

logo

Input Buffer and Output Buffer, just like Java ByteBuffer.

Install

$ npm install byte

Usage

All methods just like Java ByteBuffer, you find them out here.

var ByteBuffer = require('byte');

var bb = ByteBuffer.allocate(1024);
bb.order(ByteBuffer.BIG_ENDIAN); // default is BIG_ENDIAN, you can change it to LITTLE_ENDIAN.
bb.put(0);
bb.put(new Buffer([0, 1, 2]));
bb.put(new Buffer([255, 255, 255, 255]), 10, 3);
bb.put(21, 100);

bb.putChar('a');
bb.putChar(10, 'b');

bb.putInt(1024);
bb.putInt(-100);

bb.putFloat(100.9);

bb.putLong(10000100009099);
bb.putLong('1152921504606847000');

bb.putShort(65535);
bb.putShort(-50000);

bb.putDouble(99.99999);

// wrap for read
var rb = ByteBuffer.wrap(new Buffer(100));
rb.getInt();
rb.getLong();
rb.getChar();
rb.get();
rb.getDouble();
rb.getFloat();

Benchmark

$ node benchmark.js
put()       x 12,028,872 ops/sec ±3.87% (87 runs sampled)
putChar()   x 27,884,212 ops/sec ±1.75% (94 runs sampled)
putShort()  x 1,924,297 ops/sec ±1.94% (96 runs sampled)
putInt()    x 1,949,486 ops/sec ±0.59% (99 runs sampled)
putFloat()  x 1,633,070 ops/sec ±1.14% (96 runs sampled)
putDouble() x 1,138,782 ops/sec ±2.12% (94 runs sampled)
putLong()   x 1,279,507 ops/sec ±2.40% (92 runs sampled)
[Wed Nov 13 2013 17:01:50 GMT+0800 (CST)] Fastest is "putChar()  "

Number methods

putShort / putInt16
putUInt16
putInt / putInt32
putUInt / putUInt32
putInt64
putFloat
putDouble


getShort / getInt16
getUInt16
getInt / getInt32
getUInt / getUInt32
getInt64
getFloat
getDouble

String methods

Java String format: | length (4 bytes int) | string bytes |

C String format: | length + 1 (4 bytes int) | string bytes | \0 |

Row String format: string bytes

putString() and putCString() and putRawString()

bb.putString('foo');
bb.putString(new Buffer('foo'));

bb.putCString('foo');
bb.putCString(new Buffer('foo'));

bb.putRawString('foo');

getString() and getCString() and getRawString(), readRawString()

bb.getString();
bb.getString(10);

bb.getCString();
bb.getCString(10);

bb.getRawString(0, 10);
bb.readRawString(10);

Authors

$ git summary

 project  : byte
 repo age : 3 months
 active   : 12 days
 commits  : 30
 files    : 15
 authors  :
    23  fengmk2                 76.7%
     7  dead_horse              23.3%

License

(The MIT License)

Copyright (c) 2013 - 2014 fengmk2 <fengmk2@gmail.com> and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you