Skip to content

Commit

Permalink
Accept an array in ByteBuffer.wrap, fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jun 16, 2014
1 parent 885186b commit ff20771
Show file tree
Hide file tree
Showing 14 changed files with 17,134 additions and 81 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bytebuffer",
"version": "3.0.0-RC2",
"version": "3.0.0",
"author": "Daniel Wirtz <[email protected]>",
"description": "A full-featured ByteBuffer implementation using typed arrays.",
"main": "ByteBuffer.js",
Expand Down
11 changes: 9 additions & 2 deletions dist/ByteBufferAB.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Constructs a new ByteBuffer.
* @class The swiss army knife for binary data in JavaScript.
* @exports ByteBuffer
* @constructor
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
* {@link ByteBuffer.DEFAULT_ENDIAN}.
Expand Down Expand Up @@ -117,7 +118,7 @@
* @const
* @expose
*/
ByteBuffer.VERSION = "3.0.0-RC2";
ByteBuffer.VERSION = "3.0.0";

/**
* Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
Expand Down Expand Up @@ -308,7 +309,13 @@
bb.limit = buffer.byteLength;
bb.view = buffer.byteLength > 0 ? new DataView(buffer) : null;
}
} else throw(new TypeError("Illegal buffer"));
} else if (Object.prototype.toString.call(buffer) === "[object Array]") { // Create from octets
bb = new ByteBuffer(buffer.length, littleEndian, noAssert);
bb.limit = buffer.length;
for (i=0; i<buffer.length; ++i)
bb.view.setUint8(i, buffer[i]);
} else
throw(new TypeError("Illegal buffer")); // Otherwise fail
return bb;
};

Expand Down
118 changes: 59 additions & 59 deletions dist/ByteBufferAB.min.js

Large diffs are not rendered by default.

Binary file modified dist/ByteBufferAB.min.js.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions dist/ByteBufferAB.min.map

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions dist/ByteBufferNB.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = (function() {
* Constructs a new ByteBuffer.
* @class The swiss army knife for binary data in JavaScript.
* @exports ByteBuffer
* @constructor
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian Whether to use little or big endian byte order. Defaults to
* {@link ByteBuffer.DEFAULT_ENDIAN}.
Expand Down Expand Up @@ -109,7 +110,7 @@ module.exports = (function() {
* @const
* @expose
*/
ByteBuffer.VERSION = "3.0.0-RC2";
ByteBuffer.VERSION = "3.0.0";

/**
* Little endian constant that can be used instead of its boolean value. Evaluates to `true`.
Expand Down Expand Up @@ -305,8 +306,11 @@ module.exports = (function() {
}
}
buffer = b;
} else if (!(buffer instanceof Buffer))
throw(new TypeError("Illegal buffer"));
} else if (!(buffer instanceof Buffer)) { // Create from octets if it is an error, otherwise fail
if (Object.prototype.toString.call(buffer) !== "[object Array]")
throw(new TypeError("Illegal buffer"));
buffer = new Buffer(buffer);
}
bb = new ByteBuffer(0, littleEndian, noAssert);
if (buffer.length > 0) { // Avoid references to more than one EMPTY_BUFFER
bb.buffer = buffer;
Expand Down Expand Up @@ -3109,8 +3113,14 @@ module.exports = (function() {

/**
* node-memcpy. This is an optional binding dependency and may not be present.
* @type {?function(!(Buffer|ArrayBuffer|Uint8Array), number|!(Buffer|ArrayBuffer), (!(Buffer|ArrayBuffer|Uint8Array)|number)=, number=, number=):number}
* @see https://npmjs.org/package/memcpy
* @function
* @param {!(Buffer|ArrayBuffer|Uint8Array)} target Destination
* @param {number|!(Buffer|ArrayBuffer)} targetStart Destination start, defaults to 0.
* @param {(!(Buffer|ArrayBuffer|Uint8Array)|number)=} source Source
* @param {number=} sourceStart Source start, defaults to 0.
* @param {number=} sourceEnd Source end, defaults to capacity.
* @returns {number} Number of bytes copied
* @throws {Error} If any index is out of bounds
* @expose
*/
ByteBuffer.memcpy = memcpy;
Expand Down
Loading

0 comments on commit ff20771

Please sign in to comment.