Skip to content

Commit 75b8700

Browse files
author
Daniel Wirtz
committed
ByteBuffer#toBuffer() for node.js
1 parent 5fc328f commit 75b8700

File tree

11 files changed

+214
-8
lines changed

11 files changed

+214
-8
lines changed

ByteBuffer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,30 @@
19301930
}
19311931
return forceCopy && !copied ? b.copy().array : b.array;
19321932
};
1933+
1934+
/**
1935+
* Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will implicitly
1936+
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not
1937+
* a reference).
1938+
* @returns {Buffer} Compacted Buffer
1939+
* @throws {Error} If not running inside of node
1940+
* @expose
1941+
*/
1942+
ByteBuffer.prototype.toBuffer = function() {
1943+
try {
1944+
require("buffer"); // Just testing
1945+
var offset = this.offset, length = this.length;
1946+
if (offset > length) {
1947+
var temp = offset;
1948+
offset = length;
1949+
length = temp;
1950+
}
1951+
return new Buffer(new Uint8Array(this.array).subarray(offset, length));
1952+
} catch (e) {
1953+
console.trace(e);
1954+
throw("ByteBuffer#toBuffer() is available with node.js only");
1955+
}
1956+
};
19331957

19341958
/**
19351959
* Extends the ByteBuffer prototype with additional methods.

ByteBuffer.min.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,29 @@
18281828
}
18291829
return forceCopy && !copied ? b.copy().array : b.array;
18301830
};
1831+
1832+
/**
1833+
* Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will implicitly
1834+
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not
1835+
* a reference).
1836+
* @returns {Buffer} Compacted Buffer
1837+
* @throws {Error} If not running inside of node
1838+
*/
1839+
ByteBuffer.prototype.toBuffer = function() {
1840+
try {
1841+
require("buffer"); // Just testing
1842+
var offset = this.offset, length = this.length;
1843+
if (offset > length) {
1844+
var temp = offset;
1845+
offset = length;
1846+
length = temp;
1847+
}
1848+
return new Buffer(new Uint8Array(this.array).subarray(offset, length));
1849+
} catch (e) {
1850+
console.trace(e);
1851+
throw("ByteBuffer#toBuffer() is available with node.js only");
1852+
}
1853+
};
18311854

18321855
/**
18331856
* Extends the ByteBuffer prototype with additional methods.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ ByteBuffer
2121
* Compacting of the backing buffer (`ByteBuffer#compact()`)
2222
* Conversion to ArrayBuffer (`ByteBuffer#toArrayBuffer([forceCopy])`) (i.e. to send data over the wire, e.g. a WebSocket
2323
with `binaryType="arraybuffer"`)
24+
* Conversion to Buffer (`ByteBuffer#toBuffer()`) if running inside of node.js
2425
* Reversing (`ByteBuffer#reverse()`), appending (`ByteBuffer#append(src[, offset])`) and prepending
2526
(`ByteBuffer#prepend(src[, offset])`) of other ByteBuffers with implicit capacity management
2627
* Explicit destruction (`ByteBuffer#destroy()`)

docs/ByteBuffer.html

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9775,6 +9775,110 @@ <h5>Returns:</h5>
97759775

97769776

97779777

9778+
</dd>
9779+
9780+
9781+
9782+
<dt>
9783+
<h4 class="name" id="toBuffer"><span class="type-signature"></span>toBuffer<span class="signature">()</span><span class="type-signature"> &rarr; {Buffer}</span></h4>
9784+
9785+
9786+
</dt>
9787+
<dd>
9788+
9789+
9790+
<div class="description">
9791+
<p>Returns a node Buffer compacted to contain this ByteBuffer&#39;s actual contents. Will implicitly
9792+
<a href="ByteBuffer.html#flip">ByteBuffer#flip</a> the ByteBuffer if its offset is larger than its length. Will also copy all data (not
9793+
a reference).</p>
9794+
</div>
9795+
9796+
9797+
9798+
9799+
9800+
9801+
9802+
9803+
9804+
<dl class="details">
9805+
9806+
9807+
9808+
9809+
9810+
9811+
9812+
9813+
9814+
9815+
9816+
9817+
9818+
9819+
9820+
9821+
9822+
9823+
9824+
9825+
9826+
9827+
9828+
</dl>
9829+
9830+
9831+
9832+
9833+
9834+
<h5>Throws:</h5>
9835+
9836+
9837+
<div class="param-desc">
9838+
If not running inside of node
9839+
</div>
9840+
9841+
9842+
9843+
<dl>
9844+
<dt>
9845+
Type
9846+
</dt>
9847+
<dd>
9848+
9849+
<span class="param-type">Error</span>
9850+
9851+
9852+
</dd>
9853+
</dl>
9854+
9855+
9856+
9857+
9858+
<h5>Returns:</h5>
9859+
9860+
9861+
<div class="param-desc">
9862+
<p>Compacted Buffer</p>
9863+
</div>
9864+
9865+
9866+
9867+
<dl>
9868+
<dt>
9869+
Type
9870+
</dt>
9871+
<dd>
9872+
9873+
<span class="param-type">Buffer</span>
9874+
9875+
9876+
</dd>
9877+
</dl>
9878+
9879+
9880+
9881+
97789882
</dd>
97799883

97809884

externs/ByteBuffer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,14 +662,14 @@ ByteBuffer.prototype.readCString = function(offset) {};
662662
/**
663663
* @param {*} data
664664
* @param {number=} offset
665-
* @param {function=} stringify
665+
* @param {(function(*):string)=} stringify
666666
* @return {!ByteBuffer|number}
667667
*/
668668
ByteBuffer.prototype.writeJSON = function(data, offset, stringify) {};
669669

670670
/**
671671
* @param {number=} offset
672-
* @param {function=} parse
672+
* @param {(function(string):*)=} parse
673673
* @return {*|!{data: *, length: number}}
674674
* @throws {Error}
675675
*/

externs/minimal-node-env.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {string} moduleName
3+
* @returns {?}
4+
*/
5+
var require = function(moduleName) {};
6+
7+
/**
8+
* @param {number} size
9+
* @constructor
10+
*/
11+
var Buffer = function(size) {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"author": "Daniel Wirtz <[email protected]>",
55
"description": "ByteBuffer.js: A Java-like, Netty-inspired ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

src/ByteBuffer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,30 @@
19301930
}
19311931
return forceCopy && !copied ? b.copy().array : b.array;
19321932
};
1933+
1934+
/**
1935+
* Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will implicitly
1936+
* {@link ByteBuffer#flip} the ByteBuffer if its offset is larger than its length. Will also copy all data (not
1937+
* a reference).
1938+
* @returns {Buffer} Compacted Buffer
1939+
* @throws {Error} If not running inside of node
1940+
* @expose
1941+
*/
1942+
ByteBuffer.prototype.toBuffer = function() {
1943+
try {
1944+
require("buffer"); // Just testing
1945+
var offset = this.offset, length = this.length;
1946+
if (offset > length) {
1947+
var temp = offset;
1948+
offset = length;
1949+
length = temp;
1950+
}
1951+
return new Buffer(new Uint8Array(this.array).subarray(offset, length));
1952+
} catch (e) {
1953+
console.trace(e);
1954+
throw("ByteBuffer#toBuffer() is available with node.js only");
1955+
}
1956+
};
19331957

19341958
/**
19351959
* Extends the ByteBuffer prototype with additional methods.

0 commit comments

Comments
 (0)