|
124 | 124 | * @const
|
125 | 125 | * @expose
|
126 | 126 | */
|
127 |
| - ByteBuffer.VERSION = "4.0.0"; |
| 127 | + ByteBuffer.VERSION = "4.1.0"; |
128 | 128 |
|
129 | 129 | /**
|
130 | 130 | * Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
|
|
2371 | 2371 | ByteBufferPrototype.capacity = function() {
|
2372 | 2372 | return this.buffer.byteLength;
|
2373 | 2373 | };
|
2374 |
| - |
2375 | 2374 | /**
|
2376 | 2375 | * Clears this ByteBuffer's offsets by setting {@link ByteBuffer#offset} to `0` and {@link ByteBuffer#limit} to the
|
2377 | 2376 | * backing buffer's capacity. Discards {@link ByteBuffer#markedOffset}.
|
|
3052 | 3051 | * @param {number=} begin Offset to begin at, defaults to {@link ByteBuffer#offset}.
|
3053 | 3052 | * @param {number=} end Offset to end at, defaults to {@link ByteBuffer#limit}.
|
3054 | 3053 | * @returns {string} Base64 encoded string
|
| 3054 | + * @throws {RangeError} If `begin` or `end` is out of bounds |
3055 | 3055 | * @expose
|
3056 | 3056 | */
|
3057 | 3057 | ByteBufferPrototype.toBase64 = function(begin, end) {
|
3058 | 3058 | if (typeof begin === 'undefined')
|
3059 | 3059 | begin = this.offset;
|
3060 | 3060 | if (typeof end === 'undefined')
|
3061 | 3061 | end = this.limit;
|
3062 |
| - if (!this.noAssert) { |
3063 |
| - if (typeof begin !== 'number' || begin % 1 !== 0) |
3064 |
| - throw TypeError("Illegal begin: Not an integer"); |
3065 |
| - begin >>>= 0; |
3066 |
| - if (typeof end !== 'number' || end % 1 !== 0) |
3067 |
| - throw TypeError("Illegal end: Not an integer"); |
3068 |
| - end >>>= 0; |
3069 |
| - if (begin < 0 || begin > end || end > this.buffer.byteLength) |
3070 |
| - throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength); |
3071 |
| - } |
| 3062 | + begin = begin | 0; end = end | 0; |
| 3063 | + if (begin < 0 || end > this.capacity || begin > end) |
| 3064 | + throw RangeError("begin, end"); |
3072 | 3065 | var sd; lxiv.encode(function() {
|
3073 | 3066 | return begin < end ? this.view[begin++] : null;
|
3074 | 3067 | }.bind(this), sd = stringDestination());
|
|
3080 | 3073 | * @param {string} str String to decode
|
3081 | 3074 | * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
|
3082 | 3075 | * {@link ByteBuffer.DEFAULT_ENDIAN}.
|
3083 |
| - * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to |
3084 |
| - * {@link ByteBuffer.DEFAULT_NOASSERT}. |
3085 | 3076 | * @returns {!ByteBuffer} ByteBuffer
|
3086 | 3077 | * @expose
|
3087 | 3078 | */
|
3088 |
| - ByteBuffer.fromBase64 = function(str, littleEndian, noAssert) { |
3089 |
| - if (!noAssert) { |
3090 |
| - if (typeof str !== 'string') |
3091 |
| - throw TypeError("Illegal str: Not a string"); |
3092 |
| - if (str.length % 4 !== 0) |
3093 |
| - throw TypeError("Illegal str: Length not a multiple of 4"); |
3094 |
| - } |
3095 |
| - var bb = new ByteBuffer(str.length/4*3, littleEndian, noAssert), |
| 3079 | + ByteBuffer.fromBase64 = function(str, littleEndian) { |
| 3080 | + if (typeof str !== 'string') |
| 3081 | + throw TypeError("str"); |
| 3082 | + var bb = new ByteBuffer(str.length/4*3, littleEndian), |
3096 | 3083 | i = 0;
|
3097 | 3084 | lxiv.decode(stringSource(str), function(b) {
|
3098 | 3085 | bb.view[i++] = b;
|
|
3134 | 3121 | * @expose
|
3135 | 3122 | */
|
3136 | 3123 | ByteBufferPrototype.toBinary = function(begin, end) {
|
3137 |
| - begin = typeof begin === 'undefined' ? this.offset : begin; |
3138 |
| - end = typeof end === 'undefined' ? this.limit : end; |
3139 |
| - if (!this.noAssert) { |
3140 |
| - if (typeof begin !== 'number' || begin % 1 !== 0) |
3141 |
| - throw TypeError("Illegal begin: Not an integer"); |
3142 |
| - begin >>>= 0; |
3143 |
| - if (typeof end !== 'number' || end % 1 !== 0) |
3144 |
| - throw TypeError("Illegal end: Not an integer"); |
3145 |
| - end >>>= 0; |
3146 |
| - if (begin < 0 || begin > end || end > this.buffer.byteLength) |
3147 |
| - throw RangeError("Illegal range: 0 <= "+begin+" <= "+end+" <= "+this.buffer.byteLength); |
3148 |
| - } |
| 3124 | + if (typeof begin === 'undefined') |
| 3125 | + begin = this.offset; |
| 3126 | + if (typeof end === 'undefined') |
| 3127 | + end = this.limit; |
| 3128 | + begin |= 0; end |= 0; |
| 3129 | + if (begin < 0 || end > this.capacity() || begin > end) |
| 3130 | + throw RangeError("begin, end"); |
3149 | 3131 | if (begin === end)
|
3150 | 3132 | return "";
|
3151 |
| - var cc = [], pt = []; |
| 3133 | + var chars = [], |
| 3134 | + parts = []; |
3152 | 3135 | while (begin < end) {
|
3153 |
| - cc.push(this.view[begin++]); |
3154 |
| - if (cc.length >= 1024) |
3155 |
| - pt.push(String.fromCharCode.apply(String, cc)), |
3156 |
| - cc = []; |
| 3136 | + chars.push(this.view[begin++]); |
| 3137 | + if (chars.length >= 1024) |
| 3138 | + parts.push(String.fromCharCode.apply(String, chars)), |
| 3139 | + chars = []; |
3157 | 3140 | }
|
3158 |
| - return pt.join('') + String.fromCharCode.apply(String, cc); |
| 3141 | + return parts.join('') + String.fromCharCode.apply(String, chars); |
3159 | 3142 | };
|
3160 | 3143 |
|
3161 | 3144 | /**
|
3162 | 3145 | * Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.
|
3163 | 3146 | * @param {string} str String to decode
|
3164 | 3147 | * @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
|
3165 | 3148 | * {@link ByteBuffer.DEFAULT_ENDIAN}.
|
3166 |
| - * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to |
3167 |
| - * {@link ByteBuffer.DEFAULT_NOASSERT}. |
3168 | 3149 | * @returns {!ByteBuffer} ByteBuffer
|
3169 | 3150 | * @expose
|
3170 | 3151 | */
|
3171 |
| - ByteBuffer.fromBinary = function(str, littleEndian, noAssert) { |
3172 |
| - if (!noAssert) { |
3173 |
| - if (typeof str !== 'string') |
3174 |
| - throw TypeError("Illegal str: Not a string"); |
3175 |
| - } |
3176 |
| - var i = 0, k = str.length, charCode, |
3177 |
| - bb = new ByteBuffer(k, littleEndian, noAssert); |
| 3152 | + ByteBuffer.fromBinary = function(str, littleEndian) { |
| 3153 | + if (typeof str !== 'string') |
| 3154 | + throw TypeError("str"); |
| 3155 | + var i = 0, |
| 3156 | + k = str.length, |
| 3157 | + charCode, |
| 3158 | + bb = new ByteBuffer(k, littleEndian); |
3178 | 3159 | while (i<k) {
|
3179 | 3160 | charCode = str.charCodeAt(i);
|
3180 |
| - if (!noAssert && charCode > 255) |
3181 |
| - throw RangeError("Illegal charCode at "+i+": 0 <= "+charCode+" <= 255"); |
| 3161 | + if (charCode > 0xff) |
| 3162 | + throw RangeError("illegal char code: "+charCode); |
3182 | 3163 | bb.view[i++] = charCode;
|
3183 | 3164 | }
|
3184 | 3165 | bb.limit = k;
|
|
0 commit comments