|
46 | 46 | /**
|
47 | 47 | * Constructs a new ByteBuffer.
|
48 | 48 | * @exports ByteBuffer
|
49 |
| - * @class A full-features ByteBuffer implementation in JavaScript using typed arrays. |
| 49 | + * @class A full-featured ByteBuffer implementation in JavaScript using typed arrays. |
50 | 50 | * @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
|
51 | 51 | * @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
|
52 | 52 | * endian.
|
|
1165 | 1165 | */
|
1166 | 1166 | var TWO_PWR_28_DBL = TWO_PWR_14_DBL * TWO_PWR_14_DBL;
|
1167 | 1167 |
|
1168 |
| - /** |
1169 |
| - * Writes a 64bit base 128 variable-length integer as used in protobuf. |
1170 |
| - * @param {number|Long} value Value to write |
1171 |
| - * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted. |
1172 |
| - * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written. |
1173 |
| - * @throws {Error} If long support is not available |
1174 |
| - * @expose |
1175 |
| - */ |
1176 |
| - ByteBuffer.prototype.writeVarint64 = function(value, offset) { |
1177 |
| - if (!Long) { |
1178 |
| - throw(new Error("Long support is not available: See https://github.com/dcodeIO/ByteBuffer.js#on-long-int64-support for details")) |
1179 |
| - } |
1180 |
| - var advance = typeof offset === 'undefined'; |
1181 |
| - offset = typeof offset !== 'undefined' ? offset : this.offset; |
1182 |
| - if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, false); |
1183 |
| - |
1184 |
| - var part0 = value.toInt() >>> 0, |
1185 |
| - part1 = value.shiftRightUnsigned(28).toInt() >>> 0, |
1186 |
| - part2 = value.shiftRightUnsigned(56).toInt() >>> 0, |
1187 |
| - size = ByteBuffer.calculateVarint64(value); |
1188 |
| - |
1189 |
| - this.ensureCapacity(offset+size); |
1190 |
| - var dst = this.view; |
1191 |
| - switch (size) { |
1192 |
| - case 10: dst.setUint8(offset+9, (part2 >>> 7) | 0x80); |
1193 |
| - case 9 : dst.setUint8(offset+8, (part2 ) | 0x80); |
1194 |
| - case 8 : dst.setUint8(offset+7, (part1 >>> 21) | 0x80); |
1195 |
| - case 7 : dst.setUint8(offset+6, (part1 >>> 14) | 0x80); |
1196 |
| - case 6 : dst.setUint8(offset+5, (part1 >>> 7) | 0x80); |
1197 |
| - case 5 : dst.setUint8(offset+4, (part1 ) | 0x80); |
1198 |
| - case 4 : dst.setUint8(offset+3, (part0 >>> 21) | 0x80); |
1199 |
| - case 3 : dst.setUint8(offset+2, (part0 >>> 14) | 0x80); |
1200 |
| - case 2 : dst.setUint8(offset+1, (part0 >>> 7) | 0x80); |
1201 |
| - case 1 : dst.setUint8(offset+0, (part0 ) | 0x80); |
1202 |
| - } |
1203 |
| - dst.setUint8(offset+size-1, dst.getUint8(offset+size-1) & 0x7F); |
1204 |
| - if (advance) { |
1205 |
| - this.offset += size; |
1206 |
| - return this; |
1207 |
| - } else { |
1208 |
| - return size; |
1209 |
| - } |
1210 |
| - }; |
1211 |
| - |
1212 | 1168 | // Available with Long.js only
|
1213 | 1169 | if (Long) {
|
| 1170 | + |
| 1171 | + /** |
| 1172 | + * Writes a 64bit base 128 variable-length integer as used in protobuf. |
| 1173 | + * @param {number|Long} value Value to write |
| 1174 | + * @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted. |
| 1175 | + * @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written. |
| 1176 | + * @expose |
| 1177 | + */ |
| 1178 | + ByteBuffer.prototype.writeVarint64 = function(value, offset) { |
| 1179 | + var advance = typeof offset === 'undefined'; |
| 1180 | + offset = typeof offset !== 'undefined' ? offset : this.offset; |
| 1181 | + if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, false); |
| 1182 | + |
| 1183 | + var part0 = value.toInt() >>> 0, |
| 1184 | + part1 = value.shiftRightUnsigned(28).toInt() >>> 0, |
| 1185 | + part2 = value.shiftRightUnsigned(56).toInt() >>> 0, |
| 1186 | + size = ByteBuffer.calculateVarint64(value); |
| 1187 | + |
| 1188 | + this.ensureCapacity(offset+size); |
| 1189 | + var dst = this.view; |
| 1190 | + switch (size) { |
| 1191 | + case 10: dst.setUint8(offset+9, (part2 >>> 7) | 0x80); |
| 1192 | + case 9 : dst.setUint8(offset+8, (part2 ) | 0x80); |
| 1193 | + case 8 : dst.setUint8(offset+7, (part1 >>> 21) | 0x80); |
| 1194 | + case 7 : dst.setUint8(offset+6, (part1 >>> 14) | 0x80); |
| 1195 | + case 6 : dst.setUint8(offset+5, (part1 >>> 7) | 0x80); |
| 1196 | + case 5 : dst.setUint8(offset+4, (part1 ) | 0x80); |
| 1197 | + case 4 : dst.setUint8(offset+3, (part0 >>> 21) | 0x80); |
| 1198 | + case 3 : dst.setUint8(offset+2, (part0 >>> 14) | 0x80); |
| 1199 | + case 2 : dst.setUint8(offset+1, (part0 >>> 7) | 0x80); |
| 1200 | + case 1 : dst.setUint8(offset+0, (part0 ) | 0x80); |
| 1201 | + } |
| 1202 | + dst.setUint8(offset+size-1, dst.getUint8(offset+size-1) & 0x7F); |
| 1203 | + if (advance) { |
| 1204 | + this.offset += size; |
| 1205 | + return this; |
| 1206 | + } else { |
| 1207 | + return size; |
| 1208 | + } |
| 1209 | + }; |
1214 | 1210 |
|
1215 | 1211 | /**
|
1216 | 1212 | * Reads a 32bit base 128 variable-length integer as used in protobuf. Requires Long.js.
|
|
2157 | 2153 | }
|
2158 | 2154 | return forceCopy && !copied ? b.copy().array : b.array;
|
2159 | 2155 | };
|
2160 |
| - |
2161 |
| - /** |
2162 |
| - * Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will transparently |
2163 |
| - * {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not |
2164 |
| - * a reference). |
2165 |
| - * @returns {?Buffer} Compacted node Buffer or null if already destroyed |
2166 |
| - * @throws {Error} If not running inside of node |
2167 |
| - * @expose |
2168 |
| - */ |
2169 |
| - ByteBuffer.prototype.toBuffer = function() { |
2170 |
| - if (Buffer) { |
| 2156 | + |
| 2157 | + // Available with node.js only |
| 2158 | + if (Buffer) { |
| 2159 | + |
| 2160 | + /** |
| 2161 | + * Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will transparently |
| 2162 | + * {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not |
| 2163 | + * a reference). |
| 2164 | + * @returns {?Buffer} Compacted node Buffer or null if already destroyed |
| 2165 | + * @expose |
| 2166 | + */ |
| 2167 | + ByteBuffer.prototype.toBuffer = function() { |
2171 | 2168 | if (this.array === null) return null;
|
2172 | 2169 | var offset = this.offset, length = this.length;
|
2173 | 2170 | if (offset > length) {
|
2174 | 2171 | var temp = offset;
|
2175 | 2172 | offset = length;
|
2176 | 2173 | length = temp;
|
2177 | 2174 | }
|
2178 |
| - var srcView = new Uint8Array(this.array); |
2179 |
| - return new Buffer(srcView.subarray(offset, length)); |
2180 |
| - } |
2181 |
| - throw(new Error("Conversion to Buffer is available under node.js only")); |
2182 |
| - }; |
| 2175 | + return new Buffer(new Uint8Array(this.array).subarray(offset, length)); |
| 2176 | + }; |
| 2177 | + } |
2183 | 2178 |
|
2184 | 2179 | return ByteBuffer;
|
2185 | 2180 | }
|
|
0 commit comments