|
1 | 1 | 
|
2 | 2 | ======================================
|
3 |
| -Provides a Java-like, Netty-inspired ByteBuffer implementation using typed arrays. It also tries to abstract a bit of |
| 3 | +Provides a full-features ByteBuffer implementation using typed arrays. It also tries to abstract a bit of |
4 | 4 | the complexity away by providing convenience methods for those who just want to write stuff without caring about signed,
|
5 |
| -unsigned and the actual bit sizes. It's also one of the components driving [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js). |
6 |
| - |
7 |
| -ByteBuffer |
8 |
| ----------- |
9 |
| -* Mimics [Java ByteBuffers](http://docs.oracle.com/javase/1.5.0/docs/api/java/nio/ByteBuffer.html) as close as reasonable while using typed array terms |
10 |
| -* Full 64bit support via [Long.js](https://github.com/dcodeIO/Long.js) (optional) |
11 |
| -* Simple allocation (`new ByteBuffer(capacity[, littleEndian])` or `ByteBuffer.allocate(capacity[, littleEndian])`) |
12 |
| -* Wrapping of quite everything which is or includes an ArrayBuffer (`ByteBuffer.wrap(buffer[, littleEndian])`) |
13 |
| -* Cloning using the same (`ByteBuffer#clone()`) and copying using an independent backing buffer (`ByteBuffer#copy()`) |
14 |
| -* Slicing using the same (`ByteBuffer#slice(begin, end)`) and using an indepentent backing buffer (`ByteBuffer#sliceAndCompact(begin, end)`) |
15 |
| -* Manual offset (`ByteBuffer#offset` and `ByteBuffer#length`) and array manipulation (`ByteBuffer#array`) |
16 |
| -* Remaining readable bytes (`ByteBuffer#remaining()`) and backing buffer capacity getters (`ByteBuffer#capacity()`) |
17 |
| -* Explicit (`ByteBuffer#resize(capacity)`) and implicit resizing (`ByteBuffer#ensureCapacity(capacity)`) |
18 |
| -* Efficient implicit resizing by doubling the current capacity |
19 |
| -* Flipping (`ByteBuffer#flip()`), marking (`ByteBuffer#mark([offset])`) and resetting (`ByteBuffer#reset()`) |
20 |
| -* Compacting of the backing buffer (`ByteBuffer#compact()`) |
21 |
| -* Conversion to ArrayBuffer (`ByteBuffer#toArrayBuffer([forceCopy])`) (i.e. to send data over the wire, e.g. a WebSocket |
22 |
| - with `binaryType="arraybuffer"`) |
23 |
| -* Conversion to Buffer (`ByteBuffer#toBuffer()`) if running inside of node.js |
24 |
| -* Reversing (`ByteBuffer#reverse()`), appending (`ByteBuffer#append(src[, offset])`) and prepending |
25 |
| - (`ByteBuffer#prepend(src[, offset])`) of other ByteBuffers with implicit capacity management |
26 |
| -* Explicit destruction (`ByteBuffer#destroy()`) |
27 |
| -* `ByteBuffer#writeUint/Int8/16/32/64(value[, offset])` and `ByteBuffer#readUint/Int8/16/32/64([offset])` |
28 |
| -* `ByteBuffer#writeVarint32/64(value[, offset])` and `ByteBuffer#readVarint32/64([offset])` to write a base 128 |
29 |
| - variable-length integer as used in [protobuf](https://developers.google.com/protocol-buffers/docs/encoding#varints) |
30 |
| -* `ByteBuffer#writeZigZagVarint32/64(value[, offset])` and `ByteBuffer#readZigZagVarint32/64([offset])` to write a |
31 |
| - zig-zag encoded base 128 variable-length integer as used in protobuf for efficient encoding of signed values |
32 |
| -* `ByteBuffer#writeFloat32/64(value[, offset])` and `ByteBuffer#readFloat32/64([offset])` |
33 |
| -* `ByteBuffer#write/readByte`, `ByteBuffer#write/readShort`, `ByteBuffer#write/readInt`, `ByteBuffer#write/readLong` |
34 |
| - (all signed), `ByteBuffer#write/readVarint` and `ByteBuffer#write/readZigZagVarint` (both 32bit signed), |
35 |
| - `ByteBuffer#write/readFloat`, `ByteBuffer#write/readDouble` aliases for the above for convenience |
36 |
| -* `ByteBuffer#writeUTF8String(str[, offset])`, `ByteBuffer#readUTF8String(chars[, offset])` and |
37 |
| - `ByteBuffer#readUTF8StringBytes(length[, offset])` using the included UTF8 en-/decoder (full 6 bytes, |
38 |
| - [ref](http://en.wikipedia.org/wiki/UTF-8#Description)) |
39 |
| -* `ByteBuffer.encode64(bb)`, `ByteBuffer.decode64(str)` and `ByteBuffer#toBase64()` using the included Base64 |
40 |
| - en/-decoder. |
41 |
| -* `ByteBuffer#writeLString(str[, offset]))` and `ByteBuffer#readLString([offset])` to write respectively read a |
42 |
| - length-prepended (number of characters as UTF8 char) string |
43 |
| -* `ByteBuffer#writeVString(str[, offset]))` and `ByteBuffer#readVString([offset])` to write respectively read a |
44 |
| - length-prepended (number of bytes as base 128 variable-length 32bit integer) string |
45 |
| -* `ByteBuffer#writeCString(str[, offset])` and `ByteBuffer#readCString([offset])` to write respectively read a |
46 |
| - NULL-terminated (Uint8 0x00) string |
47 |
| -* `ByteBuffer#writeJSON(data[, offset[, stringify]])` and `ByteBuffer#readJSON([offset[, parse]])` to write respectively |
48 |
| - read arbitraty object data. Allows overriding the default stringify (default: JSON.stringify) and parse (default: |
49 |
| - JSON.parse) implementations. |
50 |
| -* All with implicit offset advance if the offset parameter is omitted or without, if specified |
51 |
| -* Chaining of all operations that allow this (i.e. do not return some specific value like in read operations), e.g. |
52 |
| - |
53 |
| - ```javascript |
54 |
| - var bb = new ByteBuffer(); |
55 |
| - ... |
56 |
| - bb.reset().writeInt(1).writeLString("Hello world!").flip().compact()... |
57 |
| - ``` |
58 |
| - |
59 |
| -* Switching between little endian and big endian byte order through `ByteBuffer#LE()` and `ByteBuffer#BE()`, e.g. |
60 |
| - |
61 |
| - ```javascript |
62 |
| - var bb = new ByteBuffer(8).LE().writeInt(1).BE().writeInt(2).flip(); // toHex: <01 00 00 00 00 00 00 02> |
63 |
| - ``` |
64 |
| - |
65 |
| -* `ByteBuffer#toString([enc])`, `ByteBuffer#toHex([wrap])`, `ByteBuffer#toASCII([wrap])`, `ByteBuffer#toUTF8()`, |
66 |
| - `ByteBuffer#toBase64()` and `ByteBuffer#printDebug()` (emits hex + ASCII + offsets to console, looks like your |
67 |
| - favourite hex editor) for pain-free debugging |
68 |
| - |
| 5 | +unsigned and the actual bit sizes. It's also one of the components driving [ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js) |
| 6 | +and the [PSON](https://github.com/dcodeIO/PSON) reference implementation. |
| 7 | + |
| 8 | +*Note:* The API behind toHex and toString has changed with ByteBuffer 2.0.0, which is a generally revised release, in |
| 9 | +favor of making this more intuitive. |
| 10 | + |
| 11 | +What can it do? |
| 12 | +--------------- |
| 13 | +* Mimics Java ByteBuffers as close as reasonable while using typed array terms |
| 14 | +* Signed and unsigned integers (8, 16, 32, 64 bit through [Long.js](https://github.com/dcodeIO/Long.js)) with endianness support |
| 15 | +* Varints as known from protobuf including zig-zag encoding |
| 16 | +* Includes an UTF8 and Base64 en-/decoder |
| 17 | +* C-strings, Varint-prefixed strings and UTF8 length-prefixed strings |
| 18 | +* Rich string toolset (to hex, base64, utf8, debug, columns) |
| 19 | +* Relative and absolute zero-copy operations |
| 20 | +* Automatic resizing (always doubles) |
| 21 | +* Chaining of all operations that do not return a specific value |
| 22 | +* Slicing, appending, prepending etc. |
| 23 | + |
| 24 | +And much more... (see the API documentation) |
| 25 | + |
69 | 26 | Features
|
70 | 27 | --------
|
71 | 28 | * [CommonJS](http://www.commonjs.org/) compatible
|
|
0 commit comments