What is bytebuffer?
The bytebuffer npm package is a utility for working with binary data in JavaScript. It provides a comprehensive API for reading and writing various data types to and from byte buffers, making it useful for tasks such as serialization, deserialization, and network communication.
What are bytebuffer's main functionalities?
Creating a ByteBuffer
This feature allows you to create a new ByteBuffer instance. ByteBuffers are used to manage binary data efficiently.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
Writing Data to a ByteBuffer
This feature allows you to write various data types to a ByteBuffer. In this example, an integer and a float are written to the buffer.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
bb.writeFloat32(3.14);
Reading Data from a ByteBuffer
This feature allows you to read various data types from a ByteBuffer. In this example, an integer and a float are read from the buffer after writing.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
bb.writeFloat32(3.14);
bb.flip();
const intVal = bb.readInt32();
const floatVal = bb.readFloat32();
Handling Endianness
This feature allows you to specify the endianness (byte order) of the ByteBuffer. In this example, the buffer is set to little-endian mode.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer(ByteBuffer.LITTLE_ENDIAN);
bb.writeInt32(123456);
bb.flip();
const intVal = bb.readInt32();
Converting ByteBuffer to ArrayBuffer
This feature allows you to convert a ByteBuffer to an ArrayBuffer, which can be useful for interoperability with other APIs that use ArrayBuffers.
const ByteBuffer = require('bytebuffer');
const bb = new ByteBuffer();
bb.writeInt32(123456);
const arrayBuffer = bb.toArrayBuffer();
Other packages similar to bytebuffer
buffer
The buffer package is a Node.js core module that provides a way of handling binary data. It is similar to bytebuffer in that it allows for reading and writing various data types, but it is more integrated into the Node.js ecosystem and is generally more performant.
protobufjs
The protobufjs package is a library for working with Protocol Buffers, a binary serialization format. While it offers more advanced serialization capabilities compared to bytebuffer, it is more complex and specifically designed for Protocol Buffers.
dataview
The dataview package provides a DataView implementation for Node.js, allowing for low-level manipulation of ArrayBuffers. It is similar to bytebuffer in terms of functionality but is more low-level and requires more manual management of buffers.
Provides a full-featured ByteBuffer implementation using typed arrays. It's one of the core components driving
ProtoBuf.js and the PSON reference
implementation.
Note: The API behind #toHex and #toString has changed with ByteBuffer 2, which is a generally revised release, in
favor of making this more intuitive.
What can it do?
- Mimics Java ByteBuffers as close as reasonable while using typed array terms
- Signed and unsigned integers (8, 16, 32, 64 bit through Long.js) with endianness support
- 32 and 64 bit floats
- Varints as known from protobuf including zig-zag encoding
- Includes an UTF8 and Base64 en-/decoder
- C-strings, V(arint-prefixed)-strings and UTF8 L(ength-prefixed)-strings
- Rich string toolset (to hex, base64, utf8, debug, columns)
- Relative and absolute zero-copy operations
- Manual and automatic resizing (efficiently doubles capacity)
- Chaining of all operations that do not return a specific value
- Slicing, appending, prepending, reversing, flip, mark, reset, etc.
And much more...
Features
- CommonJS compatible
- RequireJS/AMD compatible
- node.js compatible, also available via npm
- Browser compatible
- Closure Compiler ADVANCED_OPTIMIZATIONS compatible (fully annotated,
ByteBuffer.min.js
has been compiled this way, ByteBuffer.min.map
is the source map) - Fully documented using jsdoc3
- Well tested through nodeunit
- Zero production dependencies (Long.js is optional)
- Small footprint
Usage
Node.js / CommonJS
- Install:
npm install bytebuffer
var ByteBuffer = require("bytebuffer");
var bb = new ByteBuffer();
bb.writeLString("Hello world!").flip();
console.log(bb.readLString()+" from ByteBuffer.js");
Browser
Optionally depends on Long.js for long (int64) support. If you do not require long
support, you can skip the Long.js include.
<script src="Long.min.js"></script>
<script src="ByteBuffer.min.js"></script>
var ByteBuffer = dcodeIO.ByteBuffer;
var bb = new ByteBuffer();
bb.writeLString("Hello world!").flip();
alert(bb.readLString()+" from ByteBuffer.js");
Require.js / AMD
Optionally depends on Long.js for long (int64) support. If you do not require long
support, you can skip the Long.js config. Require.js example:
require.config({
"paths": {
"Long": "/path/to/Long.js"
"ByteBuffer": "/path/to/ByteBuffer.js"
}
});
require(["ByteBuffer"], function(ByteBuffer) {
var bb = new ByteBuffer();
bb.writeLString("Hello world!");
bb.flip();
alert(bb.readLString()+" from ByteBuffer.js");
});
On long (int64) support
As of the ECMAScript specification, number types have a maximum value
of 2^53. Beyond that, behaviour might be unexpected. However, real long support requires the full 64 bits
with the possibility to perform bitwise operations on the value for varint en-/decoding. So, to enable true long support
in ByteBuffer.js, it optionally depends on Long.js, which actually utilizes two
32 bit numbers internally. If you do not require long support at all, you can skip it and save the additional bandwidth.
On node, long support is available by default through the long dependency.
Downloads
Documentation
Tests (& Examples)
Support for IE<10, FF<15, Chrome<9 etc.
- Requires working ArrayBuffer & DataView implementations (i.e. use a polyfill)
Contributors
Dretch (IE8 compatibility)
License
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html