Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bytebuffer

Package Overview
Dependencies
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bytebuffer - npm Package Compare versions

Comparing version 0.9.3 to 0.9.4

.travis.yml

1879

ByteBuffer.js

@@ -20,967 +20,970 @@ /*

* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/ByteBuffer.js for details
* see: https://github.com/ByteBuffer.js for details
*/
"use strict";
/**
* @namespace
*/
var dcodeIO = typeof dcodeIO != 'undefined' ? dcodeIO : {};
/**
* Constructs a new ByteBuffer.
* @exports dcodeIO.ByteBuffer
* @class 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.
* @param {number=} capacity Initial capacity. Defaults to {@link dcodeIO.ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to false.
* @constructor
*/
dcodeIO.ByteBuffer = function(capacity, littleEndian) {
(function(window) {
"use strict";
capacity = typeof capacity != 'undefined' ? parseInt(capacity, 10) : dcodeIO.ByteBuffer.DEFAULT_CAPACITY;
if (capacity < 1) capacity = dcodeIO.ByteBuffer.DEFAULT_CAPACITY;
/**
* Underlying ArrayBuffer.
* @type {ArrayBuffer}
* Constructs a new ByteBuffer.
* @exports ByteBuffer
* @class Provides a Java-like ByteBuffer implementation using typed arrays. It also tries to abstract a bit of 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.
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to false.
* @constructor
*/
this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity);
var ByteBuffer = function(capacity, littleEndian) {
capacity = typeof capacity != 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY;
if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY;
/**
* DataView to mess with the ArrayBuffer.
* @type {DataView}
*/
this.view = this.array != null ? new DataView(this.array) : null;
/**
* Underlying ArrayBuffer.
* @type {ArrayBuffer}
*/
this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity);
/**
* DataView to mess with the ArrayBuffer.
* @type {DataView}
*/
this.view = this.array != null ? new DataView(this.array) : null;
/**
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset and
* length, which are both absolute indexes. There is no capacity property, use {@link ByteBuffer#capacity}
* instead.
* @type {number}
*/
this.offset = 0;
/**
* Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between offset and
* length, which are both absolute indexes. There is no capacity property, use {@link ByteBuffer#capacity}
* instead.
* @type {number}
*/
this.length = 0;
/**
* Whether to use little endian multi byte values.
* @type {boolean}
*/
this.littleEndian = typeof littleEndian != 'undefined' ? !!littleEndian : false;
};
/**
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset and
* length, which are both absolute indexes. There is no capacity property, use {@link dcodeIO.ByteBuffer#capacity}
* instead.
* Default buffer capacity of 32 if nothing else is stated. The ByteBuffer will be automatically resized by a factor
* of 2 if required.
* @type {number}
* @const
*/
this.offset = 0;
ByteBuffer.DEFAULT_CAPACITY = 32;
/**
* Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between offset and
* length, which are both absolute indexes. There is no capacity property, use {@link dcodeIO.ByteBuffer#capacity}
* instead.
* @type {number}
* Little endian constant for usage in constructors instead of a boolean value. Evaluates to true.
* @type {boolean}
* @const
*/
this.length = 0;
ByteBuffer.LITTLE_ENDIAN = true;
/**
* Whether to use little endian multi byte values.
* Big endian constant for usage in constructors instead of a boolean value. Evaluates to false.
* @type {boolean}
* @const
*/
this.littleEndian = typeof littleEndian != 'undefined' ? !!littleEndian : false;
};
/**
* Default buffer capacity of 32 if nothing else is stated. The ByteBuffer will be automatically resized by a factor
* of 2 if required.
* @type {number}
* @const
*/
dcodeIO.ByteBuffer.DEFAULT_CAPACITY = 32;
/**
* Little endian constant for usage in constructors instead of a boolean value. Evaluates to true.
* @type {boolean}
* @const
*/
dcodeIO.ByteBuffer.LITTLE_ENDIAN = true;
/**
* Big endian constant for usage in constructors instead of a boolean value. Evaluates to false.
* @type {boolean}
* @const
*/
dcodeIO.ByteBuffer.BIG_ENDIAN = false;
/**
* Allocates a new ByteBuffer.
* @param {number=} capacity Initial capacity. Defaults to {@link dcodeIO.ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
* @return {dcodeIO.ByteBuffer}
*/
dcodeIO.ByteBuffer.allocate = function(capacity, littleEndian) {
return new dcodeIO.ByteBuffer(capacity, littleEndian);
};
/**
* Wraps an ArrayBuffer. Sets the ByteBuffer's offset to 0 and its length to the specified ArrayBuffer's byte length.
* @param {ArrayBuffer|*} buffer ArrayBuffer or any object with an object#array or object#buffer property to wrap
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
* @return {dcodeIO.ByteBuffer}
*/
dcodeIO.ByteBuffer.wrap = function(buffer, littleEndian) {
if (!!buffer["array"]) {
buffer = buffer["array"];
} else if (!!buffer["buffer"]) {
buffer = buffer["buffer"];
}
if (!(buffer instanceof ArrayBuffer)) {
throw("Cannot wrap buffer of type "+typeof(buffer));
}
var b = new dcodeIO.ByteBuffer(0, littleEndian, /* shadow copy */ true);
b.array = buffer;
b.view = new DataView(b.array);
b.offset = 0;
b.length = buffer.byteLength;
return b;
};
/**
* Resizes the ByteBuffer to the given capacity.
* @param {number} capacity New capacity
* @return {boolean} true if actually resized, false if already that large or larger
*/
dcodeIO.ByteBuffer.prototype.resize = function(capacity) {
if (this.array == null && capacity > 0) { // Silently recreate
this.array = new ArrayBuffer(capacity);
this.view = new DataView(this.array);
}
if (this.array.byteLength < capacity) {
var src = this.array;
var srcView = new Uint8Array(src);
var dst = new ArrayBuffer(capacity);
ByteBuffer.BIG_ENDIAN = false;
/**
* Allocates a new ByteBuffer.
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
* @return {ByteBuffer}
*/
ByteBuffer.allocate = function(capacity, littleEndian) {
return new ByteBuffer(capacity, littleEndian);
};
/**
* Wraps an ArrayBuffer. Sets the ByteBuffer's offset to 0 and its length to the specified ArrayBuffer's byte length.
* @param {ArrayBuffer|*} buffer ArrayBuffer or any object with an object#array or object#buffer property to wrap
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
* @return {ByteBuffer}
*/
ByteBuffer.wrap = function(buffer, littleEndian) {
if (!!buffer["array"]) {
buffer = buffer["array"];
} else if (!!buffer["buffer"]) {
buffer = buffer["buffer"];
}
if (!(buffer instanceof ArrayBuffer)) {
throw("Cannot wrap buffer of type "+typeof(buffer));
}
var b = new ByteBuffer(0, littleEndian, /* shadow copy */ true);
b.array = buffer;
b.view = new DataView(b.array);
b.offset = 0;
b.length = buffer.byteLength;
return b;
};
/**
* Resizes the ByteBuffer to the given capacity.
* @param {number} capacity New capacity
* @return {boolean} true if actually resized, false if already that large or larger
*/
ByteBuffer.prototype.resize = function(capacity) {
if (this.array == null && capacity > 0) { // Silently recreate
this.array = new ArrayBuffer(capacity);
this.view = new DataView(this.array);
}
if (this.array.byteLength < capacity) {
var src = this.array;
var srcView = new Uint8Array(src);
var dst = new ArrayBuffer(capacity);
var dstView = new Uint8Array(dst);
dstView.set(srcView);
this.array = dst;
this.view = new DataView(dst);
return true;
}
return false;
};
/**
* Slices the ByteBuffer. This is independent of the ByteBuffer's actual offsets. Does not compact the underlying
* ArrayBuffer (use {@link ByteBuffer#compact} and maybe {@link ByteBuffer.wrap} instead).
* @param {number} begin Begin offset
* @param {number} end End offset
* @return {ByteBuffer} Clone of this ByteBuffer with the specified slicing applied, backed by the same ArrayBuffer
*/
ByteBuffer.prototype.slice = function(begin, end) {
if (this.array == null) {
throw(this+" cannot be sliced: Already destroyed");
}
if (end <= begin) {
throw(this+" cannot be sliced: End ("+end+") is less than begin ("+begin+")");
}
if (begin < 0 || begin > this.array.byteLength || end < 1 || end > this.array.byteLength) {
throw(this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+begin+"-"+end+")");
}
var b = this.clone();
b.offset = begin;
b.length = end;
return b;
};
/**
* Slices and compacts the ByteBuffer. The resulting ByteBuffer will have its own ArrayBuffer with the compacted contents
* of this ByteBuffer's contents.
* @param {number} begin Begin offset
* @param {number} end End offset
* @returns {ByteBuffer}
*/
ByteBuffer.prototype.sliceAndCompact = function(begin, end) {
return ByteBuffer.wrap(this.slice(begin,end).toArrayBuffer(true));
};
/**
* Makes sure that the specified capacity is available. If the current capacity is exceeded, it will be doubled. If
* double the previous capacity is less than the required capacity, the required capacity will be used.
* @param {number} capacity Required capacity
* @return {boolean} true if actually resized, false if already that large or larger
*/
ByteBuffer.prototype.ensureCapacity = function(capacity) {
if (this.array == null) {
return this.resize(capacity);
}
if (this.array.byteLength < capacity) return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
return false;
};
/**
* Flips the ByteBuffer. Sets length=offset and offset=0.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.flip = function() {
if (this.array == null) {
throw(this+" cannot be flipped: Already destroyed");
}
this.length = this.offset;
this.offset = 0;
return this;
};
/**
* Resets the ByteBuffer. Sets offset=0 and length=0.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.reset = function() {
this.offset = 0;
this.length = 0;
return this;
};
/**
* Clones this ByteBuffer. The returned cloned ByteBuffer shares the same ArrayBuffer but will have its own offsets.
* @return {ByteBuffer}
*/
ByteBuffer.prototype.clone = function() {
// When cloning, an undocumented third parameter is used to set array and view manually.
var b = new ByteBuffer(-1, this.littleEndian, /* shadow copy */ true);
b.array = this.array;
b.view = this.view;
b.offset = this.offset;
b.length = this.length;
return b;
};
/**
* Copies this ByteBuffer. The returned copied ByteBuffer has its own ArrayBuffer and uses the same offsets as this one.
* @return {ByteBuffer}
*/
ByteBuffer.prototype.copy = function() {
var b = new ByteBuffer(this.array.byteLength, this.littleEndian);
var src = new Uint8Array(this.array);
var dst = new Uint8Array(b.array);
dst.set(src);
b.offset = this.offset;
b.length = this.length;
return b;
};
/**
* Gets the number of remaining readable bytes. Contents are the bytes between offset and length, so this returns
* length-offset.
* @returns {number} Remaining readable bytes (may be negative if offset is larger than length)
*/
ByteBuffer.prototype.remaining = function() {
return this.length - this.offset;
};
/**
* Gets the capacity of the backing buffer. May be larger but not less than the contents actual length. Contents are the
* bytes between offset and length, which is independent of the actual capacity.
* @returns {number} Capacity of the backing buffer or 0 if destroyed
*/
ByteBuffer.prototype.capacity = function() {
return this.array != null ? this.array.byteLength : 0;
};
/**
* Compacts the ByteBuffer to be backed by an ArrayBuffer of its actual length. Will {@link ByteBuffer#flip} the
* ByteBuffer if its offset is larger than its length. If the ByteBuffer's offset is less than its length, only the
* portion between its offset and length will be contained in the compacted backing buffer. Will set offset=0 and
* length=capacity. Will do nothing but flipping, if required, if already compacted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.compact = function() {
if (this.array == null) {
throw(this+" cannot be compacted: Already destroyed");
}
if (this.offset > this.length) {
this.flip();
}
if (this.offset == this.length) {
throw(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")");
}
if (this.offset == 0 && this.length == this.array.byteLength) {
return this; // Already compacted
}
var srcView = new Uint8Array(this.array);
var dst = new ArrayBuffer(this.length-this.offset);
var dstView = new Uint8Array(dst);
dstView.set(srcView);
dstView.set(srcView.subarray(this.offset, this.length));
this.array = dst;
this.view = new DataView(dst);
return true;
}
return false;
};
/**
* Slices the ByteBuffer. This is independent of the ByteBuffer's actual offsets. Does not compact the underlying
* ArrayBuffer (use {@link dcodeIO.ByteBuffer#compact} and maybe {@link dcodeIO.ByteBuffer.wrap} instead).
* @param {number} begin Begin offset
* @param {number} end End offset
* @return {dcodeIO.ByteBuffer} Clone of this ByteBuffer with the specified slicing applied, backed by the same ArrayBuffer
*/
dcodeIO.ByteBuffer.prototype.slice = function(begin, end) {
if (this.array == null) {
throw(this+" cannot be sliced: Already destroyed");
}
if (end <= begin) {
throw(this+" cannot be sliced: End ("+end+") is less than begin ("+begin+")");
}
if (begin < 0 || begin > this.array.byteLength || end < 1 || end > this.array.byteLength) {
throw(this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+begin+"-"+end+")");
}
var b = this.clone();
b.offset = begin;
b.length = end;
return b;
};
/**
* Slices and compacts the ByteBuffer. The resulting ByteBuffer will have its own ArrayBuffer with the compacted contents
* of this ByteBuffer's contents.
* @param {number} begin Begin offset
* @param {number} end End offset
* @returns {dcodeIO.ByteBuffer}
*/
dcodeIO.ByteBuffer.prototype.sliceAndCompact = function(begin, end) {
return dcodeIO.ByteBuffer.wrap(this.slice(begin,end).toArrayBuffer(true));
};
/**
* Makes sure that the specified capacity is available. If the current capacity is exceeded, it will be doubled. If
* double the previous capacity is less than the required capacity, the required capacity will be used.
* @param {number} capacity Required capacity
* @return {boolean} true if actually resized, false if already that large or larger
*/
dcodeIO.ByteBuffer.prototype.ensureCapacity = function(capacity) {
if (this.array == null) {
return this.resize(capacity);
}
if (this.array.byteLength < capacity) return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
return false;
};
/**
* Flips the ByteBuffer. Sets length=offset and offset=0.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.flip = function() {
if (this.array == null) {
throw(this+" cannot be flipped: Already destroyed");
}
this.length = this.offset;
this.offset = 0;
return this;
};
/**
* Resets the ByteBuffer. Sets offset=0 and length=0.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.reset = function() {
this.offset = 0;
this.length = 0;
return this;
};
/**
* Clones this ByteBuffer. The returned cloned ByteBuffer shares the same ArrayBuffer but will have its own offsets.
* @return {dcodeIO.ByteBuffer}
*/
dcodeIO.ByteBuffer.prototype.clone = function() {
// When cloning, an undocumented third parameter is used to set array and view manually.
var b = new dcodeIO.ByteBuffer(-1, this.littleEndian, /* shadow copy */ true);
b.array = this.array;
b.view = this.view;
b.offset = this.offset;
b.length = this.length;
return b;
};
/**
* Copies this ByteBuffer. The returned copied ByteBuffer has its own ArrayBuffer and uses the same offsets as this one.
* @return {dcodeIO.ByteBuffer}
*/
dcodeIO.ByteBuffer.prototype.copy = function() {
var b = new dcodeIO.ByteBuffer(this.array.byteLength, this.littleEndian);
var src = new Uint8Array(this.array);
var dst = new Uint8Array(b.array);
dst.set(src);
b.offset = this.offset;
b.length = this.length;
return b;
};
/**
* Gets the number of remaining readable bytes. Contents are the bytes between offset and length, so this returns
* length-offset.
* @returns {number} Remaining readable bytes (may be negative if offset is larger than length)
*/
dcodeIO.ByteBuffer.prototype.remaining = function() {
return this.length - this.offset;
};
/**
* Gets the capacity of the backing buffer. May be larger but not less than the contents actual length. Contents are the
* bytes between offset and length, which is independent of the actual capacity.
* @returns {number} Capacity of the backing buffer or 0 if destroyed
*/
dcodeIO.ByteBuffer.prototype.capacity = function() {
return this.array != null ? this.array.byteLength : 0;
};
/**
* Compacts the ByteBuffer to be backed by an ArrayBuffer of its actual length. Will {@link dcodeIO.ByteBuffer#flip} the
* ByteBuffer if its offset is larger than its length. If the ByteBuffer's offset is less than its length, only the
* portion between its offset and length will be contained in the compacted backing buffer. Will set offset=0 and
* length=capacity. Will do nothing but flipping, if required, if already compacted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.compact = function() {
if (this.array == null) {
throw(this+" cannot be compacted: Already destroyed");
}
if (this.offset > this.length) {
this.flip();
}
if (this.offset == this.length) {
throw(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")");
}
if (this.offset == 0 && this.length == this.array.byteLength) {
return this; // Already compacted
}
var srcView = new Uint8Array(this.array);
var dst = new ArrayBuffer(this.length-this.offset);
var dstView = new Uint8Array(dst);
dstView.set(srcView.subarray(this.offset, this.length));
this.array = dst;
this.offset = 0;
this.length = this.array.byteLength;
return this;
};
/**
* Destroys the ByteBuffer, releasing all references to the backing array.
*/
dcodeIO.ByteBuffer.prototype.destroy = function() {
if (this.array == null) return; // Already destroyed
this.array = null;
this.view = null;
this.offset = 0;
this.length = 0;
};
/**
* Writes an 8bit singed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeInt8 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setInt8(offset, value, this.littleEndian);
return this;
};
/**
* Reads an 8bit singed integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readInt8 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
return this.view.getInt8(offset, this.littleEndian);
};
/**
* Writes a byte. This is an alias of {dcodeIO.ByteBuffer#writeInt8}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeByte = dcodeIO.ByteBuffer.prototype.writeInt8;
/**
* Reads a byte. This is an alias of {@link dcodeIO.ByteBuffer#readInt8}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readByte = dcodeIO.ByteBuffer.prototype.readInt8;
/**
* Writes an 8bit unsinged integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeUint8 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setUint8(offset, value, this.littleEndian);
return this;
};
/**
* Reads an 8bit unsinged integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readUint8 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
return this.view.getUint8(offset, this.littleEndian);
};
/**
* Writes a 16bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeInt16 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setInt16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit signed integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readInt16 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
return this.view.getInt16(offset, this.littleEndian);
};
/**
* Writes a short value. This is an alias of {@link dcodeIO.ByteBuffer#writeInt16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeShort = dcodeIO.ByteBuffer.prototype.writeInt16;
/**
* Reads a short value. This is an alias of {@link dcodeIO.ByteBuffer#readInt16}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readShort = dcodeIO.ByteBuffer.prototype.readInt16;
/**
* Writes a 16bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeUint16 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setUint16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit unsigned integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readUint16 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
return this.view.getUint16(offset, this.littleEndian);
};
/**
* Writes a 32bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeInt32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setInt32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit signed integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readInt32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getInt32(offset, this.littleEndian);
};
/**
* Writes an integer. This is an alias of {@link dcodeIO.ByteBuffer#writeInt32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeInt = dcodeIO.ByteBuffer.prototype.writeInt32;
/**
* Reads an integer. This is an alias of {@link dcodeIO.ByteBuffer#readInt32}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readInt = dcodeIO.ByteBuffer.prototype.readInt32;
/**
* Writes a 32bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeUint32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setUint32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit unsigned integer.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readUint32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getUint32(offset, this.littleEndian);
};
/**
* Writes a 32bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeFloat32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setFloat32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit float.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readFloat32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getFloat32(offset, this.littleEndian);
};
/**
* Writes a float. This is an alias of {@link dcodeIO.ByteBuffer#writeFloat32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeFloat = dcodeIO.ByteBuffer.prototype.writeFloat32;
/**
* Reads a float. This is an alias of {@link dcodeIO.ByteBuffer#readFloat32}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readFloat = dcodeIO.ByteBuffer.prototype.readFloat32;
/**
* Writes a 64bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeFloat64 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
this.ensureCapacity(offset+8);
this.view.setFloat64(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 64bit float.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readFloat64 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
return this.view.getFloat64(offset, this.littleEndian);
};
/**
* Writes a double. This is an alias of {@link dcodeIO.ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeDouble = dcodeIO.ByteBuffer.prototype.writeFloat64;
/**
* Reads a double. This is an alias of {@link ByteBuffer#readFloat64}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readDouble = dcodeIO.ByteBuffer.prototype.readFloat64;
/**
* Writes a long. This is an alias of {@link dcodeIO.ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer} this
*/
dcodeIO.ByteBuffer.prototype.writeLong = dcodeIO.ByteBuffer.prototype.writeFloat64;
/**
* Reads a long. This makes use of {@link dcodeIO.ByteBuffer#readFloat64} by additionally clamping the returned value to a natural number.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
dcodeIO.ByteBuffer.prototype.readLong = function(offset) {
// Assuming it's +- a fraction -> round, not parseInt or something
return Math.round(this.readFloat64(offset));
};
/**
* Writes an UTF8 string.
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
dcodeIO.ByteBuffer.prototype.writeUTF8String = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var start = offset;
var encLen = 0, i;
for (i=0;i< str.length; i++) {
encLen += dcodeIO.ByteBuffer.calculateUTF8Char(str.charCodeAt(i));
}
this.ensureCapacity(offset+encLen);
for (i=0; i<str.length; i++) {
offset += dcodeIO.ByteBuffer.encodeUTF8Char(str.charCodeAt(i), this, offset);
}
if (advance) {
this.offset = offset;
this.offset = 0;
this.length = this.array.byteLength;
return this;
} else {
return offset-start;
}
};
/**
* Reads an UTF8 string.
* @param {number} chars Number of characters to read
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
dcodeIO.ByteBuffer.prototype.readUTF8String = function(chars, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var dec, result = "", start = offset;
for (var i=0; i<chars; i++) {
dec = dcodeIO.ByteBuffer.decodeUTF8Char(this, offset);
offset += dec["length"];
result += String.fromCharCode(dec["char"]);
}
if (advance) {
this.offset = offset;
return result;
} else {
return {
"string": result,
"length": offset-start
}
}
};
/**
* Writes a string with prepended number of characters, which is also encoded as an UTF8 character..
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
dcodeIO.ByteBuffer.prototype.writeLString = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var encLen = dcodeIO.ByteBuffer.encodeUTF8Char(str.length, this, offset);
encLen += this.writeUTF8String(str, offset+encLen);
if (advance) {
this.offset += encLen;
};
/**
* Destroys the ByteBuffer, releasing all references to the backing array.
*/
ByteBuffer.prototype.destroy = function() {
if (this.array == null) return; // Already destroyed
this.array = null;
this.view = null;
this.offset = 0;
this.length = 0;
};
/**
* Writes an 8bit singed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeInt8 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setInt8(offset, value, this.littleEndian);
return this;
} else {
return encLen;
}
};
/**
* Reads a string with a prepended number of characters, which is also encoded as an UTF8 character.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
dcodeIO.ByteBuffer.prototype.readLString = function(offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var lenDec = dcodeIO.ByteBuffer.decodeUTF8Char(this, offset);
var dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
if (advance) {
this.offset += lenDec["length"]+dec["length"];
return dec["string"];
} else {
return {
"string": dec["string"],
"length": lenDec["length"]+dec["length"]
};
}
};
/**
* Writes a string followed by a NULL character (Uint8).
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {dcodeIO.ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
dcodeIO.ByteBuffer.prototype.writeCString = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var encLen = this.writeUTF8String(str, offset);
this.writeUint8(0, offset+encLen);
if (advance) {
this.offset += encLen+1;
};
/**
* Reads an 8bit singed integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readInt8 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
return this.view.getInt8(offset, this.littleEndian);
};
/**
* Writes a byte. This is an alias of {ByteBuffer#writeInt8}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeByte = ByteBuffer.prototype.writeInt8;
/**
* Reads a byte. This is an alias of {@link ByteBuffer#readInt8}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readByte = ByteBuffer.prototype.readInt8;
/**
* Writes an 8bit unsinged integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeUint8 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setUint8(offset, value, this.littleEndian);
return this;
} else {
return encLen+1;
}
};
/**
* Reads a string followed by a NULL character (Uint8).
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
dcodeIO.ByteBuffer.prototype.readCString = function(offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var dec, result = "", start = offset;
do {
dec = dcodeIO.ByteBuffer.decodeUTF8Char(this, offset);
offset += dec["length"];
if (dec["char"] != 0) result += String.fromCharCode(dec["char"]);
} while (dec["char"] != 0);
if (advance) {
this.offset = offset;
return result;
} else {
};
/**
* Reads an 8bit unsinged integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readUint8 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
return this.view.getUint8(offset, this.littleEndian);
};
/**
* Writes a 16bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeInt16 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setInt16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit signed integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readInt16 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
return this.view.getInt16(offset, this.littleEndian);
};
/**
* Writes a short value. This is an alias of {@link ByteBuffer#writeInt16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeShort = ByteBuffer.prototype.writeInt16;
/**
* Reads a short value. This is an alias of {@link ByteBuffer#readInt16}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readShort = ByteBuffer.prototype.readInt16;
/**
* Writes a 16bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeUint16 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setUint16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit unsigned integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readUint16 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=2)-2;
return this.view.getUint16(offset, this.littleEndian);
};
/**
* Writes a 32bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeInt32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setInt32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit signed integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readInt32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getInt32(offset, this.littleEndian);
};
/**
* Writes an integer. This is an alias of {@link ByteBuffer#writeInt32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeInt = ByteBuffer.prototype.writeInt32;
/**
* Reads an integer. This is an alias of {@link ByteBuffer#readInt32}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readInt = ByteBuffer.prototype.readInt32;
/**
* Writes a 32bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeUint32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setUint32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit unsigned integer.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readUint32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getUint32(offset, this.littleEndian);
};
/**
* Writes a 32bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setFloat32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit float.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readFloat32 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
return this.view.getFloat32(offset, this.littleEndian);
};
/**
* Writes a float. This is an alias of {@link ByteBuffer#writeFloat32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat = ByteBuffer.prototype.writeFloat32;
/**
* Reads a float. This is an alias of {@link ByteBuffer#readFloat32}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readFloat = ByteBuffer.prototype.readFloat32;
/**
* Writes a 64bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat64 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
this.ensureCapacity(offset+8);
this.view.setFloat64(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 64bit float.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readFloat64 = function(offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=8)-8;
return this.view.getFloat64(offset, this.littleEndian);
};
/**
* Writes a double. This is an alias of {@link ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeDouble = ByteBuffer.prototype.writeFloat64;
/**
* Reads a double. This is an alias of {@link ByteBuffer#readFloat64}.
* @function
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readDouble = ByteBuffer.prototype.readFloat64;
/**
* Writes a long. This is an alias of {@link ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer} this
*/
ByteBuffer.prototype.writeLong = ByteBuffer.prototype.writeFloat64;
/**
* Reads a long. This makes use of {@link ByteBuffer#readFloat64} by additionally clamping the returned value to a natural number.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {number}
*/
ByteBuffer.prototype.readLong = function(offset) {
// Assuming it's +- a fraction -> round, not parseInt or something
return Math.round(this.readFloat64(offset));
};
/**
* Writes an UTF8 string.
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
ByteBuffer.prototype.writeUTF8String = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var start = offset;
var encLen = 0, i;
for (i=0;i< str.length; i++) {
encLen += ByteBuffer.calculateUTF8Char(str.charCodeAt(i));
}
this.ensureCapacity(offset+encLen);
for (i=0; i<str.length; i++) {
offset += ByteBuffer.encodeUTF8Char(str.charCodeAt(i), this, offset);
}
if (advance) {
this.offset = offset;
return this;
} else {
return offset-start;
}
};
/**
* Reads an UTF8 string.
* @param {number} chars Number of characters to read
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
ByteBuffer.prototype.readUTF8String = function(chars, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var dec, result = "", start = offset;
for (var i=0; i<chars; i++) {
dec = ByteBuffer.decodeUTF8Char(this, offset);
offset += dec["length"];
result += String.fromCharCode(dec["char"]);
}
if (advance) {
this.offset = offset;
return result;
} else {
return {
"string": result,
"length": offset-start
}
}
};
/**
* Writes a string with prepended number of characters, which is also encoded as an UTF8 character..
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
ByteBuffer.prototype.writeLString = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var encLen = ByteBuffer.encodeUTF8Char(str.length, this, offset);
encLen += this.writeUTF8String(str, offset+encLen);
if (advance) {
this.offset += encLen;
return this;
} else {
return encLen;
}
};
/**
* Reads a string with a prepended number of characters, which is also encoded as an UTF8 character.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
ByteBuffer.prototype.readLString = function(offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var lenDec = ByteBuffer.decodeUTF8Char(this, offset);
var dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
if (advance) {
this.offset += lenDec["length"]+dec["length"];
return dec["string"];
} else {
return {
"string": dec["string"],
"length": lenDec["length"]+dec["length"]
};
}
};
/**
* Writes a string followed by a NULL character (Uint8).
* @param {string} str String to write
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
ByteBuffer.prototype.writeCString = function(str, offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var encLen = this.writeUTF8String(str, offset);
this.writeUint8(0, offset+encLen);
if (advance) {
this.offset += encLen+1;
return this;
} else {
return encLen+1;
}
};
/**
* Reads a string followed by a NULL character (Uint8).
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @return {string|{string: string, length: number}} The string read if offset is omitted, else the string read and the actual number of bytes read.
*/
ByteBuffer.prototype.readCString = function(offset) {
var advance = typeof offset == 'undefined';
offset = typeof offset != 'undefined' ? offset : this.offset;
var dec, result = "", start = offset;
do {
dec = ByteBuffer.decodeUTF8Char(this, offset);
offset += dec["length"];
if (dec["char"] != 0) result += String.fromCharCode(dec["char"]);
} while (dec["char"] != 0);
if (advance) {
this.offset = offset;
return result;
} else {
return {
"string": result,
"length": offset-start
};
}
};
/**
* Serializes and writes a JSON payload.
* @param {*} data Data payload to serialize
* @param {number=} offset Offset to write to. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted,
* @param {function=} stringify Stringify implementation to use. Defaults to {@link JSON.stringify}.
* @return {ByteBuffer|number} this if offset is omitted, else the actual number if bytes written,
*/
ByteBuffer.prototype.writeJSON = function(data, offset, stringify) {
stringify = stringify || JSON.stringify.bind(JSON);
return this.writeLString(stringify(data), offset);
};
/**
* Reads a JSON payload and unserializes it.
* @param {number=} offset Offset to read from. Defaults to {@link ByteBuffer#offset} which will be modified only if omitted.
* @param {function=} parse Parse implementation to use. Defaults to {@link JSON.parse}.
* @return {*|{data: *, length: number}} Data payload if offset is omitted, else the data payload and the actual number of bytes read.
*/
ByteBuffer.prototype.readJSON = function(offset, parse) {
parse = parse || JSON.parse.bind(JSON);
var result = this.readLString(offset);
if (typeof result == "string") {
return parse(result);
} else {
return {
"data": parse(result["string"]),
"length": result["length"]
};
}
};
/**
* Prints debug information about this ByteBuffer's contents to console.
*/
ByteBuffer.prototype.printDebug = function() {
if (typeof console != "undefined" && console["log"]) {
console.log(
this.toString()+"\n"+
"--------------------------------------------------\n"+
this.toHex()+"\n"
);
}
};
/**
* Returns a hex representation of this ByteBuffer's contents. Beware: May be large.
* @param {number=} wrap Wrap length. Defaults to 16.
* @return {string} Hex representation as of " 00<01 02>03..." with marked offsets
*/
ByteBuffer.prototype.toHex = function(wrap) {
if (this.array == null) return "DESTROYED";
wrap = typeof wrap != 'undefined' ? parseInt(wrap, 10) : 16;
if (wrap < 1) wrap = 16;
var out = "", view = new Uint8Array(this.array);
for (var i=0; i<this.array.byteLength; i++) {
var val = view[i];
val = val.toString(16).toUpperCase();
if (val.length < 2) val = "0"+val;
if (i>0 && i%wrap == 0) {
out += "\n";
}
if (i == this.offset && i == this.length) {
out += "|";
} else if (i == this.offset) {
out += "<";
} else if (i == this.length) {
out += ">";
} else {
out += " ";
}
out += val;
}
if (this.offset == this.array.byteLength && this.length == this.array.byteLength) {
out += "|";
} else if (this.length == this.array.byteLength) {
out += ">";
} else if (this.offset == this.array.byteLength) {
out += "<";
}
return out;
};
/**
* Returns a string representation of this object.
* @return {string} String representation as of "ByteBuffer(offset,length,capacity)"
*/
ByteBuffer.prototype.toString = function() {
if (this.array == null) {
return "ByteBuffer(DESTROYED)";
}
return "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")";
};
/**
* Returns an ArrayBuffer compacted to contain this ByteBuffer's actual contents. Will implicitly
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will return a reference to
* the unmodified backing buffer if offset=0 and length=capacity unless forceCopy is set to true.
* @param {boolean=} forceCopy Forces the creation of a copy if set to true. Defaults to false.
* @return {ArrayBuffer} Compacted ArrayBuffer
*/
ByteBuffer.prototype.toArrayBuffer = function(forceCopy) {
var b = this.clone();
if (b.offset > b.length) {
b.flip();
}
var copied = false;
if (b.offset > 0 || b.length < b.array.byteLength) {
b.compact(); // Will always create a new backing buffer because of the above condition
copied = true;
}
return forceCopy && !copied ? b.copy().array : b.array;
};
/**
* Decodes a single UTF8 character from the specified ByteBuffer. The ByteBuffer's offsets are not modified.
* @param {ByteBuffer} src
* @param {number} offset Offset to read from
* @return {{char: number, length: number}} Decoded char code and the actual number of bytes read
*/
ByteBuffer.decodeUTF8Char = function(src, offset) {
var a = src.readUint8(offset), b, c, d, e, f, start = offset, charCode;
// ref: http://en.wikipedia.org/wiki/UTF-8#Description
// It's quite huge but should be pretty fast.
if ((a&0x80)==0) {
charCode = a;
offset += 1;
} else if ((a&0xE0)==0xC0) {
b = src.readUint8(offset+1);
charCode = ((a&0x1F)<<6) | (b&0x3F);
offset += 2;
} else if ((a&0xF0)==0xE0) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
charCode = ((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F);
offset += 3;
} else if ((a&0xF8)==0xF0) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
charCode = ((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F);
offset += 4;
} else if ((a&0xFC)==0xF8) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
e = src.readUint8(offset+4);
charCode = ((a&0x03)<<24) | ((b&0x3F)<<18) | ((c&0x3F)<<12) | ((d&0x3F)<<6) | (e&0x3F);
offset += 5;
} else if ((a&0xFE)==0xFC) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
e = src.readUint8(offset+4);
f = src.readUint8(offset+5);
charCode = ((a&0x01)<<30) | ((b&0x3F)<<24) | ((c&0x3F)<<18) | ((d&0x3F)<<12) | ((e&0x3F)<<6) | (f&0x3F);
offset += 6;
} else {
throw("Invalid byte at offset "+offset+": 0x"+a.toString(16));
}
return {
"string": result,
"char": charCode ,
"length": offset-start
};
}
};
/**
* Serializes and writes a JSON payload.
* @param {*} data Data payload to serialize
* @param {number=} offset Offset to write to. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted,
* @param {function=} stringify Stringify implementation to use. Defaults to {@link JSON.stringify}.
* @return {dcodeIO.ByteBuffer|number} this if offset is omitted, else the actual number if bytes written,
*/
dcodeIO.ByteBuffer.prototype.writeJSON = function(data, offset, stringify) {
stringify = stringify || JSON.stringify.bind(JSON);
return this.writeLString(stringify(data), offset);
};
/**
* Reads a JSON payload and unserializes it.
* @param {number=} offset Offset to read from. Defaults to {@link dcodeIO.ByteBuffer#offset} which will be modified only if omitted.
* @param {function=} parse Parse implementation to use. Defaults to {@link JSON.parse}.
* @return {*|{data: *, length: number}} Data payload if offset is omitted, else the data payload and the actual number of bytes read.
*/
dcodeIO.ByteBuffer.prototype.readJSON = function(offset, parse) {
parse = parse || JSON.parse.bind(JSON);
var result = this.readLString(offset);
if (typeof result == "string") {
return parse(result);
} else {
return {
"data": parse(result["string"]),
"length": result["length"]
};
}
};
/**
* Prints debug information about this ByteBuffer's contents to console.
*/
dcodeIO.ByteBuffer.prototype.printDebug = function() {
if (typeof console != "undefined" && console["log"]) {
console.log(
this.toString()+"\n"+
"--------------------------------------------------\n"+
this.toHex()+"\n"
);
}
};
/**
* Returns a hex representation of this ByteBuffer's contents. Beware: May be large.
* @param {number=} wrap Wrap length. Defaults to 16.
* @return {string} Hex representation as of " 00<01 02>03..." with marked offsets
*/
dcodeIO.ByteBuffer.prototype.toHex = function(wrap) {
if (this.array == null) return "DESTROYED";
wrap = typeof wrap != 'undefined' ? parseInt(wrap, 10) : 16;
if (wrap < 1) wrap = 16;
var out = "", view = new Uint8Array(this.array);
for (var i=0; i<this.array.byteLength; i++) {
var val = view[i];
val = val.toString(16).toUpperCase();
if (val.length < 2) val = "0"+val;
if (i>0 && i%wrap == 0) {
out += "\n";
};
/**
* Encodes a single UTF8 character to the specified ByteBuffer. The ByteBuffer's offsets are not modified.
* @param {number} charCode Character to encode as char code
* @param {ByteBuffer} dst ByteBuffer to encode to
* @param {number} offset Offset to write to
* @return {number} Actual number of bytes written
*/
ByteBuffer.encodeUTF8Char = function(charCode, dst, offset) {
var start = offset;
// ref: http://en.wikipedia.org/wiki/UTF-8#Description
// It's quite huge but should be pretty fast.
if (charCode < 0) {
throw("Cannot encode character with negative charCode ("+charCode+")");
}
if (i == this.offset && i == this.length) {
out += "|";
} else if (i == this.offset) {
out += "<";
} else if (i == this.length) {
out += ">";
if (charCode < 0x80) {
dst.writeUint8(charCode&0x7F, offset);
offset += 1;
} else if (charCode < 0x800) {
dst.writeUint8(((charCode>>6)&0x1F)|0xC0, offset)
.writeUint8((charCode&0x3F)|0x80, offset+1);
offset += 2;
} else if (charCode < 0x10000) {
dst.writeUint8(((charCode>>12)&0x0F)|0xE0, offset)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+1)
.writeUint8((charCode&0x3F)|0x80, offset+2);
offset += 3;
} else if (charCode < 0x200000) {
dst.writeUint8(((charCode>>18)&0x07)|0xF0, offset)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+2)
.writeUint8((charCode&0x3F)|0x80, offset+3);
offset += 4;
} else if (charCode < 0x4000000) {
dst.writeUint8(((charCode>>24)&0x03)|0xF8, offset)
.writeUint8(((charCode>>18)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+2)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+3)
.writeUint8((charCode&0x3F)|0x80, offset+4);
offset += 5;
} else {
out += " ";
dst.writeUint8(((charCode>>30)&0x01)|0xFC, offset)
.writeUint8(((charCode>>24)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>18)&0x3F)|0x80, offset+2)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+3)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+4)
.writeUint8((charCode&0x3F)|0x80, offset+5);
offset += 6;
}
out += val;
}
if (this.offset == this.array.byteLength && this.length == this.array.byteLength) {
out += "|";
} else if (this.length == this.array.byteLength) {
out += ">";
} else if (this.offset == this.array.byteLength) {
out += "<";
}
return out;
};
/**
* Returns a string representation of this object.
* @return {string} String representation as of "ByteBuffer(offset,length,capacity)"
*/
dcodeIO.ByteBuffer.prototype.toString = function() {
if (this.array == null) {
return "ByteBuffer(DESTROYED)";
}
return "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")";
};
/**
* Returns an ArrayBuffer compacted to contain this ByteBuffer's actual contents. Will implicitly
* {@link dcodeIO.ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will return a reference to
* the unmodified backing buffer if offset=0 and length=capacity unless forceCopy is set to true.
* @param {boolean=} forceCopy Forces the creation of a copy if set to true. Defaults to false.
* @return {ArrayBuffer} Compacted ArrayBuffer
*/
dcodeIO.ByteBuffer.prototype.toArrayBuffer = function(forceCopy) {
var b = this.clone();
if (b.offset > b.length) {
b.flip();
}
var copied = false;
if (b.offset > 0 || b.length < b.array.byteLength) {
b.compact(); // Will always create a new backing buffer because of the above condition
copied = true;
}
return forceCopy && !copied ? b.copy().array : b.array;
};
/**
* Decodes a single UTF8 character from the specified ByteBuffer. The ByteBuffer's offsets are not modified.
* @param {dcodeIO.ByteBuffer} src
* @param {number} offset Offset to read from
* @return {{char: number, length: number}} Decoded char code and the actual number of bytes read
*/
dcodeIO.ByteBuffer.decodeUTF8Char = function(src, offset) {
var a = src.readUint8(offset), b, c, d, e, f, start = offset, charCode;
// ref: http://en.wikipedia.org/wiki/UTF-8#Description
// It's quite huge but should be pretty fast.
if ((a&0x80)==0) {
charCode = a;
offset += 1;
} else if ((a&0xE0)==0xC0) {
b = src.readUint8(offset+1);
charCode = ((a&0x1F)<<6) | (b&0x3F);
offset += 2;
} else if ((a&0xF0)==0xE0) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
charCode = ((a&0x0F)<<12) | ((b&0x3F)<<6) | (c&0x3F);
offset += 3;
} else if ((a&0xF8)==0xF0) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
charCode = ((a&0x07)<<18) | ((b&0x3F)<<12) | ((c&0x3F)<<6) | (d&0x3F);
offset += 4;
} else if ((a&0xFC)==0xF8) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
e = src.readUint8(offset+4);
charCode = ((a&0x03)<<24) | ((b&0x3F)<<18) | ((c&0x3F)<<12) | ((d&0x3F)<<6) | (e&0x3F);
offset += 5;
} else if ((a&0xFE)==0xFC) {
b = src.readUint8(offset+1);
c = src.readUint8(offset+2);
d = src.readUint8(offset+3);
e = src.readUint8(offset+4);
f = src.readUint8(offset+5);
charCode = ((a&0x01)<<30) | ((b&0x3F)<<24) | ((c&0x3F)<<18) | ((d&0x3F)<<12) | ((e&0x3F)<<6) | (f&0x3F);
offset += 6;
} else {
throw("Invalid byte at offset "+offset+": 0x"+a.toString(16));
}
return {
"char": charCode ,
"length": offset-start
return offset-start;
};
};
/**
* Calculates the actual number of bytes required to encode the specified char code.
* @param {number} charCode Character to encode as char code
* @return {number} Number of bytes required to encode the specified char code
*/
ByteBuffer.calculateUTF8Char = function(charCode) {
if (charCode < 0) {
throw("Cannot calculate length of character with negative charCode ("+charCode+")");
}
if (charCode < 0x80) {
return 1;
} else if (charCode < 0x800) {
return 2;
} else if (charCode < 0x10000) {
return 3;
} else if (charCode < 0x200000) {
return 4;
} else if (charCode < 0x4000000) {
return 5;
} else {
return 6;
}
};
/**
* Encodes a single UTF8 character to the specified ByteBuffer. The ByteBuffer's offsets are not modified.
* @param {number} charCode Character to encode as char code
* @param {dcodeIO.ByteBuffer} dst ByteBuffer to encode to
* @param {number} offset Offset to write to
* @return {number} Actual number of bytes written
*/
dcodeIO.ByteBuffer.encodeUTF8Char = function(charCode, dst, offset) {
var start = offset;
// ref: http://en.wikipedia.org/wiki/UTF-8#Description
// It's quite huge but should be pretty fast.
if (charCode < 0) {
throw("Cannot encode character with negative charCode ("+charCode+")");
// Enable module loading if available
if (typeof module != 'undefined' && module["exports"]) { // CommonJS
module["exports"] = ByteBuffer;
} else if (typeof define != 'undefined' && define["amd"]) { // AMD
define([], function() { return ByteBuffer; });
} else { // Shim
if (!window["dcodeIO"]) {
window["dcodeIO"] = {};
} else {
window["dcodeIO"]["ByteBuffer"] = ByteBuffer;
}
}
if (charCode < 0x80) {
dst.writeUint8(charCode&0x7F, offset);
offset += 1;
} else if (charCode < 0x800) {
dst.writeUint8(((charCode>>6)&0x1F)|0xC0, offset)
.writeUint8((charCode&0x3F)|0x80, offset+1);
offset += 2;
} else if (charCode < 0x10000) {
dst.writeUint8(((charCode>>12)&0x0F)|0xE0, offset)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+1)
.writeUint8((charCode&0x3F)|0x80, offset+2);
offset += 3;
} else if (charCode < 0x200000) {
dst.writeUint8(((charCode>>18)&0x07)|0xF0, offset)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+2)
.writeUint8((charCode&0x3F)|0x80, offset+3);
offset += 4;
} else if (charCode < 0x4000000) {
dst.writeUint8(((charCode>>24)&0x03)|0xF8, offset)
.writeUint8(((charCode>>18)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+2)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+3)
.writeUint8((charCode&0x3F)|0x80, offset+4);
offset += 5;
} else {
dst.writeUint8(((charCode>>30)&0x01)|0xFC, offset)
.writeUint8(((charCode>>24)&0x3F)|0x80, offset+1)
.writeUint8(((charCode>>18)&0x3F)|0x80, offset+2)
.writeUint8(((charCode>>12)&0x3F)|0x80, offset+3)
.writeUint8(((charCode>>6)&0x3F)|0x80, offset+4)
.writeUint8((charCode&0x3F)|0x80, offset+5);
offset += 6;
}
return offset-start;
};
/**
* Calculates the actual number of bytes required to encode the specified char code.
* @param {number} charCode Character to encode as char code
* @return {number} Number of bytes required to encode the specified char code
*/
dcodeIO.ByteBuffer.calculateUTF8Char = function(charCode) {
if (charCode < 0) {
throw("Cannot calculate length of character with negative charCode ("+charCode+")");
}
if (charCode < 0x80) {
return 1;
} else if (charCode < 0x800) {
return 2;
} else if (charCode < 0x10000) {
return 3;
} else if (charCode < 0x200000) {
return 4;
} else if (charCode < 0x4000000) {
return 5;
} else {
return 6;
}
};
// Enable module loading if available
if (typeof module != 'undefined' && module["exports"]) {
module["exports"] = dcodeIO.ByteBuffer;
} else if (typeof require != 'undefined' && typeof define != 'undefined') {
define([], function() { return dcodeIO.ByteBuffer; });
}
})(typeof window != 'undefined' ? window : null);
/**
* ByteBuffer.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
* @license ByteBuffer.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/ByteBuffer.js for details
* see: https://github.com/ByteBuffer.js for details
*/
var dcodeIO=typeof dcodeIO!="undefined"?dcodeIO:{};dcodeIO.ByteBuffer=function(e,t){e=typeof e!="undefined"?parseInt(e,10):dcodeIO.ByteBuffer.DEFAULT_CAPACITY;if(e<1)e=dcodeIO.ByteBuffer.DEFAULT_CAPACITY;this.array=arguments.length==3&&arguments[2]===true?null:new ArrayBuffer(e);this.view=this.array!=null?new DataView(this.array):null;this.offset=0;this.length=0;this.littleEndian=typeof t!="undefined"?!!t:false};dcodeIO.ByteBuffer.DEFAULT_CAPACITY=32;dcodeIO.ByteBuffer.LITTLE_ENDIAN=true;dcodeIO.ByteBuffer.BIG_ENDIAN=false;dcodeIO.ByteBuffer.allocate=function(e,t){return new dcodeIO.ByteBuffer(e,t)};dcodeIO.ByteBuffer.wrap=function(e,t){if(!!e["array"]){e=e["array"]}else if(!!e["buffer"]){e=e["buffer"]}if(!(e instanceof ArrayBuffer)){throw"Cannot wrap buffer of type "+typeof e}var n=new dcodeIO.ByteBuffer(0,t,true);n.array=e;n.view=new DataView(n.array);n.offset=0;n.length=e.byteLength;return n};dcodeIO.ByteBuffer.prototype.resize=function(e){if(this.array==null&&e>0){this.array=new ArrayBuffer(e);this.view=new DataView(this.array)}if(this.array.byteLength<e){var t=this.array;var n=new Uint8Array(t);var r=new ArrayBuffer(e);var i=new Uint8Array(r);i.set(n);this.array=r;this.view=new DataView(r);return true}return false};dcodeIO.ByteBuffer.prototype.slice=function(e,t){if(this.array==null){throw this+" cannot be sliced: Already destroyed"}if(t<=e){throw this+" cannot be sliced: End ("+t+") is less than begin ("+e+")"}if(e<0||e>this.array.byteLength||t<1||t>this.array.byteLength){throw this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+e+"-"+t+")"}var n=this.clone();n.offset=e;n.length=t;return n};dcodeIO.ByteBuffer.prototype.sliceAndCompact=function(e,t){return dcodeIO.ByteBuffer.wrap(this.slice(e,t).toArrayBuffer(true))};dcodeIO.ByteBuffer.prototype.ensureCapacity=function(e){if(this.array==null){return this.resize(e)}if(this.array.byteLength<e)return this.resize(this.array.byteLength*2>=e?this.array.byteLength*2:e);return false};dcodeIO.ByteBuffer.prototype.flip=function(){if(this.array==null){throw this+" cannot be flipped: Already destroyed"}this.length=this.offset;this.offset=0;return this};dcodeIO.ByteBuffer.prototype.reset=function(){this.offset=0;this.length=0;return this};dcodeIO.ByteBuffer.prototype.clone=function(){var e=new dcodeIO.ByteBuffer(-1,this.littleEndian,true);e.array=this.array;e.view=this.view;e.offset=this.offset;e.length=this.length;return e};dcodeIO.ByteBuffer.prototype.copy=function(){var e=new dcodeIO.ByteBuffer(this.array.byteLength,this.littleEndian);var t=new Uint8Array(this.array);var n=new Uint8Array(e.array);n.set(t);e.offset=this.offset;e.length=this.length;return e};dcodeIO.ByteBuffer.prototype.remaining=function(){return this.length-this.offset};dcodeIO.ByteBuffer.prototype.capacity=function(){return this.array!=null?this.array.byteLength:0};dcodeIO.ByteBuffer.prototype.compact=function(){if(this.array==null){throw this+" cannot be compacted: Already destroyed"}if(this.offset>this.length){this.flip()}if(this.offset==this.length){throw this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"}if(this.offset==0&&this.length==this.array.byteLength){return this}var e=new Uint8Array(this.array);var t=new ArrayBuffer(this.length-this.offset);var n=new Uint8Array(t);n.set(e.subarray(this.offset,this.length));this.array=t;this.offset=0;this.length=this.array.byteLength;return this};dcodeIO.ByteBuffer.prototype.destroy=function(){if(this.array==null)return;this.array=null;this.view=null;this.offset=0;this.length=0};dcodeIO.ByteBuffer.prototype.writeInt8=function(e,t){t=typeof t!="undefined"?t:(this.offset+=1)-1;this.ensureCapacity(t+1);this.view.setInt8(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readInt8=function(e){e=typeof e!="undefined"?e:(this.offset+=1)-1;return this.view.getInt8(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeByte=dcodeIO.ByteBuffer.prototype.writeInt8;dcodeIO.ByteBuffer.prototype.readByte=dcodeIO.ByteBuffer.prototype.readInt8;dcodeIO.ByteBuffer.prototype.writeUint8=function(e,t){t=typeof t!="undefined"?t:(this.offset+=1)-1;this.ensureCapacity(t+1);this.view.setUint8(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readUint8=function(e){e=typeof e!="undefined"?e:(this.offset+=1)-1;return this.view.getUint8(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeInt16=function(e,t){t=typeof t!="undefined"?t:(this.offset+=2)-2;this.ensureCapacity(t+2);this.view.setInt16(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readInt16=function(e){e=typeof e!="undefined"?e:(this.offset+=2)-2;return this.view.getInt16(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeShort=dcodeIO.ByteBuffer.prototype.writeInt16;dcodeIO.ByteBuffer.prototype.readShort=dcodeIO.ByteBuffer.prototype.readInt16;dcodeIO.ByteBuffer.prototype.writeUint16=function(e,t){t=typeof t!="undefined"?t:(this.offset+=2)-2;this.ensureCapacity(t+2);this.view.setUint16(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readUint16=function(e){e=typeof e!="undefined"?e:(this.offset+=2)-2;return this.view.getUint16(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeInt32=function(e,t){t=typeof t!="undefined"?t:(this.offset+=4)-4;this.ensureCapacity(t+4);this.view.setInt32(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readInt32=function(e){e=typeof e!="undefined"?e:(this.offset+=4)-4;return this.view.getInt32(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeInt=dcodeIO.ByteBuffer.prototype.writeInt32;dcodeIO.ByteBuffer.prototype.readInt=dcodeIO.ByteBuffer.prototype.readInt32;dcodeIO.ByteBuffer.prototype.writeUint32=function(e,t){t=typeof t!="undefined"?t:(this.offset+=4)-4;this.ensureCapacity(t+4);this.view.setUint32(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readUint32=function(e){e=typeof e!="undefined"?e:(this.offset+=4)-4;return this.view.getUint32(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeFloat32=function(e,t){t=typeof t!="undefined"?t:(this.offset+=4)-4;this.ensureCapacity(t+4);this.view.setFloat32(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readFloat32=function(e){e=typeof e!="undefined"?e:(this.offset+=4)-4;return this.view.getFloat32(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeFloat=dcodeIO.ByteBuffer.prototype.writeFloat32;dcodeIO.ByteBuffer.prototype.readFloat=dcodeIO.ByteBuffer.prototype.readFloat32;dcodeIO.ByteBuffer.prototype.writeFloat64=function(e,t){t=typeof t!="undefined"?t:(this.offset+=8)-8;this.ensureCapacity(t+8);this.view.setFloat64(t,e,this.littleEndian);return this};dcodeIO.ByteBuffer.prototype.readFloat64=function(e){e=typeof e!="undefined"?e:(this.offset+=8)-8;return this.view.getFloat64(e,this.littleEndian)};dcodeIO.ByteBuffer.prototype.writeDouble=dcodeIO.ByteBuffer.prototype.writeFloat64;dcodeIO.ByteBuffer.prototype.readDouble=dcodeIO.ByteBuffer.prototype.readFloat64;dcodeIO.ByteBuffer.prototype.writeLong=dcodeIO.ByteBuffer.prototype.writeFloat64;dcodeIO.ByteBuffer.prototype.readLong=function(e){return Math.round(this.readFloat64(e))};dcodeIO.ByteBuffer.prototype.writeUTF8String=function(e,t){var n=typeof t=="undefined";t=typeof t!="undefined"?t:this.offset;var r=t;var i=0,s;for(s=0;s<e.length;s++){i+=dcodeIO.ByteBuffer.calculateUTF8Char(e.charCodeAt(s))}this.ensureCapacity(t+i);for(s=0;s<e.length;s++){t+=dcodeIO.ByteBuffer.encodeUTF8Char(e.charCodeAt(s),this,t)}if(n){this.offset=t;return this}else{return t-r}};dcodeIO.ByteBuffer.prototype.readUTF8String=function(e,t){var n=typeof t=="undefined";t=typeof t!="undefined"?t:this.offset;var r,i="",s=t;for(var o=0;o<e;o++){r=dcodeIO.ByteBuffer.decodeUTF8Char(this,t);t+=r["length"];i+=String.fromCharCode(r["char"])}if(n){this.offset=t;return i}else{return{string:i,length:t-s}}};dcodeIO.ByteBuffer.prototype.writeLString=function(e,t){var n=typeof t=="undefined";t=typeof t!="undefined"?t:this.offset;var r=dcodeIO.ByteBuffer.encodeUTF8Char(e.length,this,t);r+=this.writeUTF8String(e,t+r);if(n){this.offset+=r;return this}else{return r}};dcodeIO.ByteBuffer.prototype.readLString=function(e){var t=typeof e=="undefined";e=typeof e!="undefined"?e:this.offset;var n=dcodeIO.ByteBuffer.decodeUTF8Char(this,e);var r=this.readUTF8String(n["char"],e+n["length"]);if(t){this.offset+=n["length"]+r["length"];return r["string"]}else{return{string:r["string"],length:n["length"]+r["length"]}}};dcodeIO.ByteBuffer.prototype.writeCString=function(e,t){var n=typeof t=="undefined";t=typeof t!="undefined"?t:this.offset;var r=this.writeUTF8String(e,t);this.writeUint8(0,t+r);if(n){this.offset+=r+1;return this}else{return r+1}};dcodeIO.ByteBuffer.prototype.readCString=function(e){var t=typeof e=="undefined";e=typeof e!="undefined"?e:this.offset;var n,r="",i=e;do{n=dcodeIO.ByteBuffer.decodeUTF8Char(this,e);e+=n["length"];if(n["char"]!=0)r+=String.fromCharCode(n["char"])}while(n["char"]!=0);if(t){this.offset=e;return r}else{return{string:r,length:e-i}}};dcodeIO.ByteBuffer.prototype.writeJSON=function(e,t,n){n=n||JSON.stringify.bind(JSON);return this.writeLString(n(e),t)};dcodeIO.ByteBuffer.prototype.readJSON=function(e,t){t=t||JSON.parse.bind(JSON);var n=this.readLString(e);if(typeof n=="string"){return t(n)}else{return{data:t(n["string"]),length:n["length"]}}};dcodeIO.ByteBuffer.LINE="--------------------------------------------------";dcodeIO.ByteBuffer.prototype.printDebug=function(){console.log(this.toString()+"\n"+dcodeIO.ByteBuffer.LINE);console.log(this.toHex()+"\n")};dcodeIO.ByteBuffer.prototype.toHex=function(e){if(this.array==null)return"DESTROYED";e=typeof e!="undefined"?parseInt(e,10):16;if(e<1)e=16;var t="",n=new Uint8Array(this.array);for(var r=0;r<this.array.byteLength;r++){var i=n[r];i=i.toString(16).toUpperCase();if(i.length<2)i="0"+i;if(r>0&&r%e==0){t+="\n"}if(r==this.offset&&r==this.length){t+="|"}else if(r==this.offset){t+="<"}else if(r==this.length){t+=">"}else{t+=" "}t+=i}if(this.offset==this.array.byteLength&&this.length==this.array.byteLength){t+="|"}else if(this.length==this.array.byteLength){t+=">"}else if(this.offset==this.array.byteLength){t+="<"}return t};dcodeIO.ByteBuffer.prototype.toString=function(){if(this.array==null){return"ByteBuffer(DESTROYED)"}return"ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")"};dcodeIO.ByteBuffer.prototype.toArrayBuffer=function(e){var t=this.clone();if(t.offset>t.length){t.flip()}var n=false;if(t.offset>0||t.length<t.array.byteLength){t.compact();n=true}return e&&!n?t.copy().array:t.array};dcodeIO.ByteBuffer.decodeUTF8Char=function(e,t){var n=e.readUint8(t),r,i,s,o,u,a=t,f;if((n&128)==0){f=n;t+=1}else if((n&224)==192){r=e.readUint8(t+1);f=(n&31)<<6|r&63;t+=2}else if((n&240)==224){r=e.readUint8(t+1);i=e.readUint8(t+2);f=(n&15)<<12|(r&63)<<6|i&63;t+=3}else if((n&248)==240){r=e.readUint8(t+1);i=e.readUint8(t+2);s=e.readUint8(t+3);f=(n&7)<<18|(r&63)<<12|(i&63)<<6|s&63;t+=4}else if((n&252)==248){r=e.readUint8(t+1);i=e.readUint8(t+2);s=e.readUint8(t+3);o=e.readUint8(t+4);f=(n&3)<<24|(r&63)<<18|(i&63)<<12|(s&63)<<6|o&63;t+=5}else if((n&254)==252){r=e.readUint8(t+1);i=e.readUint8(t+2);s=e.readUint8(t+3);o=e.readUint8(t+4);u=e.readUint8(t+5);f=(n&1)<<30|(r&63)<<24|(i&63)<<18|(s&63)<<12|(o&63)<<6|u&63;t+=6}else{throw"Invalid byte at offset "+t+": 0x"+n.toString(16)}return{"char":f,length:t-a}};dcodeIO.ByteBuffer.encodeUTF8Char=function(e,t,n){var r=n;if(e<0){throw"Cannot encode character with negative charCode ("+e+")"}if(e<128){t.writeUint8(e&127,n);n+=1}else if(e<2048){t.writeUint8(e>>6&31|192,n).writeUint8(e&63|128,n+1);n+=2}else if(e<65536){t.writeUint8(e>>12&15|224,n).writeUint8(e>>6&63|128,n+1).writeUint8(e&63|128,n+2);n+=3}else if(e<2097152){t.writeUint8(e>>18&7|240,n).writeUint8(e>>12&63|128,n+1).writeUint8(e>>6&63|128,n+2).writeUint8(e&63|128,n+3);n+=4}else if(e<67108864){t.writeUint8(e>>24&3|248,n).writeUint8(e>>18&63|128,n+1).writeUint8(e>>12&63|128,n+2).writeUint8(e>>6&63|128,n+3).writeUint8(e&63|128,n+4);n+=5}else{t.writeUint8(e>>30&1|252,n).writeUint8(e>>24&63|128,n+1).writeUint8(e>>18&63|128,n+2).writeUint8(e>>12&63|128,n+3).writeUint8(e>>6&63|128,n+4).writeUint8(e&63|128,n+5);n+=6}return n-r};dcodeIO.ByteBuffer.calculateUTF8Char=function(e){if(e<0){throw"Cannot calculate length of character with negative charCode ("+e+")"}if(e<128){return 1}else if(e<2048){return 2}else if(e<65536){return 3}else if(e<2097152){return 4}else if(e<67108864){return 5}else{return 6}};if(typeof module!="undefined"&&module["exports"]){module["exports"]=dcodeIO.ByteBuffer}else if(typeof require!="undefined"&&typeof define!="undefined"){define([],function(){return dcodeIO.ByteBuffer})}
(function(window){"use strict";var ByteBuffer=function(capacity,littleEndian){capacity=typeof capacity!="undefined"?parseInt(capacity,10):ByteBuffer.DEFAULT_CAPACITY;if(capacity<1)capacity=ByteBuffer.DEFAULT_CAPACITY;this.array=arguments.length==3&&arguments[2]===true?null:new ArrayBuffer(capacity);this.view=this.array!=null?new DataView(this.array):null;this.offset=0;this.length=0;this.littleEndian=typeof littleEndian!="undefined"?!!littleEndian:false};ByteBuffer.DEFAULT_CAPACITY=32;ByteBuffer.LITTLE_ENDIAN=true;ByteBuffer.BIG_ENDIAN=false;ByteBuffer.allocate=function(capacity,littleEndian){return new ByteBuffer(capacity,littleEndian)};ByteBuffer.wrap=function(buffer,littleEndian){if(!!buffer["array"]){buffer=buffer["array"]}else if(!!buffer["buffer"]){buffer=buffer["buffer"]}if(!(buffer instanceof ArrayBuffer)){throw"Cannot wrap buffer of type "+typeof buffer}var b=new ByteBuffer(0,littleEndian,true);b.array=buffer;b.view=new DataView(b.array);b.offset=0;b.length=buffer.byteLength;return b};ByteBuffer.prototype.resize=function(capacity){if(this.array==null&&capacity>0){this.array=new ArrayBuffer(capacity);this.view=new DataView(this.array)}if(this.array.byteLength<capacity){var src=this.array;var srcView=new Uint8Array(src);var dst=new ArrayBuffer(capacity);var dstView=new Uint8Array(dst);dstView.set(srcView);this.array=dst;this.view=new DataView(dst);return true}return false};ByteBuffer.prototype.slice=function(begin,end){if(this.array==null){throw this+" cannot be sliced: Already destroyed"}if(end<=begin){throw this+" cannot be sliced: End ("+end+") is less than begin ("+begin+")"}if(begin<0||begin>this.array.byteLength||end<1||end>this.array.byteLength){throw this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+begin+"-"+end+")"}var b=this.clone();b.offset=begin;b.length=end;return b};ByteBuffer.prototype.sliceAndCompact=function(begin,end){return ByteBuffer.wrap(this.slice(begin,end).toArrayBuffer(true))};ByteBuffer.prototype.ensureCapacity=function(capacity){if(this.array==null){return this.resize(capacity)}if(this.array.byteLength<capacity)return this.resize(this.array.byteLength*2>=capacity?this.array.byteLength*2:capacity);return false};ByteBuffer.prototype.flip=function(){if(this.array==null){throw this+" cannot be flipped: Already destroyed"}this.length=this.offset;this.offset=0;return this};ByteBuffer.prototype.reset=function(){this.offset=0;this.length=0;return this};ByteBuffer.prototype.clone=function(){var b=new ByteBuffer(-1,this.littleEndian,true);b.array=this.array;b.view=this.view;b.offset=this.offset;b.length=this.length;return b};ByteBuffer.prototype.copy=function(){var b=new ByteBuffer(this.array.byteLength,this.littleEndian);var src=new Uint8Array(this.array);var dst=new Uint8Array(b.array);dst.set(src);b.offset=this.offset;b.length=this.length;return b};ByteBuffer.prototype.remaining=function(){return this.length-this.offset};ByteBuffer.prototype.capacity=function(){return this.array!=null?this.array.byteLength:0};ByteBuffer.prototype.compact=function(){if(this.array==null){throw this+" cannot be compacted: Already destroyed"}if(this.offset>this.length){this.flip()}if(this.offset==this.length){throw this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"}if(this.offset==0&&this.length==this.array.byteLength){return this}var srcView=new Uint8Array(this.array);var dst=new ArrayBuffer(this.length-this.offset);var dstView=new Uint8Array(dst);dstView.set(srcView.subarray(this.offset,this.length));this.array=dst;this.offset=0;this.length=this.array.byteLength;return this};ByteBuffer.prototype.destroy=function(){if(this.array==null)return;this.array=null;this.view=null;this.offset=0;this.length=0};ByteBuffer.prototype.writeInt8=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=1)-1;this.ensureCapacity(offset+1);this.view.setInt8(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readInt8=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=1)-1;return this.view.getInt8(offset,this.littleEndian)};ByteBuffer.prototype.writeByte=ByteBuffer.prototype.writeInt8;ByteBuffer.prototype.readByte=ByteBuffer.prototype.readInt8;ByteBuffer.prototype.writeUint8=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=1)-1;this.ensureCapacity(offset+1);this.view.setUint8(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readUint8=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=1)-1;return this.view.getUint8(offset,this.littleEndian)};ByteBuffer.prototype.writeInt16=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=2)-2;this.ensureCapacity(offset+2);this.view.setInt16(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readInt16=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=2)-2;return this.view.getInt16(offset,this.littleEndian)};ByteBuffer.prototype.writeShort=ByteBuffer.prototype.writeInt16;ByteBuffer.prototype.readShort=ByteBuffer.prototype.readInt16;ByteBuffer.prototype.writeUint16=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=2)-2;this.ensureCapacity(offset+2);this.view.setUint16(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readUint16=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=2)-2;return this.view.getUint16(offset,this.littleEndian)};ByteBuffer.prototype.writeInt32=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;this.ensureCapacity(offset+4);this.view.setInt32(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readInt32=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;return this.view.getInt32(offset,this.littleEndian)};ByteBuffer.prototype.writeInt=ByteBuffer.prototype.writeInt32;ByteBuffer.prototype.readInt=ByteBuffer.prototype.readInt32;ByteBuffer.prototype.writeUint32=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;this.ensureCapacity(offset+4);this.view.setUint32(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readUint32=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;return this.view.getUint32(offset,this.littleEndian)};ByteBuffer.prototype.writeFloat32=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;this.ensureCapacity(offset+4);this.view.setFloat32(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readFloat32=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=4)-4;return this.view.getFloat32(offset,this.littleEndian)};ByteBuffer.prototype.writeFloat=ByteBuffer.prototype.writeFloat32;ByteBuffer.prototype.readFloat=ByteBuffer.prototype.readFloat32;ByteBuffer.prototype.writeFloat64=function(value,offset){offset=typeof offset!="undefined"?offset:(this.offset+=8)-8;this.ensureCapacity(offset+8);this.view.setFloat64(offset,value,this.littleEndian);return this};ByteBuffer.prototype.readFloat64=function(offset){offset=typeof offset!="undefined"?offset:(this.offset+=8)-8;return this.view.getFloat64(offset,this.littleEndian)};ByteBuffer.prototype.writeDouble=ByteBuffer.prototype.writeFloat64;ByteBuffer.prototype.readDouble=ByteBuffer.prototype.readFloat64;ByteBuffer.prototype.writeLong=ByteBuffer.prototype.writeFloat64;ByteBuffer.prototype.readLong=function(offset){return Math.round(this.readFloat64(offset))};ByteBuffer.prototype.writeUTF8String=function(str,offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var start=offset;var encLen=0,i;for(i=0;i<str.length;i++){encLen+=ByteBuffer.calculateUTF8Char(str.charCodeAt(i))}this.ensureCapacity(offset+encLen);for(i=0;i<str.length;i++){offset+=ByteBuffer.encodeUTF8Char(str.charCodeAt(i),this,offset)}if(advance){this.offset=offset;return this}else{return offset-start}};ByteBuffer.prototype.readUTF8String=function(chars,offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var dec,result="",start=offset;for(var i=0;i<chars;i++){dec=ByteBuffer.decodeUTF8Char(this,offset);offset+=dec["length"];result+=String.fromCharCode(dec["char"])}if(advance){this.offset=offset;return result}else{return{string:result,length:offset-start}}};ByteBuffer.prototype.writeLString=function(str,offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var encLen=ByteBuffer.encodeUTF8Char(str.length,this,offset);encLen+=this.writeUTF8String(str,offset+encLen);if(advance){this.offset+=encLen;return this}else{return encLen}};ByteBuffer.prototype.readLString=function(offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var lenDec=ByteBuffer.decodeUTF8Char(this,offset);var dec=this.readUTF8String(lenDec["char"],offset+lenDec["length"]);if(advance){this.offset+=lenDec["length"]+dec["length"];return dec["string"]}else{return{string:dec["string"],length:lenDec["length"]+dec["length"]}}};ByteBuffer.prototype.writeCString=function(str,offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var encLen=this.writeUTF8String(str,offset);this.writeUint8(0,offset+encLen);if(advance){this.offset+=encLen+1;return this}else{return encLen+1}};ByteBuffer.prototype.readCString=function(offset){var advance=typeof offset=="undefined";offset=typeof offset!="undefined"?offset:this.offset;var dec,result="",start=offset;do{dec=ByteBuffer.decodeUTF8Char(this,offset);offset+=dec["length"];if(dec["char"]!=0)result+=String.fromCharCode(dec["char"])}while(dec["char"]!=0);if(advance){this.offset=offset;return result}else{return{string:result,length:offset-start}}};ByteBuffer.prototype.writeJSON=function(data,offset,stringify){stringify=stringify||JSON.stringify.bind(JSON);return this.writeLString(stringify(data),offset)};ByteBuffer.prototype.readJSON=function(offset,parse){parse=parse||JSON.parse.bind(JSON);var result=this.readLString(offset);if(typeof result=="string"){return parse(result)}else{return{data:parse(result["string"]),length:result["length"]}}};ByteBuffer.prototype.printDebug=function(){if(typeof console!="undefined"&&console["log"]){console.log(this.toString()+"\n"+"--------------------------------------------------\n"+this.toHex()+"\n")}};ByteBuffer.prototype.toHex=function(wrap){if(this.array==null)return"DESTROYED";wrap=typeof wrap!="undefined"?parseInt(wrap,10):16;if(wrap<1)wrap=16;var out="",view=new Uint8Array(this.array);for(var i=0;i<this.array.byteLength;i++){var val=view[i];val=val.toString(16).toUpperCase();if(val.length<2)val="0"+val;if(i>0&&i%wrap==0){out+="\n"}if(i==this.offset&&i==this.length){out+="|"}else if(i==this.offset){out+="<"}else if(i==this.length){out+=">"}else{out+=" "}out+=val}if(this.offset==this.array.byteLength&&this.length==this.array.byteLength){out+="|"}else if(this.length==this.array.byteLength){out+=">"}else if(this.offset==this.array.byteLength){out+="<"}return out};ByteBuffer.prototype.toString=function(){if(this.array==null){return"ByteBuffer(DESTROYED)"}return"ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")"};ByteBuffer.prototype.toArrayBuffer=function(forceCopy){var b=this.clone();if(b.offset>b.length){b.flip()}var copied=false;if(b.offset>0||b.length<b.array.byteLength){b.compact();copied=true}return forceCopy&&!copied?b.copy().array:b.array};ByteBuffer.decodeUTF8Char=function(src,offset){var a=src.readUint8(offset),b,c,d,e,f,start=offset,charCode;if((a&128)==0){charCode=a;offset+=1}else if((a&224)==192){b=src.readUint8(offset+1);charCode=(a&31)<<6|b&63;offset+=2}else if((a&240)==224){b=src.readUint8(offset+1);c=src.readUint8(offset+2);charCode=(a&15)<<12|(b&63)<<6|c&63;offset+=3}else if((a&248)==240){b=src.readUint8(offset+1);c=src.readUint8(offset+2);d=src.readUint8(offset+3);charCode=(a&7)<<18|(b&63)<<12|(c&63)<<6|d&63;offset+=4}else if((a&252)==248){b=src.readUint8(offset+1);c=src.readUint8(offset+2);d=src.readUint8(offset+3);e=src.readUint8(offset+4);charCode=(a&3)<<24|(b&63)<<18|(c&63)<<12|(d&63)<<6|e&63;offset+=5}else if((a&254)==252){b=src.readUint8(offset+1);c=src.readUint8(offset+2);d=src.readUint8(offset+3);e=src.readUint8(offset+4);f=src.readUint8(offset+5);charCode=(a&1)<<30|(b&63)<<24|(c&63)<<18|(d&63)<<12|(e&63)<<6|f&63;offset+=6}else{throw"Invalid byte at offset "+offset+": 0x"+a.toString(16)}return{"char":charCode,length:offset-start}};ByteBuffer.encodeUTF8Char=function(charCode,dst,offset){var start=offset;if(charCode<0){throw"Cannot encode character with negative charCode ("+charCode+")"}if(charCode<128){dst.writeUint8(charCode&127,offset);offset+=1}else if(charCode<2048){dst.writeUint8(charCode>>6&31|192,offset).writeUint8(charCode&63|128,offset+1);offset+=2}else if(charCode<65536){dst.writeUint8(charCode>>12&15|224,offset).writeUint8(charCode>>6&63|128,offset+1).writeUint8(charCode&63|128,offset+2);offset+=3}else if(charCode<2097152){dst.writeUint8(charCode>>18&7|240,offset).writeUint8(charCode>>12&63|128,offset+1).writeUint8(charCode>>6&63|128,offset+2).writeUint8(charCode&63|128,offset+3);offset+=4}else if(charCode<67108864){dst.writeUint8(charCode>>24&3|248,offset).writeUint8(charCode>>18&63|128,offset+1).writeUint8(charCode>>12&63|128,offset+2).writeUint8(charCode>>6&63|128,offset+3).writeUint8(charCode&63|128,offset+4);offset+=5}else{dst.writeUint8(charCode>>30&1|252,offset).writeUint8(charCode>>24&63|128,offset+1).writeUint8(charCode>>18&63|128,offset+2).writeUint8(charCode>>12&63|128,offset+3).writeUint8(charCode>>6&63|128,offset+4).writeUint8(charCode&63|128,offset+5);offset+=6}return offset-start};ByteBuffer.calculateUTF8Char=function(charCode){if(charCode<0){throw"Cannot calculate length of character with negative charCode ("+charCode+")"}if(charCode<128){return 1}else if(charCode<2048){return 2}else if(charCode<65536){return 3}else if(charCode<2097152){return 4}else if(charCode<67108864){return 5}else{return 6}};if(typeof module!="undefined"&&module["exports"]){module["exports"]=ByteBuffer}else if(typeof define!="undefined"&&define["amd"]){define([],function(){return ByteBuffer})}else{if(!window["dcodeIO"]){window["dcodeIO"]={}}else{window["dcodeIO"]["ByteBuffer"]=ByteBuffer}}})(typeof window!="undefined"?window:null);

@@ -10,5 +10,5 @@ {

"template": "templates/esoccer",
"destination": "./docs"
"destination": "docs"
},
"plugins": ["plugins/markdown"]
}
{
"name": "bytebuffer",
"version": "0.9.3",
"version": "0.9.4",
"author": "Daniel Wirtz <dcode@dcode.io>",

@@ -14,8 +14,20 @@ "description": "A Java-like ByteBuffer implementation using typed arrays.",

},
"keywords": ["net", "buffer"],
"dependencies": {},
"keywords": ["net", "array", "buffer", "arraybuffer", "typed array", "bytebuffer", "json", "websocket", "webrtc"],
"dependencies": {
},
"devDependencies": {
"nodeunit": ">=0.7",
"uglify-js": ">=2"
},
"license": "Apache License, Version 2.0",
"engines": {
"node": ">=0.6"
},
"scripts": {
"prepublish": "npm test",
"minify": "uglifyjs ByteBuffer.js -o ByteBuffer.min.js --comments",
"jsdoc": "jsdoc.cmd -c jsdoc.json",
"pretest": "npm run-script minify",
"test": "nodeunit tests/suite.js"
}
}
}
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](http://www.esoccer.me),
Provides a Java-like ByteBuffer implementation using typed arrays. It also tries to abstract a bit of 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](http://www.esoccer.me),
a HTML5 game developed at [University of Applied Sciences Bonn](http://www.h-brs.de).
ByteBuffer
ByteBuffer [![Build Status](https://travis-ci.org/dcodeIO/ByteBuffer.js.png?branch=master)](https://travis-ci.org/dcodeIO/ByteBuffer.js)
----------

@@ -98,6 +98,10 @@ * 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

Downloads
---------
* [ZIP-Archive](https://github.com/dcodeIO/ByteBuffer.js/archive/master.zip)
* [Tarball](https://github.com/dcodeIO/ByteBuffer.js/tarball/master)
Documentation
-------------
* [View documentation](http://htmlpreview.github.com/?http://github.com/dcodeIO/ByteBuffer.js/master/docs/dcodeIO.ByteBuffer.html)
* Create: `jsdoc -c jsdoc.json README.md` (you'll need to comment out the eSoccer template in jsdoc.json)
* [View documentation](http://htmlpreview.github.com/?http://github.com/dcodeIO/ByteBuffer.js/master/docs/ByteBuffer.html)

@@ -107,4 +111,2 @@ Tests (& Examples)

* [View source](https://github.com/dcodeIO/ByteBuffer.js/blob/master/tests/suite.js)
* Install: `npm install -g nodeunit`
* Run: `nodeunit tests/suite.js`

@@ -111,0 +113,0 @@ Prerequisites to run it against IE<10, FF<15, Chrome<9 etc.

@@ -1,2 +0,2 @@

var ByteBuffer = require("../ByteBuffer.js");
var ByteBuffer = require("../ByteBuffer.min.js");

@@ -3,0 +3,0 @@ var suite = {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc