Permalink
Browse files

Added ability to create empty buffer (and operate on it) by passing c…

…ount of bytes to wrapBuffer.
  • Loading branch information...
1 parent 70a7869 commit ef6532db5e95ed6051d80f0115a45a0b8a4eb1ef @RReverser RReverser committed Mar 16, 2013
Showing with 22 additions and 11 deletions.
  1. +4 −3 README.md
  2. +18 −8 src/jdataview.js
View
7 README.md
@@ -27,7 +27,7 @@ See the [Typed Array Specification](http://www.khronos.org/registry/typedarray/s
Constructor
-----------------
* new **jDataView**(buffer, offset, length, littleEndian = false)
- * buffer can be either a binary String or any Array-like byte storage (Array, Uint8Array, Arguments, jQuery(Array), ...)
+ * buffer can be either a binary String, any Array-like byte storage (Array, Uint8Array, Arguments, jQuery(Array), ...) or count of bytes if you want to operate on new empty buffer.
* littleEndian = false (Big Endian mode) is a default value for the view
Specification API
@@ -95,7 +95,7 @@ Addition of Char, String and Bytes utilities.
Addition of wrapBuffer and createBuffer, utilities to easily create buffers with the latest available storage type (Node.js Buffer, ArrayBuffer or simple Array).
-* **wrapBuffer**(string_or_bytes)
+* **wrapBuffer**(string_or_bytes_or_byteCount)
* **createBuffer**(byte1, byte2, ...)
Example
@@ -151,11 +151,12 @@ $.get(
Changelog
========
-* **March 15 2013**:
+* **March 16 2013**:
* [RReverser](https://github.com/rreverser) added support for setters in all supported implementations!
* Performance improvements changing lower level constructs and type of inner buffers
* Addition of [gs]etBytes, write*, wrapBuffer and slice helpers
* Added support for any Array-like byte storage as input (Array, Uint8Array, Arguments, jQuery(Array), ...)
+ * Added ability to create empty buffer (and operate on it) by passing count of bytes to wrapBuffer.
* **June 30 2012**: Thanks to [Mithgol](https://github.com/Mithgol) for the changes!
* Changed default to big endian from little endian to be compatible with DataView specification
* Dropped support for NodeJS < 0.5.5, it was buggy anyway
View
26 src/jdataview.js
@@ -328,22 +328,32 @@ var jDataView = function (buffer, byteOffset, byteLength, littleEndian) {
// mostly internal function for wrapping any supported input (String or Array-like) to best suitable buffer format
jDataView.wrapBuffer = function (buffer) {
- if (typeof buffer === 'string') {
- buffer = Array.prototype.map.call(buffer, function (char) {
- return char.charCodeAt(0) & 0xff;
- });
+ switch (typeof buffer) {
+ case 'string':
+ buffer = Array.prototype.map.call(buffer, function (char) {
+ return char.charCodeAt(0) & 0xff;
+ });
+ break;
+
+ case 'number':
+ buffer = {length: buffer};
+ break;
}
if ('length' in buffer && !((compatibility.NodeBuffer && buffer instanceof Buffer) || (compatibility.ArrayBuffer && buffer instanceof ArrayBuffer))) {
if (compatibility.NodeBuffer) {
- return new Buffer(buffer);
+ buffer = new Buffer(buffer);
} else
if (compatibility.ArrayBuffer) {
var bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
- return bytes.buffer;
+ buffer = bytes.buffer;
} else {
if (!(buffer instanceof Array)) {
- return Array.prototype.slice.call(buffer);
+ buffer = Array.prototype.slice.call(buffer);
+ }
+ // as simple Array may contain non-byte values (incl. undefined)
+ for (var i = 0, length = buffer.length; i < length; i++) {
+ buffer[i] &= 0xff;
}
}
}
@@ -453,7 +463,7 @@ jDataView.prototype = {
if (this._isNodeBuffer) {
new Buffer(bytes).copy(this.buffer, byteOffset);
} else {
- for (var i = 0; i < bytes.length; i++) {
+ for (var i = 0; i < length; i++) {
this.buffer[byteOffset + i] = bytes[i];
}
}

0 comments on commit ef6532d

Please sign in to comment.