Skip to content

Commit c1c9ed6

Browse files
committed
ByteBuffer.isByteBuffer, see protobufjs/protobuf.js#96
1 parent f676ba4 commit c1c9ed6

File tree

9 files changed

+510
-83
lines changed

9 files changed

+510
-83
lines changed

ByteBuffer.js

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
* @const
111111
* @expose
112112
*/
113-
ByteBuffer.VERSION = "2.1.1";
113+
ByteBuffer.VERSION = "2.2.0";
114114

115115
/**
116116
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -145,6 +145,26 @@
145145
*/
146146
ByteBuffer.Long = Long || null;
147147

148+
/**
149+
* Tests if the specified type is a ByteBuffer or ByteBuffer-like.
150+
* @param {*} bb ByteBuffer to test
151+
* @returns {boolean} true if it is a ByteBuffer or ByteBuffer-like, otherwise false
152+
* @expose
153+
*/
154+
ByteBuffer.isByteBuffer = function(bb) {
155+
return bb && (
156+
(bb instanceof ByteBuffer) || (
157+
typeof bb === 'object' &&
158+
(bb.array === null || bb.array instanceof ArrayBuffer) &&
159+
(bb.view === null || bb.view instanceof DataView) &&
160+
typeof bb.offset === 'number' &&
161+
typeof bb.markedOffset === 'number' &&
162+
typeof bb.length === 'number' &&
163+
typeof bb.littleEndian === 'boolean'
164+
)
165+
);
166+
};
167+
148168
/**
149169
* Allocates a new ByteBuffer.
150170
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
@@ -209,8 +229,8 @@
209229
throw(new Error("Cannot wrap null or non-object"));
210230
}
211231
// Wrap ByteBuffer by cloning (preserve offsets)
212-
if (buffer instanceof ByteBuffer) {
213-
return buffer.clone();
232+
if (ByteBuffer.isByteBuffer(buffer)) {
233+
return ByteBuffer.prototype.clone.call(buffer); // Also makes ByteBuffer-like a ByteBuffer
214234
}
215235
// Wrap any object that is or contains an ArrayBuffer
216236
if (!!buffer["array"]) {
@@ -1612,16 +1632,18 @@
16121632

16131633
/**
16141634
* Encodes a ByteBuffer's contents to a base64 string.
1615-
* @param {!ByteBuffer} bb ByteBuffer
1635+
* @param {!ByteBuffer} bb ByteBuffer to encode. Will be cloned and flipped if length < offset.
16161636
* @returns {string} Base64 encoded string
1617-
* @throws {Error} If the argument is invalid
1637+
* @throws {Error} If the argument is not a valid ByteBuffer
16181638
* @expose
16191639
*/
16201640
ByteBuffer.encode64 = function(bb) {
16211641
// ref: http://phpjs.org/functions/base64_encode/
1622-
if (!bb || !(bb instanceof ByteBuffer) || bb.length < bb.offset) {
1623-
throw(new Error("Illegal argument: Not a ByteBuffer or offset out of bounds"));
1624-
}
1642+
if (!(bb instanceof ByteBuffer)) {
1643+
bb = ByteBuffer.wrap(bb);
1644+
} else if (bb.length < bb.offset) {
1645+
bb = bb.clone().flip();
1646+
}
16251647
var o1, o2, o3, h1, h2, h3, h4, bits, i = bb.offset,
16261648
oi = 0,
16271649
out = [];
@@ -1682,6 +1704,29 @@
16821704
return out.flip();
16831705
};
16841706

1707+
/**
1708+
* Encodes a ByteBuffer to a hex encoded string.
1709+
* @param {!ByteBuffer} bb ByteBuffer to encode. Will be cloned and flipped if length < offset.
1710+
* @returns {string} Hex encoded string
1711+
* @throws {Error} If the argument is not a valid ByteBuffer
1712+
* @expose
1713+
*/
1714+
ByteBuffer.encodeHex = function(bb) {
1715+
if (!(bb instanceof ByteBuffer)) {
1716+
bb = ByteBuffer.wrap(bb);
1717+
} else if (bb.length < bb.offset) {
1718+
bb = bb.clone().flip();
1719+
}
1720+
if (bb.array === null) return "";
1721+
var val, out = [];
1722+
for (var i=bb.offset, k=bb.length; i<k; i++) {
1723+
val = bb.view.getUint8(i).toString(16).toUpperCase();
1724+
if (val.length < 2) val = "0"+val;
1725+
out.push(val);
1726+
}
1727+
return out.join('');
1728+
};
1729+
16851730
/**
16861731
* Decodes a hex encoded string to a ByteBuffer.
16871732
* @param {string} str Hex encoded string
@@ -1825,8 +1870,8 @@
18251870
ByteBuffer.prototype.readLString = function(offset) {
18261871
var advance = typeof offset === 'undefined';
18271872
offset = typeof offset !== 'undefined' ? offset : this.offset;
1828-
var lenDec = ByteBuffer.decodeUTF8Char(this, offset);
1829-
var dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
1873+
var lenDec = ByteBuffer.decodeUTF8Char(this, offset),
1874+
dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
18301875
if (advance) {
18311876
this.offset += lenDec["length"]+dec["length"];
18321877
return dec["string"];

ByteBuffer.min.js

Lines changed: 48 additions & 47 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: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* @type {string}
103103
* @const
104104
*/
105-
ByteBuffer.VERSION = "2.1.1";
105+
ByteBuffer.VERSION = "2.2.0";
106106

