bytebuffer
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -6,39 +6,45 @@ /* | ||
*/ | ||
function g(q){throw q;}var k=!0,l=null,n=!1; | ||
(function(q){function r(p){function c(a,b){a="undefined"!=typeof a?parseInt(a,10):c.DEFAULT_CAPACITY;1>a&&(a=c.DEFAULT_CAPACITY);this.array=3==arguments.length&&arguments[2]===k?l:new ArrayBuffer(a);this.view=this.array!=l?new DataView(this.array):l;this.offset=0;this.markedOffset=-1;this.length=0;this.littleEndian="undefined"!=typeof b?!!b:n}c.DEFAULT_CAPACITY=16;c.LITTLE_ENDIAN=k;c.BIG_ENDIAN=n;c.INT8=new Int8Array(1);c.UINT8=new Uint8Array(1);c.INT16=new Int16Array(1);c.UINT16=new Uint16Array(1); | ||
c.INT32=new Int32Array(1);c.UINT32=new Uint32Array(1);c.FLOAT32=new Float32Array(1);c.FLOAT64=new Float64Array(1);c.Long=p;c.cast=function(a,b){a[0]=b;return a[0]};c.allocate=function(a,b){return new c(a,b)};c.wrap=function(a,b){if("string"==typeof a)return(new c).writeUTF8String(a).flip();a.array?a=a.array:a.buffer&&(a=a.buffer);a instanceof ArrayBuffer||g(Error("Cannot wrap buffer of type "+typeof a));var e=new c(0,b,k);e.array=a;e.view=new DataView(e.array);e.offset=0;e.length=a.byteLength;return e}; | ||
c.prototype.LE=function(){this.littleEndian=k;return this};c.prototype.BE=function(){this.littleEndian=n;return this};c.prototype.resize=function(a){if(1>a)return n;this.array==l&&(this.array=new ArrayBuffer(a),this.view=new DataView(this.array));if(this.array.byteLength<a){var b=new Uint8Array(this.array);a=new ArrayBuffer(a);(new Uint8Array(a)).set(b);this.array=a;this.view=new DataView(a);return k}return n};c.prototype.slice=function(a,b){this.array==l&&g(Error(this+" cannot be sliced: Already destroyed")); | ||
b<=a&&g(Error(this+" cannot be sliced: End ("+b+") is less than begin ("+a+")"));(0>a||a>this.array.byteLength||1>b||b>this.array.byteLength)&&g(Error(this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+a+"-"+b+")"));var c=this.clone();c.offset=a;c.length=b;return c};c.prototype.sliceAndCompact=function(a,b){return c.wrap(this.slice(a,b).toArrayBuffer(k))};c.prototype.ensureCapacity=function(a){return this.array==l?this.resize(a):this.array.byteLength<a?this.resize(2*this.array.byteLength>= | ||
a?2*this.array.byteLength:a):n};c.prototype.flip=function(){this.length=this.array==l?0:this.offset;this.offset=0;return this};c.prototype.mark=function(a){this.array==l&&g(Error(this+" cannot be marked: Already destroyed"));a="undefined"!=typeof a?parseInt(a,10):this.offset;(0>a||a>this.array.byteLength)&&g(Error(this+" cannot be marked: Offset to mark is less than 0 or bigger than the capacity ("+this.array.byteLength+"): "+a));this.markedOffset=a;return this};c.prototype.reset=function(){0<=this.markedOffset? | ||
(this.offset=this.markedOffset,this.markedOffset=-1):this.length=this.offset=0;return this};c.prototype.clone=function(){var a=new c(-1,this.littleEndian,k);a.array=this.array;a.view=this.view;a.offset=this.offset;a.length=this.length;return a};c.prototype.copy=function(){if(this.array==l)return this.clone();var a=new c(this.array.byteLength,this.littleEndian),b=new Uint8Array(this.array);(new Uint8Array(a.array)).set(b);a.offset=this.offset;a.length=this.length;return a};c.prototype.remaining=function(){return this.array== | ||
l?0:this.length-this.offset};c.prototype.capacity=function(){return this.array!=l?this.array.byteLength:0};c.prototype.compact=function(){this.array==l&&g(Error(this+" cannot be compacted: Already destroyed"));this.offset>this.length&&this.flip();this.offset==this.length&&g(Error(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"));if(0==this.offset&&this.length==this.array.byteLength)return this;var a=new Uint8Array(this.array),b=new ArrayBuffer(this.length- | ||
this.offset);(new Uint8Array(b)).set(a.subarray(this.offset,this.length));this.array=b;this.offset=0;this.length=this.array.byteLength;return this};c.prototype.destroy=function(){if(this.array!=l)return this.view=this.array=l,this.length=this.offset=0,this};c.prototype.reverse=function(){this.array==l&&g(Error(this+" cannot be reversed: Already destroyed"));Array.prototype.reverse.call(new Uint8Array(this.array));var a=this.offset;this.offset=this.array.byteLength-this.length;this.length=this.array.byteLength- | ||
a;return this};c.prototype.append=function(a,b){a instanceof c||(a=c.wrap(a));a.array==l&&g(Error(a+" cannot be appended to "+this+": Already destroyed"));var e=a.length-a.offset;if(0==e)return this;0>e&&(a=a.clone().flip(),e=a.length-a.offset);b="undefined"!=typeof b?b:(this.offset+=e)-e;this.ensureCapacity(b+e);e=new Uint8Array(a.array);(new Uint8Array(this.array)).set(e.subarray(a.offset,a.length),b);return this};c.prototype.prepend=function(a,b){a instanceof c||(a=c.wrap(a));a.array==l&&g(a+" cannot be prepended to "+ | ||
this+": Already destroyed");var e=a.length-a.offset;if(0==e)return this;0>e&&(a=a.clone().flip(),e=a.length-a.offset);var d="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var f=e-b;0<f?(this.ensureCapacity(this.length+f),this.append(this,e),this.offset+=f,this.length+=f,this.append(a,0)):this.append(a,b-e);d&&(this.offset-=e);return this};c.prototype.writeInt8=function(a,b){b="undefined"!=typeof b?b:(this.offset+=1)-1;this.ensureCapacity(b+1);this.view.setInt8(b,a);return this};c.prototype.readInt8= | ||
function(a){a="undefined"!=typeof a?a:(this.offset+=1)-1;a>=this.array.byteLength&&g(Error("Cannot read int8 from "+this+": Capacity overflow"));return this.view.getInt8(a)};c.prototype.writeByte=c.prototype.writeInt8;c.prototype.readByte=c.prototype.readInt8;c.prototype.writeUint8=function(a,b){b="undefined"!=typeof b?b:(this.offset+=1)-1;this.ensureCapacity(b+1);this.view.setUint8(b,a);return this};c.prototype.readUint8=function(a){a="undefined"!=typeof a?a:(this.offset+=1)-1;a>=this.array.byteLength&& | ||
g("Cannot read uint8 from "+this+": Capacity overflow");return this.view.getUint8(a)};c.prototype.writeInt16=function(a,b){b="undefined"!=typeof b?b:(this.offset+=2)-2;this.ensureCapacity(b+2);this.view.setInt16(b,a,this.littleEndian);return this};c.prototype.readInt16=function(a){a="undefined"!=typeof a?a:(this.offset+=2)-2;a+2>this.array.byteLength&&g(Error("Cannot read int16 from "+this+": Capacity overflow"));return this.view.getInt16(a,this.littleEndian)};c.prototype.writeShort=c.prototype.writeInt16; | ||
c.prototype.readShort=c.prototype.readInt16;c.prototype.writeUint16=function(a,b){b="undefined"!=typeof b?b:(this.offset+=2)-2;this.ensureCapacity(b+2);this.view.setUint16(b,a,this.littleEndian);return this};c.prototype.readUint16=function(a){a="undefined"!=typeof a?a:(this.offset+=2)-2;a+2>this.array.c&&g(Error("Cannot read int16 from "+this+": Capacity overflow"));return this.view.getUint16(a,this.littleEndian)};c.prototype.writeInt32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+ | ||
4);this.view.setInt32(b,a,this.littleEndian);return this};c.prototype.readInt32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;a+4>this.array.byteLength&&g(Error("Cannot read int32 from "+this+": Capacity overflow"));return this.view.getInt32(a,this.littleEndian)};c.prototype.writeInt=c.prototype.writeInt32;c.prototype.readInt=c.prototype.readInt32;c.prototype.writeUint32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+4);this.view.setUint32(b,a,this.littleEndian); | ||
return this};c.prototype.readUint32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;a+4>this.array.byteLength&&g(Error("Cannot read uint32 from "+this+": Capacity overflow"));return this.view.getUint32(a,this.littleEndian)};c.prototype.writeFloat32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+4);this.view.setFloat32(b,a,this.littleEndian);return this};c.prototype.readFloat32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;(this.array==l||a+4>this.array.byteLength)&& | ||
g(Error("Cannot read float32 from "+this+": Capacity overflow"));return this.view.getFloat32(a,this.littleEndian)};c.prototype.writeFloat=c.prototype.writeFloat32;c.prototype.readFloat=c.prototype.readFloat32;c.prototype.writeFloat64=function(a,b){b="undefined"!=typeof b?b:(this.offset+=8)-8;this.ensureCapacity(b+8);this.view.setFloat64(b,a,this.littleEndian);return this};c.prototype.readFloat64=function(a){a="undefined"!=typeof a?a:(this.offset+=8)-8;(this.array==l||a+8>this.array.byteLength)&&g(Error("Cannot read float64 from "+ | ||
this+": Capacity overflow"));return this.view.getFloat64(a,this.littleEndian)};c.prototype.writeDouble=c.prototype.writeFloat64;c.prototype.readDouble=c.prototype.readFloat64;c.prototype.writeInt64=function(a,b){p||g(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));b="undefined"!=typeof b?b:(this.offset+=8)-8;"object"==typeof a&&a instanceof p||(a=p.fromNumber(a,n));this.ensureCapacity(b+8);this.littleEndian?(this.view.setInt32(b, | ||
a.getLowBits(),k),this.view.setInt32(b+4,a.getHighBits(),k)):(this.view.setInt32(b,a.getHighBits(),n),this.view.setInt32(b+4,a.getLowBits(),n));return this};c.prototype.readInt64=function(a){p||g(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));a="undefined"!=typeof a?a:(this.offset+=8)-8;if(this.array==l||a+8>this.array.byteLength)this.offset-=8,g(Error("Cannot read int64 from "+this+": Capacity overflow"));return this.littleEndian? | ||
p.fromBits(this.view.getInt32(a,k),this.view.getInt32(a+4,k),n):p.fromBits(this.view.getInt32(a+4,n),this.view.getInt32(a,n),n)};c.prototype.writeUint64=function(a,b){p||g(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));b="undefined"!=typeof b?b:(this.offset+=8)-8;"object"==typeof a&&a instanceof p||(a=p.fromNumber(a,k));this.ensureCapacity(b+8);this.littleEndian?(this.view.setUint32(b,a.getLowBitsUnsigned(),k),this.view.setUint32(b+ | ||
4,a.getHighBitsUnsigned(),k)):(this.view.setUint32(b,a.getHighBitsUnsigned(),n),this.view.setUint32(b+4,a.getLowBitsUnsigned(),n));return this};c.prototype.readUint64=function(a){p||g(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));a="undefined"!=typeof a?a:(this.offset+=8)-8;if(this.array==l||a+8>this.array.byteLength)this.offset-=8,g(Error("Cannot read int64 from "+this+": Capacity overflow"));return this.littleEndian?p.fromBits(this.view.getUint32(a, | ||
k),this.view.getUint32(a+4,k),k):p.fromBits(this.view.getUint32(a+4,n),this.view.getUint32(a,n),k)};c.prototype.writeLong=c.prototype.writeInt64;c.prototype.readLong=c.prototype.readInt64;c.a=5;c.prototype.writeVarint32=function(a,b){var e="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;c.UINT32[0]=a;this.ensureCapacity(b+c.calculateVarint32(a=c.UINT32[0]));var d=new Uint8Array(this.array),f=0;d[b]=a|128;128<=a?(d[b+1]=a>>7|128,16384<=a?(d[b+2]=a>>14|128,2097152<=a?(d[b+3]=a>>21|128,268435456<= | ||
a?(d[b+4]=a>>28&127,f=5):(d[b+3]&=127,f=4)):(d[b+2]&=127,f=3)):(d[b+1]&=127,f=2)):(d[b]&=127,f=1);return e?(this.offset+=f,this):f};c.prototype.readVarint32=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var e=new Uint8Array(this.array),d=0,f;c.UINT32[0]=0;do d==c.a&&g(Error("Cannot read Varint32 from "+this+"@"+a+": Number of bytes is larger than "+c.a)),f=e[a+d],c.UINT32[0]|=(f&127)<<7*d,++d;while(f&128);c.INT32[0]=c.UINT32[0];return b?(this.offset+=d,c.INT32[0]): | ||
{value:c.INT32[0],length:d}};c.prototype.writeZigZagVarint32=function(a,b){return this.writeVarint32(c.zigZagEncode32(a),b)};c.prototype.readZigZagVarint32=function(a){a=this.readVarint32(a);return"object"==typeof a?(a.value=c.zigZagDecode32(a.value),a):c.zigZagDecode32(a)};c.prototype.writeVarint=c.prototype.writeVarint32;c.prototype.readVarint=c.prototype.readVarint32;c.calculateVarint32=function(a){c.UINT32[0]=a;return 128>c.UINT32[0]?1:16384>c.UINT32[0]?2:2097152>c.UINT32[0]?3:268435456>c.UINT32[0]? | ||
4:5};c.zigZagEncode32=function(a){c.INT32[0]=a;return 0<=(a=c.INT32[0])?2*a:2*-a-1};c.zigZagDecode32=function(a){c.UINT32[0]=a;return 0==((a=c.UINT32[0])&1)?a/2:-(a+1)/2};c.decodeUTF8Char=function(a,b){var c=a.readUint8(b),d,f,h,m,p,q=b;0==(c&128)?(d=c,b+=1):192==(c&224)?(d=a.readUint8(b+1),d=(c&31)<<6|d&63,b+=2):224==(c&240)?(d=a.readUint8(b+1),f=a.readUint8(b+2),d=(c&15)<<12|(d&63)<<6|f&63,b+=3):240==(c&248)?(d=a.readUint8(b+1),f=a.readUint8(b+2),h=a.readUint8(b+3),d=(c&7)<<18|(d&63)<<12|(f&63)<< | ||
6|h&63,b+=4):248==(c&252)?(d=a.readUint8(b+1),f=a.readUint8(b+2),h=a.readUint8(b+3),m=a.readUint8(b+4),d=(c&3)<<24|(d&63)<<18|(f&63)<<12|(h&63)<<6|m&63,b+=5):252==(c&254)?(d=a.readUint8(b+1),f=a.readUint8(b+2),h=a.readUint8(b+3),m=a.readUint8(b+4),p=a.readUint8(b+5),d=(c&1)<<30|(d&63)<<24|(f&63)<<18|(h&63)<<12|(m&63)<<6|p&63,b+=6):g(Error("Cannot decode UTF8 character at offset "+b+": charCode (0x"+c.toString(16)+") is invalid"));return{"char":d,length:b-q}};c.encodeUTF8Char=function(a,b,c){var d= | ||
c;0>a&&g(Error("Cannot encode UTF8 character: charCode ("+a+") is negative"));128>a?(b.writeUint8(a&127,c),c+=1):2048>a?(b.writeUint8(a>>6&31|192,c).writeUint8(a&63|128,c+1),c+=2):65536>a?(b.writeUint8(a>>12&15|224,c).writeUint8(a>>6&63|128,c+1).writeUint8(a&63|128,c+2),c+=3):2097152>a?(b.writeUint8(a>>18&7|240,c).writeUint8(a>>12&63|128,c+1).writeUint8(a>>6&63|128,c+2).writeUint8(a&63|128,c+3),c+=4):67108864>a?(b.writeUint8(a>>24&3|248,c).writeUint8(a>>18&63|128,c+1).writeUint8(a>>12&63|128,c+2).writeUint8(a>> | ||
6&63|128,c+3).writeUint8(a&63|128,c+4),c+=5):2147483648>a?(b.writeUint8(a>>30&1|252,c).writeUint8(a>>24&63|128,c+1).writeUint8(a>>18&63|128,c+2).writeUint8(a>>12&63|128,c+3).writeUint8(a>>6&63|128,c+4).writeUint8(a&63|128,c+5),c+=6):g(Error("Cannot encode UTF8 character: charCode (0x"+a.toString(16)+") is too large (>= 0x80000000)"));return c-d};c.calculateUTF8Char=function(a){0>a&&g(Error("Cannot calculate length of UTF8 character: charCode ("+a+") is negative"));if(128>a)return 1;if(2048>a)return 2; | ||
if(65536>a)return 3;if(2097152>a)return 4;if(67108864>a)return 5;if(2147483648>a)return 6;g(Error("Cannot calculate length of UTF8 character: charCode (0x"+a.toString(16)+") is too large (>= 0x80000000)"))};c.b=function(a){a=""+a;for(var b=0,e=0;e<a.length;e++)b+=c.calculateUTF8Char(a.charCodeAt(e));return b};c.prototype.writeUTF8String=function(a,b){var e="undefined"==typeof b,d=b="undefined"!=typeof b?b:this.offset,f=c.b(a);this.ensureCapacity(b+f);for(f=0;f<a.length;f++)b+=c.encodeUTF8Char(a.charCodeAt(f), | ||
this,b);return e?(this.offset=b,this):b-d};c.prototype.readUTF8String=function(a,b){var e="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;for(var d,f="",h=b,m=0;m<a;m++)d=c.decodeUTF8Char(this,b),b+=d.length,f+=String.fromCharCode(d["char"]);return e?(this.offset=b,f):{string:f,length:b-h}};c.prototype.readUTF8StringBytes=function(a,b){var e="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d,f="",h=b;for(a=b+a;b<a;)d=c.decodeUTF8Char(this,b),b+=d.length,f+=String.fromCharCode(d["char"]); | ||
b!=a&&g(Error("Actual string length differs from the specified: "+((b>a?"+":"")+b-a)+" bytes"));return e?(this.offset=b,f):{string:f,length:b-h}};c.prototype.writeLString=function(a,b){a=""+a;var e="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=c.encodeUTF8Char(a.length,this,b),d=d+this.writeUTF8String(a,b+d);return e?(this.offset+=d,this):d};c.prototype.readLString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var e=c.decodeUTF8Char(this,a);a=this.readUTF8String(e["char"], | ||
a+e.length);return b?(this.offset+=e.length+a.length,a.string):{string:a.string,length:e.length+a.length}};c.prototype.writeVString=function(a,b){a=""+a;var e="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=this.writeVarint32(c.b(a),b),d=d+this.writeUTF8String(a,b+d);return e?(this.offset+=d,this):d};c.prototype.readVString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var c=this.readVarint32(a);a=this.readUTF8StringBytes(c.value,a+c.length);return b? | ||
(this.offset+=c.length+a.length,a.string):{string:a.string,length:c.length+a.length}};c.prototype.writeCString=function(a,b){var c="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=this.writeUTF8String(""+a,b);this.writeUint8(0,b+d);return c?(this.offset+=d+1,this):d+1};c.prototype.readCString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var e,d="",f=a;do e=c.decodeUTF8Char(this,a),a+=e.length,0!=e["char"]&&(d+=String.fromCharCode(e["char"]));while(0!= | ||
e["char"]);return b?(this.offset=a,d):{string:d,length:a-f}};c.prototype.writeJSON=function(a,b,c){c="function"==typeof c?c:JSON.stringify;return this.writeLString(c(a),b)};c.prototype.readJSON=function(a,b){b="function"==typeof b?b:JSON.parse;var c=this.readLString(a);return"string"==typeof c?b(c):{data:b(c.string),length:c.length}};c.prototype.printDebug=function(a){for(var b=(this.array!=l?"ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+ | ||
")":"ByteBuffer(DESTROYED)")+"\n-------------------------------------------------------------------\n",c=this.toHex(16,k),d=this.toASCII(16,k),f=0;f<c.length;f++)b+=c[f]+" "+d[f]+"\n";if(a===k)return b;"function"==typeof a?a(b):console.log(b)};c.prototype.toHex=function(a,b){if(this.array==l)return"DESTROYED";b=!!b;a="undefined"!=typeof a?parseInt(a,10):16;1>a&&(a=16);for(var c="",d=[],f=new Uint8Array(this.array),c=0==this.offset&&0==this.length?c+"|":0==this.length?c+">":0==this.offset?c+"<":c+ | ||
" ",h=0;h<this.array.byteLength;h++){0<h&&0==h%a&&(d.push(c),c=" ");var m=f[h],m=m.toString(16).toUpperCase();2>m.length&&(m="0"+m);c+=m;c=h+1==this.offset&&h+1==this.length?c+"|":h+1==this.offset?c+"<":h+1==this.length?c+">":c+" "}if(b)for(;c.length<3*a+1;)c+=" ";d.push(c);return b?d:d.join("\n")};c.prototype.toASCII=function(a,b){if(this.array==l)return"";b=!!b;a="undefined"!=typeof a?parseInt(a,10):16;1>a&&(a=16);for(var c="",d=[],f=new Uint8Array(this.array),h=0;h<this.array.byteLength;h++){0< | ||
h&&0==h%a&&(d.push(c),c="");var m=f[h],m=32<m&&127>m?String.fromCharCode(m):".",c=c+m}d.push(c);return b?d:d.join("\n")+"\n"};c.prototype.toString=function(){return this.array==l?"ByteBuffer(DESTROYED)":"ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")"};c.prototype.toArrayBuffer=function(a){var b=this.clone();b.offset>b.length&&b.flip();var c=n;if(0<b.offset||b.length<b.array.byteLength)b.compact(),c=k;return a&&!c?b.copy().array: | ||
b.array};c.extend=function(a,b){"string"==typeof a&&"function"==typeof b?c.prototype[a]=b:g(Error("Cannot extend prototype with "+a+"="+b+" (exptected string and function)"))};return c}"undefined"!=typeof module&&module.exports?module.exports=r(require("long")):"undefined"!=typeof define&&define.amd?define("ByteBuffer",["Math/Long"],function(p){return r(p)}):(q.dcodeIO||(q.dcodeIO={}),q.dcodeIO.ByteBuffer=r(dcodeIO.Long))})(this); | ||
function k(r){throw r;}var m=!0,n=null,p=!1; | ||
(function(r){function s(g){function c(a,b){a="undefined"!=typeof a?parseInt(a,10):c.DEFAULT_CAPACITY;1>a&&(a=c.DEFAULT_CAPACITY);this.array=3==arguments.length&&arguments[2]===m?n:new ArrayBuffer(a);this.view=this.array!=n?new DataView(this.array):n;this.offset=0;this.markedOffset=-1;this.length=0;this.littleEndian="undefined"!=typeof b?!!b:p}c.DEFAULT_CAPACITY=16;c.LITTLE_ENDIAN=m;c.BIG_ENDIAN=p;c.INT8=new Int8Array(1);c.UINT8=new Uint8Array(1);c.INT16=new Int16Array(1);c.UINT16=new Uint16Array(1); | ||
c.INT32=new Int32Array(1);c.UINT32=new Uint32Array(1);c.FLOAT32=new Float32Array(1);c.FLOAT64=new Float64Array(1);c.Long=g;c.cast=function(a,b){a[0]=b;return a[0]};c.allocate=function(a,b){return new c(a,b)};c.wrap=function(a,b){if("string"==typeof a)return(new c).writeUTF8String(a).flip();a.array?a=a.array:a.buffer&&(a=a.buffer);a instanceof ArrayBuffer||k(Error("Cannot wrap buffer of type "+typeof a));var f=new c(0,b,m);f.array=a;f.view=new DataView(f.array);f.offset=0;f.length=a.byteLength;return f}; | ||
c.prototype.LE=function(){this.littleEndian=m;return this};c.prototype.BE=function(){this.littleEndian=p;return this};c.prototype.resize=function(a){if(1>a)return p;this.array==n&&(this.array=new ArrayBuffer(a),this.view=new DataView(this.array));if(this.array.byteLength<a){var b=new Uint8Array(this.array);a=new ArrayBuffer(a);(new Uint8Array(a)).set(b);this.array=a;this.view=new DataView(a);return m}return p};c.prototype.slice=function(a,b){this.array==n&&k(Error(this+" cannot be sliced: Already destroyed")); | ||
b<=a&&k(Error(this+" cannot be sliced: End ("+b+") is less than begin ("+a+")"));(0>a||a>this.array.byteLength||1>b||b>this.array.byteLength)&&k(Error(this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+a+"-"+b+")"));var c=this.clone();c.offset=a;c.length=b;return c};c.prototype.sliceAndCompact=function(a,b){return c.wrap(this.slice(a,b).toArrayBuffer(m))};c.prototype.ensureCapacity=function(a){return this.array==n?this.resize(a):this.array.byteLength<a?this.resize(2*this.array.byteLength>= | ||
a?2*this.array.byteLength:a):p};c.prototype.flip=function(){this.length=this.array==n?0:this.offset;this.offset=0;return this};c.prototype.mark=function(a){this.array==n&&k(Error(this+" cannot be marked: Already destroyed"));a="undefined"!=typeof a?parseInt(a,10):this.offset;(0>a||a>this.array.byteLength)&&k(Error(this+" cannot be marked: Offset to mark is less than 0 or bigger than the capacity ("+this.array.byteLength+"): "+a));this.markedOffset=a;return this};c.prototype.reset=function(){0<=this.markedOffset? | ||
(this.offset=this.markedOffset,this.markedOffset=-1):this.length=this.offset=0;return this};c.prototype.clone=function(){var a=new c(-1,this.littleEndian,m);a.array=this.array;a.view=this.view;a.offset=this.offset;a.length=this.length;return a};c.prototype.copy=function(){if(this.array==n)return this.clone();var a=new c(this.array.byteLength,this.littleEndian),b=new Uint8Array(this.array);(new Uint8Array(a.array)).set(b);a.offset=this.offset;a.length=this.length;return a};c.prototype.remaining=function(){return this.array== | ||
n?0:this.length-this.offset};c.prototype.capacity=function(){return this.array!=n?this.array.byteLength:0};c.prototype.compact=function(){this.array==n&&k(Error(this+" cannot be compacted: Already destroyed"));this.offset>this.length&&this.flip();this.offset==this.length&&k(Error(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"));if(0==this.offset&&this.length==this.array.byteLength)return this;var a=new Uint8Array(this.array),b=new ArrayBuffer(this.length- | ||
this.offset);(new Uint8Array(b)).set(a.subarray(this.offset,this.length));this.array=b;this.offset=0;this.length=this.array.byteLength;return this};c.prototype.destroy=function(){if(this.array!=n)return this.view=this.array=n,this.length=this.offset=0,this};c.prototype.reverse=function(){this.array==n&&k(Error(this+" cannot be reversed: Already destroyed"));Array.prototype.reverse.call(new Uint8Array(this.array));var a=this.offset;this.offset=this.array.byteLength-this.length;this.length=this.array.byteLength- | ||
a;return this};c.prototype.append=function(a,b){a instanceof c||(a=c.wrap(a));a.array==n&&k(Error(a+" cannot be appended to "+this+": Already destroyed"));var f=a.length-a.offset;if(0==f)return this;0>f&&(a=a.clone().flip(),f=a.length-a.offset);b="undefined"!=typeof b?b:(this.offset+=f)-f;this.ensureCapacity(b+f);f=new Uint8Array(a.array);(new Uint8Array(this.array)).set(f.subarray(a.offset,a.length),b);return this};c.prototype.prepend=function(a,b){a instanceof c||(a=c.wrap(a));a.array==n&&k(a+" cannot be prepended to "+ | ||
this+": Already destroyed");var f=a.length-a.offset;if(0==f)return this;0>f&&(a=a.clone().flip(),f=a.length-a.offset);var d="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var e=f-b;0<e?(this.ensureCapacity(this.length+e),this.append(this,f),this.offset+=e,this.length+=e,this.append(a,0)):this.append(a,b-f);d&&(this.offset-=f);return this};c.prototype.writeInt8=function(a,b){b="undefined"!=typeof b?b:(this.offset+=1)-1;this.ensureCapacity(b+1);this.view.setInt8(b,a);return this};c.prototype.readInt8= | ||
function(a){a="undefined"!=typeof a?a:(this.offset+=1)-1;a>=this.array.byteLength&&k(Error("Cannot read int8 from "+this+": Capacity overflow"));return this.view.getInt8(a)};c.prototype.writeByte=c.prototype.writeInt8;c.prototype.readByte=c.prototype.readInt8;c.prototype.writeUint8=function(a,b){b="undefined"!=typeof b?b:(this.offset+=1)-1;this.ensureCapacity(b+1);this.view.setUint8(b,a);return this};c.prototype.readUint8=function(a){a="undefined"!=typeof a?a:(this.offset+=1)-1;a>=this.array.byteLength&& | ||
k("Cannot read uint8 from "+this+": Capacity overflow");return this.view.getUint8(a)};c.prototype.writeInt16=function(a,b){b="undefined"!=typeof b?b:(this.offset+=2)-2;this.ensureCapacity(b+2);this.view.setInt16(b,a,this.littleEndian);return this};c.prototype.readInt16=function(a){a="undefined"!=typeof a?a:(this.offset+=2)-2;a+2>this.array.byteLength&&k(Error("Cannot read int16 from "+this+": Capacity overflow"));return this.view.getInt16(a,this.littleEndian)};c.prototype.writeShort=c.prototype.writeInt16; | ||
c.prototype.readShort=c.prototype.readInt16;c.prototype.writeUint16=function(a,b){b="undefined"!=typeof b?b:(this.offset+=2)-2;this.ensureCapacity(b+2);this.view.setUint16(b,a,this.littleEndian);return this};c.prototype.readUint16=function(a){a="undefined"!=typeof a?a:(this.offset+=2)-2;a+2>this.array.b&&k(Error("Cannot read int16 from "+this+": Capacity overflow"));return this.view.getUint16(a,this.littleEndian)};c.prototype.writeInt32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+ | ||
4);this.view.setInt32(b,a,this.littleEndian);return this};c.prototype.readInt32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;a+4>this.array.byteLength&&k(Error("Cannot read int32 from "+this+": Capacity overflow"));return this.view.getInt32(a,this.littleEndian)};c.prototype.writeInt=c.prototype.writeInt32;c.prototype.readInt=c.prototype.readInt32;c.prototype.writeUint32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+4);this.view.setUint32(b,a,this.littleEndian); | ||
return this};c.prototype.readUint32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;a+4>this.array.byteLength&&k(Error("Cannot read uint32 from "+this+": Capacity overflow"));return this.view.getUint32(a,this.littleEndian)};c.prototype.writeFloat32=function(a,b){b="undefined"!=typeof b?b:(this.offset+=4)-4;this.ensureCapacity(b+4);this.view.setFloat32(b,a,this.littleEndian);return this};c.prototype.readFloat32=function(a){a="undefined"!=typeof a?a:(this.offset+=4)-4;(this.array==n||a+4>this.array.byteLength)&& | ||
k(Error("Cannot read float32 from "+this+": Capacity overflow"));return this.view.getFloat32(a,this.littleEndian)};c.prototype.writeFloat=c.prototype.writeFloat32;c.prototype.readFloat=c.prototype.readFloat32;c.prototype.writeFloat64=function(a,b){b="undefined"!=typeof b?b:(this.offset+=8)-8;this.ensureCapacity(b+8);this.view.setFloat64(b,a,this.littleEndian);return this};c.prototype.readFloat64=function(a){a="undefined"!=typeof a?a:(this.offset+=8)-8;(this.array==n||a+8>this.array.byteLength)&&k(Error("Cannot read float64 from "+ | ||
this+": Capacity overflow"));return this.view.getFloat64(a,this.littleEndian)};c.prototype.writeDouble=c.prototype.writeFloat64;c.prototype.readDouble=c.prototype.readFloat64;c.prototype.writeInt64=function(a,b){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));b="undefined"!=typeof b?b:(this.offset+=8)-8;"object"==typeof a&&a instanceof g||(a=g.fromNumber(a,p));this.ensureCapacity(b+8);this.littleEndian?(this.view.setInt32(b, | ||
a.getLowBits(),m),this.view.setInt32(b+4,a.getHighBits(),m)):(this.view.setInt32(b,a.getHighBits(),p),this.view.setInt32(b+4,a.getLowBits(),p));return this};c.prototype.readInt64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));a="undefined"!=typeof a?a:(this.offset+=8)-8;if(this.array==n||a+8>this.array.byteLength)this.offset-=8,k(Error("Cannot read int64 from "+this+": Capacity overflow"));return this.littleEndian? | ||
g.fromBits(this.view.getInt32(a,m),this.view.getInt32(a+4,m),p):g.fromBits(this.view.getInt32(a+4,p),this.view.getInt32(a,p),p)};c.prototype.writeUint64=function(a,b){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));b="undefined"!=typeof b?b:(this.offset+=8)-8;"object"==typeof a&&a instanceof g||(a=g.fromNumber(a,m));this.ensureCapacity(b+8);this.littleEndian?(this.view.setUint32(b,a.getLowBitsUnsigned(),m),this.view.setUint32(b+ | ||
4,a.getHighBitsUnsigned(),m)):(this.view.setUint32(b,a.getHighBitsUnsigned(),p),this.view.setUint32(b+4,a.getLowBitsUnsigned(),p));return this};c.prototype.readUint64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));a="undefined"!=typeof a?a:(this.offset+=8)-8;if(this.array==n||a+8>this.array.byteLength)this.offset-=8,k(Error("Cannot read int64 from "+this+": Capacity overflow"));return this.littleEndian?g.fromBits(this.view.getUint32(a, | ||
m),this.view.getUint32(a+4,m),m):g.fromBits(this.view.getUint32(a+4,p),this.view.getUint32(a,p),m)};c.prototype.writeLong=c.prototype.writeInt64;c.prototype.readLong=c.prototype.readInt64;c.MAX_VARINT32_BYTES=5;c.prototype.writeVarint32=function(a,b){var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;c.UINT32[0]=a;this.ensureCapacity(b+c.calculateVarint32(a=c.UINT32[0]));var d=new Uint8Array(this.array),e=0;d[b]=a|128;128<=a?(d[b+1]=a>>7|128,16384<=a?(d[b+2]=a>>14|128,2097152<=a?(d[b+ | ||
3]=a>>21|128,268435456<=a?(d[b+4]=a>>28&127,e=5):(d[b+3]&=127,e=4)):(d[b+2]&=127,e=3)):(d[b+1]&=127,e=2)):(d[b]&=127,e=1);return f?(this.offset+=e,this):e};c.prototype.readVarint32=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var f=new Uint8Array(this.array),d=0,e;c.UINT32[0]=0;do d==c.MAX_VARINT32_BYTES&&k(Error("Cannot read Varint32 from "+this+"@"+a+": Number of bytes is larger than "+c.MAX_VARINT32_BYTES)),e=f[a+d],c.UINT32[0]|=(e&127)<<7*d,++d;while(e&128);c.INT32[0]= | ||
c.UINT32[0];return b?(this.offset+=d,c.INT32[0]):{value:c.INT32[0],length:d}};c.prototype.writeZigZagVarint32=function(a,b){return this.writeVarint32(c.zigZagEncode32(a),b)};c.prototype.readZigZagVarint32=function(a){a=this.readVarint32(a);return"object"==typeof a?(a.value=c.zigZagDecode32(a.value),a):c.zigZagDecode32(a)};c.MAX_VARINT64_BYTES=10;c.prototype.writeVarint64=function(a,b){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details")); | ||
var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;"object"==typeof a&&a instanceof g||(a=g.fromNumber(a,p));var d=a.toInt()>>>0,e=a.shiftRightUnsigned(28).toInt()>>>0,l=a.shiftRightUnsigned(56).toInt()>>>0,q=c.calculateVarint64(a),h=new Uint8Array(this.array);this.ensureCapacity(b+q);switch(q){case 10:h[b+9]=l>>>7|128;case 9:h[b+8]=l|128;case 8:h[b+7]=e>>>21|128;case 7:h[b+6]=e>>>14|128;case 6:h[b+5]=e>>>7|128;case 5:h[b+4]=e|128;case 4:h[b+3]=d>>>21|128;case 3:h[b+2]=d>>>14|128;case 2:h[b+ | ||
1]=d>>>7|128;case 1:h[b+0]=d|128}h[b+q-1]&=127;return f?(this.offset+=q,this):q};c.prototype.readVarint64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));var b="undefined"==typeof a,c=a="undefined"!=typeof a?a:this.offset,d=new Uint8Array(this.array),e,l=0,q=0,h;h=d[a++];e=h&127;h&128&&(h=d[a++],e|=(h&127)<<7,h&128&&(h=d[a++],e|=(h&127)<<14,h&128&&(h=d[a++],e|=(h&127)<<21,h&128&&(h=d[a++],l=h&127,h&128&&(h=d[a++], | ||
l|=(h&127)<<7,h&128&&(h=d[a++],l|=(h&127)<<14,h&128&&(h=d[a++],l|=(h&127)<<21,h&128&&(h=d[a++],q=h&127,h&128&&(h=d[a++],q|=(h&127)<<7,h&128&&k(Error("Data must be corrupt: Buffer overrun")))))))))));d=g.from28Bits(e,l,q,p);return b?(this.offset=a,d):{value:d,length:a-c}};c.prototype.writeZigZagVarint64=function(a,b){return this.writeVarint64(c.zigZagEncode64(a),b)};c.prototype.readZigZagVarint64=function(a){a=this.readVarint64(a);return"object"==typeof a?(a.value=c.zigZagDecode64(a.value),a):c.zigZagDecode64(a)}; | ||
c.prototype.writeVarint=c.prototype.writeVarint32;c.prototype.readVarint=c.prototype.readVarint32;c.prototype.writeZigZagVarint=c.prototype.writeZigZagVarint32;c.prototype.readZigZagVarint=c.prototype.readZigZagVarint32;c.calculateVarint32=function(a){a>>>=0;return 128>a?1:16384>a?2:2097152>a?3:268435456>a?4:5};c.calculateVarint64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));"object"==typeof a&&a instanceof | ||
g||(a=g.fromNumber(a,p));var b=a.toInt()>>>0,c=a.shiftRightUnsigned(28).toInt()>>>0;a=a.shiftRightUnsigned(56).toInt()>>>0;return 0==a?0==c?16384>b?128>b?1:2:2097152>b?3:4:16384>c?128>c?5:6:2097152>c?7:8:128>a?9:10};c.zigZagEncode32=function(a){return((a|=0)<<1^a>>31)>>>0};c.zigZagDecode32=function(a){return a>>>1^-(a&1)|0};c.zigZagEncode64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));"object"==typeof a&&a instanceof | ||
g?a.unsigned&&(a=a.toSigned()):a=g.fromNumber(a,p);return a.shiftLeft(1).xor(a.shiftRight(63)).toUnsigned()};c.zigZagDecode64=function(a){g||k(Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details"));"object"==typeof a&&a instanceof g?a.unsigned||(a=a.toUnsigned()):a=g.fromNumber(a,m);return a.shiftRightUnsigned(1).xor(a.and(g.ONE).toSigned().negate()).toSigned()};c.decodeUTF8Char=function(a,b){var c=a.readUint8(b),d,e,l,q,h,g=b;0==(c& | ||
128)?(d=c,b+=1):192==(c&224)?(d=a.readUint8(b+1),d=(c&31)<<6|d&63,b+=2):224==(c&240)?(d=a.readUint8(b+1),e=a.readUint8(b+2),d=(c&15)<<12|(d&63)<<6|e&63,b+=3):240==(c&248)?(d=a.readUint8(b+1),e=a.readUint8(b+2),l=a.readUint8(b+3),d=(c&7)<<18|(d&63)<<12|(e&63)<<6|l&63,b+=4):248==(c&252)?(d=a.readUint8(b+1),e=a.readUint8(b+2),l=a.readUint8(b+3),q=a.readUint8(b+4),d=(c&3)<<24|(d&63)<<18|(e&63)<<12|(l&63)<<6|q&63,b+=5):252==(c&254)?(d=a.readUint8(b+1),e=a.readUint8(b+2),l=a.readUint8(b+3),q=a.readUint8(b+ | ||
4),h=a.readUint8(b+5),d=(c&1)<<30|(d&63)<<24|(e&63)<<18|(l&63)<<12|(q&63)<<6|h&63,b+=6):k(Error("Cannot decode UTF8 character at offset "+b+": charCode (0x"+c.toString(16)+") is invalid"));return{"char":d,length:b-g}};c.encodeUTF8Char=function(a,b,c){var d=c;0>a&&k(Error("Cannot encode UTF8 character: charCode ("+a+") is negative"));128>a?(b.writeUint8(a&127,c),c+=1):2048>a?(b.writeUint8(a>>6&31|192,c).writeUint8(a&63|128,c+1),c+=2):65536>a?(b.writeUint8(a>>12&15|224,c).writeUint8(a>>6&63|128,c+1).writeUint8(a& | ||
63|128,c+2),c+=3):2097152>a?(b.writeUint8(a>>18&7|240,c).writeUint8(a>>12&63|128,c+1).writeUint8(a>>6&63|128,c+2).writeUint8(a&63|128,c+3),c+=4):67108864>a?(b.writeUint8(a>>24&3|248,c).writeUint8(a>>18&63|128,c+1).writeUint8(a>>12&63|128,c+2).writeUint8(a>>6&63|128,c+3).writeUint8(a&63|128,c+4),c+=5):2147483648>a?(b.writeUint8(a>>30&1|252,c).writeUint8(a>>24&63|128,c+1).writeUint8(a>>18&63|128,c+2).writeUint8(a>>12&63|128,c+3).writeUint8(a>>6&63|128,c+4).writeUint8(a&63|128,c+5),c+=6):k(Error("Cannot encode UTF8 character: charCode (0x"+ | ||
a.toString(16)+") is too large (>= 0x80000000)"));return c-d};c.calculateUTF8Char=function(a){0>a&&k(Error("Cannot calculate length of UTF8 character: charCode ("+a+") is negative"));if(128>a)return 1;if(2048>a)return 2;if(65536>a)return 3;if(2097152>a)return 4;if(67108864>a)return 5;if(2147483648>a)return 6;k(Error("Cannot calculate length of UTF8 character: charCode (0x"+a.toString(16)+") is too large (>= 0x80000000)"))};c.a=function(a){a=""+a;for(var b=0,f=0;f<a.length;f++)b+=c.calculateUTF8Char(a.charCodeAt(f)); | ||
return b};c.prototype.writeUTF8String=function(a,b){var f="undefined"==typeof b,d=b="undefined"!=typeof b?b:this.offset,e=c.a(a);this.ensureCapacity(b+e);for(e=0;e<a.length;e++)b+=c.encodeUTF8Char(a.charCodeAt(e),this,b);return f?(this.offset=b,this):b-d};c.prototype.readUTF8String=function(a,b){var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;for(var d,e="",l=b,g=0;g<a;g++)d=c.decodeUTF8Char(this,b),b+=d.length,e+=String.fromCharCode(d["char"]);return f?(this.offset=b,e):{string:e, | ||
length:b-l}};c.prototype.readUTF8StringBytes=function(a,b){var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d,e="",l=b;for(a=b+a;b<a;)d=c.decodeUTF8Char(this,b),b+=d.length,e+=String.fromCharCode(d["char"]);b!=a&&k(Error("Actual string length differs from the specified: "+((b>a?"+":"")+b-a)+" bytes"));return f?(this.offset=b,e):{string:e,length:b-l}};c.prototype.writeLString=function(a,b){a=""+a;var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=c.encodeUTF8Char(a.length, | ||
this,b),d=d+this.writeUTF8String(a,b+d);return f?(this.offset+=d,this):d};c.prototype.readLString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var f=c.decodeUTF8Char(this,a);a=this.readUTF8String(f["char"],a+f.length);return b?(this.offset+=f.length+a.length,a.string):{string:a.string,length:f.length+a.length}};c.prototype.writeVString=function(a,b){a=""+a;var f="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=this.writeVarint32(c.a(a),b),d=d+this.writeUTF8String(a, | ||
b+d);return f?(this.offset+=d,this):d};c.prototype.readVString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var c=this.readVarint32(a);a=this.readUTF8StringBytes(c.value,a+c.length);return b?(this.offset+=c.length+a.length,a.string):{string:a.string,length:c.length+a.length}};c.prototype.writeCString=function(a,b){var c="undefined"==typeof b;b="undefined"!=typeof b?b:this.offset;var d=this.writeUTF8String(""+a,b);this.writeUint8(0,b+d);return c?(this.offset+=d+1,this): | ||
d+1};c.prototype.readCString=function(a){var b="undefined"==typeof a;a="undefined"!=typeof a?a:this.offset;var f,d="",e=a;do f=c.decodeUTF8Char(this,a),a+=f.length,0!=f["char"]&&(d+=String.fromCharCode(f["char"]));while(0!=f["char"]);return b?(this.offset=a,d):{string:d,length:a-e}};c.prototype.writeJSON=function(a,b,c){c="function"==typeof c?c:JSON.stringify;return this.writeLString(c(a),b)};c.prototype.readJSON=function(a,b){b="function"==typeof b?b:JSON.parse;var c=this.readLString(a);return"string"== | ||
typeof c?b(c):{data:b(c.string),length:c.length}};c.prototype.printDebug=function(a){for(var b=(this.array!=n?"ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")":"ByteBuffer(DESTROYED)")+"\n-------------------------------------------------------------------\n",c=this.toHex(16,m),d=this.toASCII(16,m),e=0;e<c.length;e++)b+=c[e]+" "+d[e]+"\n";if(a===m)return b;"function"==typeof a?a(b):console.log(b)};c.prototype.toHex=function(a, | ||
b){if(this.array==n)return"DESTROYED";b=!!b;a="undefined"!=typeof a?parseInt(a,10):16;1>a&&(a=16);for(var c="",d=[],e=new Uint8Array(this.array),c=0==this.offset&&0==this.length?c+"|":0==this.length?c+">":0==this.offset?c+"<":c+" ",l=0;l<this.array.byteLength;l++){0<l&&0==l%a&&(d.push(c),c=" ");var g=e[l],g=g.toString(16).toUpperCase();2>g.length&&(g="0"+g);c+=g;c=l+1==this.offset&&l+1==this.length?c+"|":l+1==this.offset?c+"<":l+1==this.length?c+">":c+" "}if(b)for(;c.length<3*a+1;)c+=" ";d.push(c); | ||
return b?d:d.join("\n")};c.prototype.toASCII=function(a,b){if(this.array==n)return"";b=!!b;a="undefined"!=typeof a?parseInt(a,10):16;1>a&&(a=16);for(var c="",d=[],e=new Uint8Array(this.array),g=0;g<this.array.byteLength;g++){0<g&&0==g%a&&(d.push(c),c="");var q=e[g],q=32<q&&127>q?String.fromCharCode(q):".",c=c+q}d.push(c);return b?d:d.join("\n")+"\n"};c.prototype.toString=function(){return this.array==n?"ByteBuffer(DESTROYED)":"ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+ | ||
this.length+",capacity="+this.array.byteLength+")"};c.prototype.toArrayBuffer=function(a){var b=this.clone();b.offset>b.length&&b.flip();var c=p;if(0<b.offset||b.length<b.array.byteLength)b.compact(),c=m;return a&&!c?b.copy().array:b.array};c.extend=function(a,b){"string"==typeof a&&"function"==typeof b?c.prototype[a]=b:k(Error("Cannot extend prototype with "+a+"="+b+" (exptected string and function)"))};return c}"undefined"!=typeof module&&module.exports?module.exports=s(require("long")):"undefined"!= | ||
typeof define&&define.amd?define("ByteBuffer",["Math/Long"],function(g){return s(g)}):(r.dcodeIO||(r.dcodeIO={}),r.dcodeIO.ByteBuffer=s(dcodeIO.Long))})(this); |
@@ -512,3 +512,3 @@ /* | ||
* @param {number=} offset | ||
* @return {number|!{value: number, length: number}} | ||
* @return {number|{value: number, length: number}} | ||
* @throws {Error} | ||
@@ -519,2 +519,32 @@ */ | ||
/** | ||
* @param {number|Long} value | ||
* @param {number=} offset | ||
* @return {!ByteBuffer|number} | ||
* @throws {Error} | ||
*/ | ||
ByteBuffer.prototype.writeVarint64 = function(value, offset) {}; | ||
/** | ||
* @param {number=} offset | ||
* @return {!Long|{value: !Long, length: number}} | ||
* @throws {Error} | ||
*/ | ||
ByteBuffer.prototype.readVarint64 = function(offset) {}; | ||
/** | ||
* @param {number|Long} value | ||
* @param {number=} offset | ||
* @return {!ByteBuffer|number} | ||
* @throws {Error} | ||
*/ | ||
ByteBuffer.prototype.writeZigZagVarint64 = function(value, offset) {}; | ||
/** | ||
* @param {number=} offset | ||
* @return {!Long|!{value: !Long, length: number}} | ||
* @throws {Error} | ||
*/ | ||
ByteBuffer.prototype.readZigZagVarint64 = function(offset) {}; | ||
/** | ||
* @param {number} value | ||
@@ -535,3 +565,18 @@ * @param {number=} offset | ||
* @param {number} value | ||
* @param {number=} offset | ||
* @return {!ByteBuffer|number} | ||
*/ | ||
ByteBuffer.prototype.writeZigZagVarint = function(value, offset) {}; | ||
/** | ||
* @param {number=} offset | ||
* @return {number|{value: number, length: number}} | ||
* @throws {Error} | ||
*/ | ||
ByteBuffer.prototype.readZigZagVarint = function(offset) {}; | ||
/** | ||
* @param {number} value | ||
* @return {number} | ||
* @throws {Error} | ||
* @nosideeffects | ||
@@ -542,2 +587,10 @@ */ | ||
/** | ||
* @param {number} value | ||
* @return {number} | ||
* @throws {Error} | ||
* @nosideeffects | ||
*/ | ||
ByteBuffer.calculateVarint64 = function(value) {}; | ||
/** | ||
* @param {string} str | ||
@@ -708,1 +761,7 @@ * @return {number} | ||
ByteBuffer.MAX_VARINT32_BYTES = 5; | ||
/** | ||
* @type {number} | ||
* @const | ||
*/ | ||
ByteBuffer.MAX_VARINT64_BYTES = 10; |
@@ -60,2 +60,11 @@ /* | ||
/** | ||
* @param {number} part0 | ||
* @param {number} part1 | ||
* @param {number} part2 | ||
* @param {boolean=} unsigned | ||
* @return {!Long} | ||
*/ | ||
Long.from28Bits = function(part0, part1, part2, unsigned) {}; | ||
/** | ||
* @param {string} str | ||
@@ -306,1 +315,16 @@ * @param {(boolean|number)=} unsigned | ||
Long.prototype.shiftRightUnsigned = function(numBits) {}; | ||
/** | ||
* @return {!Long} | ||
*/ | ||
Long.prototype.toSigned = function() {}; | ||
/** | ||
* @return {!Long} | ||
*/ | ||
Long.prototype.toUnsigned = function() {}; | ||
/** | ||
* @return {!Long} | ||
*/ | ||
Long.prototype.clone = function() {}; |
{ | ||
"name": "bytebuffer", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"author": "Daniel Wirtz <dcode@dcode.io>", | ||
@@ -16,3 +16,3 @@ "description": "ByteBuffer.js: A Java-like, Netty-inspired ByteBuffer implementation using typed arrays.", | ||
"dependencies": { | ||
"long": ">=1.1.0" | ||
"long": ">=1.1.2" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -27,9 +27,11 @@ ![ByteBuffer.js - A Java-like ByteBuffer](https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png) | ||
* `ByteBuffer#writeUint/Int8/16/32(value[, offset])` and `ByteBuffer#readUint/Int8/16/32([offset])` | ||
* `ByteBuffer#writeVarint32(value[, offset])` and `ByteBuffer#readVarint32([offset])` to write a base 128 variable-length | ||
integer as used in [protobuf](https://developers.google.com/protocol-buffers/docs/encoding#varints) | ||
* `ByteBuffer#writeVarint32/64(value[, offset])` and `ByteBuffer#readVarint32/64([offset])` to write a base 128 | ||
variable-length integer as used in [protobuf](https://developers.google.com/protocol-buffers/docs/encoding#varints) | ||
* `ByteBuffer#writeZigZagVarint32/64(value[, offset])` and `ByteBuffer#readZigZagVarint32/64([offset])` to write a | ||
zig-zag encoded base 128 variable-length integer as used in protobuf for efficient encoding of signed values | ||
* `ByteBuffer#writeUint/Int64(value[, offset])` and `ByteBuffer#readUint/Int64([offset])` via [Long.js](https://github.com/dcodeIO/Long.js) | ||
* `ByteBuffer#writeFloat32/64(value[, offset])` and `ByteBuffer#readFloat32/64([offset])` | ||
* `ByteBuffer#write/readByte`, `ByteBuffer#write/readShort`, `ByteBuffer#write/readInt`, `ByteBuffer#write/readLong` | ||
(all signed), `ByteBuffer#write/readVarint`, `ByteBuffer#write/readFloat`, `ByteBuffer#write/readDouble` aliases for | ||
the above for convenience | ||
(all signed), `ByteBuffer#write/readVarint` and `ByteBuffer#write/readZigZagVarint` (both 32bit), | ||
`ByteBuffer#write/readFloat`, `ByteBuffer#write/readDouble` aliases for the above for convenience | ||
* `ByteBuffer#writeUTF8String(str[, offset])` and `ByteBuffer#readUTF8String(chars[, offset])` using the included UTF8 | ||
@@ -74,3 +76,3 @@ en-/decoder (full 6 bytes, [ref](http://en.wikipedia.org/wiki/UTF-8#Description)) | ||
* Well tested through [nodeunit](https://github.com/caolan/nodeunit) | ||
* Zero production dependencies | ||
* Zero production dependencies (Long.js is optional) | ||
* Small footprint | ||
@@ -132,3 +134,3 @@ | ||
As of the [ECMAScript specification](http://ecma262-5.com/ELS5_HTML.htm#Section_8.5), number types have a maximum value | ||
of 2^53. Beyond that, JavaScript falls back to double internally. However, real long support requires the full 64 bits | ||
of 2^53. Beyond that, behaviour might be unexpected. However, real long support requires the full 64 bits | ||
with the possibility to perform bitwise operations on the value for varint en-/decoding. So, to enable true long support | ||
@@ -135,0 +137,0 @@ in ByteBuffer.js, it optionally depends on [Long.js](https://github.com/dcodeIO/Long.js), which actually utilizes two |
@@ -444,4 +444,38 @@ /* | ||
"zigZagEncode/Decode32": function(test) { | ||
"calculateVarint32/64": function(test) { | ||
test.equal(ByteBuffer.MAX_VARINT32_BYTES, 5); | ||
test.equal(ByteBuffer.MAX_VARINT64_BYTES, 10); | ||
var values = [ | ||
[0, 1], | ||
[-1, 5, 10], | ||
[1<<7, 2], | ||
[1<<14, 3], | ||
[1<<21, 4], | ||
[1<<28, 5], | ||
[0x7FFFFFFF | 0, 5], | ||
[0xFFFFFFFF, 5], | ||
[0xFFFFFFFF | 0, 5, 10] | ||
]; | ||
for (var i=0; i<values.length; i++) { | ||
test.equal(ByteBuffer.calculateVarint32(values[i][0]), values[i][1]); | ||
test.equal(ByteBuffer.calculateVarint64(values[i][0]), values[i].length > 2 ? values[i][2] : values[i][1]); | ||
} | ||
var Long = ByteBuffer.Long; | ||
values = [ | ||
[Long.fromNumber(1).shiftLeft(35), 6], | ||
[Long.fromNumber(1).shiftLeft(42), 7], | ||
[Long.fromNumber(1).shiftLeft(49), 8], | ||
[Long.fromNumber(1).shiftLeft(56), 9], | ||
[Long.fromNumber(1).shiftLeft(63), 10], | ||
[Long.fromNumber(1, true).shiftLeft(63), 10] | ||
]; | ||
for (i=0; i<values.length; i++) { | ||
test.equal(ByteBuffer.calculateVarint64(values[i][0]), values[i][1]); | ||
} | ||
test.done(); | ||
}, | ||
"zigZagEncode/Decode32/64": function(test) { | ||
var Long = ByteBuffer.Long; | ||
var values = [ | ||
[ 0, 0], | ||
@@ -460,3 +494,14 @@ [-1, 1], | ||
test.equal(ByteBuffer.zigZagDecode32(values[i][1]), values[i][0]); | ||
test.equal(ByteBuffer.zigZagEncode64(values[i][0]).toNumber(), values[i][1]); | ||
test.equal(ByteBuffer.zigZagDecode64(values[i][1]).toNumber(), values[i][0]); | ||
} | ||
values = [ | ||
[Long.MAX_SIGNED_VALUE, Long.MAX_UNSIGNED_VALUE.subtract(Long.ONE)], | ||
[Long.MIN_SIGNED_VALUE, Long.MAX_UNSIGNED_VALUE] | ||
]; | ||
// NOTE: Even 64bit doubles from toNumber() fail for these values so we are using toString() here | ||
for (i=0; i<values.length; i++) { | ||
test.equal(ByteBuffer.zigZagEncode64(values[i][0]).toString(), values[i][1].toString()); | ||
test.equal(ByteBuffer.zigZagDecode64(values[i][1]).toString(), values[i][0].toString()); | ||
} | ||
test.done(); | ||
@@ -473,3 +518,3 @@ }, | ||
]; | ||
var bb = new ByteBuffer(10); | ||
var bb = new ByteBuffer(5); | ||
for (var i=0; i<values.length; i++) { | ||
@@ -483,17 +528,37 @@ var encLen = bb.writeVarint32(values[i][0], 0); | ||
}, | ||
"write/readVarint64": function(test) { | ||
var Long = ByteBuffer.Long; | ||
var values = [ | ||
[Long.ONE], | ||
[Long.fromNumber(300)], | ||
[Long.fromNumber(0x7FFFFFFF)], | ||
[Long.fromNumber(0xFFFFFFFF)], | ||
[Long.fromBits(0xFFFFFFFF, 0x7FFFFFFF)], | ||
[Long.fromBits(0xFFFFFFFF, 0xFFFFFFFF)] | ||
]; | ||
var bb = new ByteBuffer(10); | ||
for (var i=0; i<values.length; i++) { | ||
var encLen = bb.writeVarint64(values[i][0], 0); | ||
var dec = bb.readVarint64(0); | ||
test.equal((values[i].length > 1 ? values[i][1] : values[i][0]).toString(), dec['value'].toString()); | ||
test.equal(encLen, dec['length']); | ||
} | ||
test.done(); | ||
}, | ||
"write/readZigZagVarint32": function(test) { | ||
var values = [ | ||
[0,0], | ||
[1,1], | ||
[ 300, 300], | ||
[-300,-300], | ||
[ 2147483647, 2147483647], | ||
[-2147483648, -2147483648] | ||
0, | ||
1, | ||
300, | ||
-300, | ||
2147483647, | ||
-2147483648 | ||
]; | ||
var bb = new ByteBuffer(10); | ||
for (var i=0; i<values.length; i++) { | ||
var encLen = bb.writeZigZagVarint32(values[i][0], 0); | ||
var encLen = bb.writeZigZagVarint32(values[i], 0); | ||
var dec = bb.readZigZagVarint32(0); | ||
test.equal(values[i][1], dec['value']); | ||
test.equal(values[i], dec['value']); | ||
test.equal(encLen, dec['length']); | ||
@@ -503,2 +568,25 @@ } | ||
}, | ||
"write/readZigZagVarint64": function(test) { | ||
var Long = ByteBuffer.Long; | ||
var values = [ | ||
Long.ONE, 1, | ||
Long.fromNumber(-3), | ||
Long.fromNumber(300), | ||
Long.fromNumber(-300), | ||
Long.fromNumber(0x7FFFFFFF), | ||
Long.fromNumber(0x8FFFFFFF), | ||
Long.fromNumber(0xFFFFFFFF), | ||
Long.fromBits(0xFFFFFFFF, 0x7FFFFFFF), | ||
Long.fromBits(0xFFFFFFFF, 0xFFFFFFFF) | ||
]; | ||
var bb = new ByteBuffer(10); | ||
for (var i=0; i<values.length; i++) { | ||
var encLen = bb.writeZigZagVarint64(values[i], 0); | ||
var dec = bb.readZigZagVarint64(0); | ||
test.equal(values[i].toString(), dec['value'].toString()); | ||
test.equal(encLen, dec['length']); | ||
} | ||
test.done(); | ||
}, | ||
@@ -505,0 +593,0 @@ "write/readVarint": function(test) { |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
701113
7901
178
Updatedlong@>=1.1.2