Package Exports
- bytebuffer
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bytebuffer) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ByteBuffer.js - A Java-like ByteBuffer
Provides a Java-like ByteBuffer implementation using typed arrays. It also tries to abstract the complexity away by providing convenience methods for those who just want to write stuff without caring about signed, unsigned and the actual bit sizes. It's also used for the cross-platform multiplayer component in eSoccer, a HTML5 game developed at University of Applied Sciences Bonn.
ByteBuffer
- Mimics Java ByteBuffers as close as reasonable
- Allocation through new ByteBuffer([capacity, [littleEndian]]) or ByteBuffer#allocate
- Wrapping of plain ArrayBuffers or any object with an "array" or "buffer" property which is an ArrayBuffer through ByteBuffer.wrap(buffer[, littleEndian])
- Cloning by maintaining the reference to the underlying ArrayBuffer (ByteBuffer#clone)
- Copying to use an independent ArrayBuffer for the copy through ByteBuffer#copy
- Slicing while maintaining the reference to the underlying ArrayBuffer through ByteBuffer#slice
- Slicing to use an independent ArrayBuffer for the copy through ByteBuffer#sliceAndCompact
- Manual offset manipulation throught ByteBuffer#offset and ByteBuffer#length
- Manual array manipulation through ByteBuffer#array
- Explicit resizing through ByteBuffer#resize(capacity)
- Implicit resizing through ByteBuffer#ensureCapacity(newCapacity) on each operation. Doubles the capacity by default to be efficient in speed while maintaining reasonable memory usage.
- Hate it or love it: ByteBuffer#flip and ByteBuffer#reset
- Compacting to a an ArrayBuffer of its actual size (offset to length) through ByteBuffer#compact. Will implicitly flip if required. This is used to send ByteBuffer data over the wire (e.g. a WebSocket with binaryType="arraybuffer").
- If you do not want to care about compacting yourself, use ByteBuffer#toArrayBuffer to do all the stuff automatically and just send the data over whatever wire you prefer afterwards. Will return a reference to the backing buffer if already in a compacted form unless the forceCopy parameter is set to true.
- Explicit destruction through ByteBuffer#destroy
- writeInt8, readInt8 with alias writeByte, readByte
- writeUint8, readUint8
- writeInt16, readInt16 with aliases writeShort, readShort
- writeUint16, readUint16
- writeInt32, readInt32 with aliases writeInt, readInt
- writeUint32, readUint32
- writeFloat32, readFloat32 with aliases writeFloat, readFloat
- writeFloat64, readFloat64 with aliases writeDouble, readDouble
- writeUTF8String, readUTF8String
- writeLString, readLString to write respectively read a length-prepended (as UTF8 character) string
- writeCString, readCString to write respectively read a NULL-terminated (Uint8) string
- writeJSON, readJSON to stringify and write respectivly to read and parse JSON data. Allows overriding the default stringify (default: JSON.stringify) and parse (default: JSON.parse) implementations.
- All with implicit offset advance if the offset parameter is omitted or without, if specified.
- Chaining for all operations that allow this (i.e. do not return some specific value like in read operations), e.g. bb.writeInt(1).writeString("Hello world!")...
- Provides ByteBuffer#toString, ByteBuffer#toHex and ByteBuffer#printDebug
- Includes an UTF8 encoder and decoder (full 1-6 bytes, ref) available through ByteBuffer.encodeUTF8Char and ByteBuffer.decodeUTF8Char
Features
- CommonJS compatible
- RequireJS/AMD compatible
- Shim compatible (include the script, then use var ByteBuffer = dcodeIO.ByteBuffer;)
- node.js compatible, also available via npm (npm install bytebuffer)
- Closure Compiler ADVANCED_OPTIMIZATIONS compatible (fully annotated)
- Fully documented (jsdoc3)
- Tested through nodeunit (TODO: heavily test UTF8 en-/decoding)
- Zero dependencies
- Small footprint
Documentation
Tests (& Examples)
Prerequisites to run it against IE<10, FF<15, Chrome<9 etc.
License
Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html