Coder Social home page Coder Social logo

byte's Introduction

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 |

putString() and putCString()

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

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

getString() and getCString()

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

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

Authors

$ git summary

 project  : byte
 repo age : 2 weeks
 active   : 2 days
 commits  : 4
 files    : 13
 authors  :
     4  fengmk2                 100.0%

License

(The MIT License)

Copyright (c) 2013 - 2014 fengmk2 <[email protected]> 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.

byte's People

Contributors

dead-horse avatar fengmk2 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.