|
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. |
| 53 | + * @param {boolean=} sparse If set to `true`, a ByteBuffer with array=view=null will be created which have to be |
| 54 | + * set manually afterwards. Defaults to `false`. |
53 | 55 | * @expose |
54 | 56 | */ |
55 | | - var ByteBuffer = function(capacity, littleEndian) { |
56 | | - |
| 57 | + var ByteBuffer = function(capacity, littleEndian, sparse) { |
57 | 58 | capacity = typeof capacity !== 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY; |
58 | 59 | if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY; |
59 | 60 |
|
|
62 | 63 | * @type {?ArrayBuffer} |
63 | 64 | * @expose |
64 | 65 | */ |
65 | | - this.array = arguments.length == 3 && arguments[2] === true ? null : new ArrayBuffer(capacity); |
| 66 | + this.array = sparse ? null : new ArrayBuffer(capacity); |
66 | 67 |
|
67 | 68 | /** |
68 | 69 | * DataView to mess with the ArrayBuffer. |
69 | 70 | * @type {?DataView} |
70 | 71 | * @expose |
71 | 72 | */ |
72 | | - this.view = this.array != null ? new DataView(this.array) : null; |
| 73 | + this.view = sparse ? null : new DataView(this.array); |
73 | 74 |
|
74 | 75 | /** |
75 | 76 | * Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset |
|
110 | 111 | * @const |
111 | 112 | * @expose |
112 | 113 | */ |
113 | | - ByteBuffer.VERSION = "2.3.0"; |
| 114 | + ByteBuffer.VERSION = "2.3.1"; |
114 | 115 |
|
115 | 116 | /** |
116 | 117 | * Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required. |
|
243 | 244 | if (!(buffer instanceof ArrayBuffer)) { |
244 | 245 | throw(new Error("Cannot wrap buffer of type "+typeof(buffer)+", "+buffer.constructor.name)); |
245 | 246 | } |
246 | | - b = new ByteBuffer(0, littleEndian, /* shadow copy */ true); |
| 247 | + b = new ByteBuffer(0, littleEndian, true); |
247 | 248 | b.array = buffer; |
248 | 249 | b.view = b.array.byteLength > 0 ? new DataView(b.array) : null; |
249 | 250 | b.offset = 0; |
|
1781 | 1782 | }; |
1782 | 1783 |
|
1783 | 1784 | /** |
1784 | | - * Decodes a binary string to a ByteBuffer.A binary string in this case is a string composed of 8bit values |
| 1785 | + * Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values |
1785 | 1786 | * as characters with a char code between 0 and 255 inclusive. |
1786 | 1787 | * @param {string} str Binary string |
1787 | 1788 | * @param {boolean=} littleEndian `true` to use little endian byte order, defaults to `false` for big endian. |
|
1801 | 1802 | if ((val = str.charCodeAt(i)) > 255) throw(new Error("Illegal argument: Not a binary string (char code "+val+")")); |
1802 | 1803 | view.setUint8(i, val); |
1803 | 1804 | } |
1804 | | - return ByteBuffer.wrap(dst, littleEndian); |
| 1805 | + var bb = new ByteBuffer(k, littleEndian, true); |
| 1806 | + bb.array = dst; |
| 1807 | + bb.view = view; |
| 1808 | + bb.length = k; |
| 1809 | + return bb; |
1805 | 1810 | }; |
1806 | 1811 |
|
1807 | 1812 | /** |
|
0 commit comments