107107
/**
108108
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -133,6 +133,25 @@
133133
*/
134134
ByteBuffer.Long = Long || null;
135135

136+
/**
137+
* Tests if the specified type is a ByteBuffer or ByteBuffer-like.
138+
* @param {*} bb ByteBuffer to test
139+
* @returns {boolean} true if it is a ByteBuffer or ByteBuffer-like, otherwise false
140+
*/
141+
ByteBuffer.isByteBuffer = function(bb) {
142+
return bb && (
143+
(bb instanceof ByteBuffer) || (
144+
typeof bb === 'object' &&
145+
(bb.array === null || bb.array instanceof ArrayBuffer) &&
146+
(bb.view === null || bb.view instanceof DataView) &&
147+
typeof bb.offset === 'number' &&
148+
typeof bb.markedOffset === 'number' &&
149+
typeof bb.length === 'number' &&
150+
typeof bb.littleEndian === 'boolean'
151+
)
152+
);
153+
};
154+
136155
/**
137156
* Allocates a new ByteBuffer.
138157
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
@@ -195,8 +214,8 @@
195214
throw(new Error("Cannot wrap null or non-object"));
196215
}
197216
// Wrap ByteBuffer by cloning (preserve offsets)
198-
if (buffer instanceof ByteBuffer) {
199-
return buffer.clone();
217+
if (ByteBuffer.isByteBuffer(buffer)) {
218+
return ByteBuffer.prototype.clone.call(buffer); // Also makes ByteBuffer-like a ByteBuffer
200219
}
201220
// Wrap any object that is or contains an ArrayBuffer
202221
if (!!buffer["array"]) {
@@ -1526,15 +1545,17 @@
15261545

15271546
/**
15281547
* Encodes a ByteBuffer's contents to a base64 string.
1529-
* @param {!ByteBuffer} bb ByteBuffer
1548+
* @param {!ByteBuffer} bb ByteBuffer to encode. Will be cloned and flipped if length < offset.
15301549
* @returns {string} Base64 encoded string
1531-
* @throws {Error} If the argument is invalid
1550+
* @throws {Error} If the argument is not a valid ByteBuffer
15321551
*/
15331552
ByteBuffer.encode64 = function(bb) {
15341553
// ref: http://phpjs.org/functions/base64_encode/
1535-
if (!bb || !(bb instanceof ByteBuffer) || bb.length < bb.offset) {
1536-
throw(new Error("Illegal argument: Not a ByteBuffer or offset out of bounds"));
1537-
}
1554+
if (!(bb instanceof ByteBuffer)) {
1555+
bb = ByteBuffer.wrap(bb);
1556+
} else if (bb.length < bb.offset) {
1557+
bb = bb.clone().flip();
1558+
}
15381559
var o1, o2, o3, h1, h2, h3, h4, bits, i = bb.offset,
15391560
oi = 0,
15401561
out = [];
@@ -1594,6 +1615,28 @@
15941615
return out.flip();
15951616
};
15961617

1618+
/**
1619+
* Encodes a ByteBuffer to a hex encoded string.
1620+
* @param {!ByteBuffer} bb ByteBuffer to encode. Will be cloned and flipped if length < offset.
1621+
* @returns {string} Hex encoded string
1622+
* @throws {Error} If the argument is not a valid ByteBuffer
1623+
*/
1624+
ByteBuffer.encodeHex = function(bb) {
1625+
if (!(bb instanceof ByteBuffer)) {
1626+
bb = ByteBuffer.wrap(bb);
1627+
} else if (bb.length < bb.offset) {
1628+
bb = bb.clone().flip();
1629+
}
1630+
if (bb.array === null) return "";
1631+
var val, out = [];
1632+
for (var i=bb.offset, k=bb.length; i<k; i++) {
1633+
val = bb.view.getUint8(i).toString(16).toUpperCase();
1634+
if (val.length < 2) val = "0"+val;
1635+
out.push(val);
1636+
}
1637+
return out.join('');
1638+
};
1639+
15971640
/**
15981641
* Decodes a hex encoded string to a ByteBuffer.
15991642
* @param {string} str Hex encoded string
@@ -1731,8 +1774,8 @@
17311774
ByteBuffer.prototype.readLString = function(offset) {
17321775
var advance = typeof offset === 'undefined';
17331776
offset = typeof offset !== 'undefined' ? offset : this.offset;
1734-
var lenDec = ByteBuffer.decodeUTF8Char(this, offset);
1735-
var dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
1777+
var lenDec = ByteBuffer.decodeUTF8Char(this, offset),
1778+
dec = this.readUTF8String(lenDec["char"], offset+lenDec["length"]);
17361779
if (advance) {
17371780
this.offset += lenDec["length"]+dec["length"];
17381781
return dec["string"];

bower.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": "2.1.1",
3+
"version": "2.2.0",
44
"author": "Daniel Wirtz <[email protected]>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

0 commit comments

Comments
 (0)