1
- ![ ByteBuffer.js - A full-featured ByteBuffer implementation in JavaScript] ( https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png )
1
+ ![ ByteBuffer.js - A full-featured and highly optimized ByteBuffer in JavaScript] ( https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png )
2
2
======================================
3
- Provides a full-featured ByteBuffer implementation using typed arrays. It's one of the core components driving
4
- [ ProtoBuf.js] ( https://github.com/dcodeIO/ProtoBuf.js ) and the [ PSON] ( https://github.com/dcodeIO/PSON ) reference
5
- implementation.
3
+ ByteBuffer.js provides a full-featured and highly optimized ByteBuffer implementation in JavaScript, since version 3
4
+ either backed by an ArrayBuffer for browser environments (ByteBufferAB) or, alternatively, a node Buffer (ByteBufferNB)
5
+ to make use of the performance benefits when running under node.js. Both versions are API-compatible and generated from
6
+ a single source tree using [ MetaScript] ( https://github.com/dcodeIO/MetaScript ) .
6
7
7
- * Note:* The API behind #toHex and #toString has changed with ByteBuffer 2, which is a generally revised release, in
8
- favor of making this more intuitive.
8
+ If you are looking for ByteBuffer.js 2, [ that's the branch] ( https://github.com/dcodeIO/ByteBuffer.js/tree/ByteBuffer2 ) .
9
+
10
+ ** Please note** : Though all old and new test cases are passing, ByteBuffer.js 3 still needs to be tested in real world
11
+ scenarios. Also, the API has changed a bit to make things more straight forward.
9
12
10
13
What can it do?
11
14
---------------
12
15
* Mimics Java ByteBuffers as close as reasonable while using typed array terms
13
- * Signed and unsigned integers ( 8, 16, 32, 64 bit through [ Long.js ] ( https://github.com/dcodeIO/Long.js ) ) with endianness support
16
+ * 8, 16, 32 and 64 bit signed and unsigned integers
14
17
* 32 and 64 bit floats
15
- * Varints as known from protobuf including zig-zag encoding
16
- * Includes an UTF8 and Base64 en-/decoder
17
- * C-strings, V(arint-prefixed)-strings and UTF8 L(ength-prefixed)-strings
18
- * Rich string toolset (to hex, base64, binary, utf8, debug, columns)
19
- * Relative and absolute zero-copy operations
18
+ * Big and little endianness
19
+ * Variable length integers as used in protobuf (32 and 64 bit, including zig zag encoding)
20
+ * Base64, binary, debug, hex and utf8 encodings
21
+ * Handy string and debugging utilities
22
+ * Relative and absolute zero-copy operations wherever possible
20
23
* Manual and automatic resizing (efficiently doubles capacity)
21
24
* Chaining of all operations that do not return a specific value
22
25
* Slicing, appending, prepending, reversing, flip, mark, reset, etc.
23
26
24
- And much more...
25
-
26
- Features
27
- --------
28
- * [ CommonJS] ( http://www.commonjs.org/ ) compatible
29
- * [ RequireJS] ( http://requirejs.org/ ) /AMD compatible
30
- * [ node.js] ( http://nodejs.org ) compatible, also available via [ npm] ( https://npmjs.org/package/bytebuffer )
31
- * Browser compatible
32
- * [ Closure Compiler] ( https://developers.google.com/closure/compiler/ ) ADVANCED_OPTIMIZATIONS compatible (fully annotated,
33
- ` ByteBuffer.min.js ` has been compiled this way, ` ByteBuffer.min.map ` is the source map)
27
+ More
28
+ ----
29
+ * CommonJS, AMD and shim compatible
30
+ * Also available via [ npm] ( https://npmjs.org/package/bytebuffer )
31
+ * Compiled through [ Closure Compiler] ( https://developers.google.com/closure/compiler/ ) using ADVANCED_OPTIMIZATIONS
32
+ (fully annotated, includes externs and source map)
34
33
* Fully documented using [ jsdoc3] ( https://github.com/jsdoc3/jsdoc )
35
- * Well tested through [ nodeunit ] ( https://github.com/caolan/nodeunit )
34
+ * Well tested through [ test.js ] ( https://github.com/dcodeIO/test.js )
36
35
* Zero production dependencies (Long.js is optional)
37
- * Small footprint
38
36
39
37
Usage
40
38
-----
41
- ### Node.js / CommonJS ###
39
+ ### Node.js ###
42
40
* Install: ` npm install bytebuffer `
43
41
44
42
``` javascript
45
43
var ByteBuffer = require (" bytebuffer" );
46
44
var bb = new ByteBuffer ();
47
- bb .writeLString (" Hello world!" ).flip ();
48
- console .log (bb .readLString ()+ " from ByteBuffer.js" );
45
+ bb .writeIString (" Hello world!" ).flip ();
46
+ console .log (bb .readIString ()+ " from ByteBuffer.js" );
49
47
```
50
48
51
49
### Browser ###
@@ -61,11 +59,11 @@ support, you can skip the Long.js include.
61
59
``` javascript
62
60
var ByteBuffer = dcodeIO .ByteBuffer ;
63
61
var bb = new ByteBuffer ();
64
- bb .writeLString (" Hello world!" ).flip ();
65
- alert (bb .readLString ()+ " from ByteBuffer.js" );
62
+ bb .writeIString (" Hello world!" ).flip ();
63
+ alert (bb .readIString ()+ " from ByteBuffer.js" );
66
64
```
67
65
68
- ### Require.js / AMD ###
66
+ ### AMD ###
69
67
70
68
Optionally depends on [ Long.js] ( https://github.com/dcodeIO/Long.js ) for long (int64) support. If you do not require long
71
69
support, you can skip the Long.js config. [ Require.js] ( http://requirejs.org/ ) example:
@@ -79,21 +77,11 @@ require.config({
79
77
});
80
78
require ([" ByteBuffer" ], function (ByteBuffer ) {
81
79
var bb = new ByteBuffer ();
82
- bb .writeLString (" Hello world!" );
83
- bb .flip ();
80
+ bb .writeLString (" Hello world!" ).flip ();
84
81
alert (bb .readLString ()+ " from ByteBuffer.js" );
85
82
});
86
83
```
87
84
88
- On long (int64) support
89
- -----------------------
90
- As of the [ ECMAScript specification] ( http://ecma262-5.com/ELS5_HTML.htm#Section_8.5 ) , number types have a maximum value
91
- of 2^53. Beyond that, behaviour might be unexpected. However, real long support requires the full 64 bits
92
- with the possibility to perform bitwise operations on the value for varint en-/decoding. So, to enable true long support
93
- in ByteBuffer.js, it optionally depends on [ Long.js] ( https://github.com/dcodeIO/Long.js ) , which actually utilizes two
94
- 32 bit numbers internally. If you do not require long support at all, you can skip it and save the additional bandwidth.
95
- On node, long support is available by default through the [ long] ( https://npmjs.org/package/long ) dependency.
96
-
97
85
Downloads
98
86
---------
99
87
* [ ZIP-Archive] ( https://github.com/dcodeIO/ByteBuffer.js/archive/master.zip )
0 commit comments