Skip to content

Commit ca5c829

Browse files
committed
Wrap string encoding, pass LE, ...
1 parent 41a2243 commit ca5c829

File tree

9 files changed

+549
-217
lines changed

9 files changed

+549
-217
lines changed

ByteBuffer.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,27 @@
173173
* Wraps an ArrayBuffer, any object containing an ArrayBuffer or a string. Sets the created ByteBuffer's offset to 0
174174
* and its length to the wrapped objects byte length.
175175
* @param {!ArrayBuffer|!Buffer|string|{array: !ArrayBuffer}|{buffer: !ArrayBuffer}|string} buffer Anything that man be wrapped
176-
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
176+
* @param {(string|boolean)=} enc String encoding if a string is provided (hex, utf8, defaults to base64)
177+
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to false.
177178
* @returns {!ByteBuffer}
178179
* @throws {Error} If the specified object cannot be wrapped
179180
* @expose
180181
*/
181-
ByteBuffer.wrap = function(buffer, littleEndian) {
182+
ByteBuffer.wrap = function(buffer, enc, littleEndian) {
183+
if (typeof enc === 'boolean') {
184+
littleEndian = enc;
185+
enc = "utf8";
186+
}
182187
// Wrap a string
183188
if (typeof buffer === 'string') {
184-
return new ByteBuffer().writeUTF8String(buffer).flip();
189+
switch (enc) {
190+
case "hex":
191+
return ByteBuffer.decodeHex(buffer, littleEndian);
192+
case "base64":
193+
return ByteBuffer.decode64(buffer, littleEndian);
194+
default:
195+
return new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, littleEndian).writeUTF8String(buffer).flip();
196+
}
185197
}
186198
var b;
187199
// Wrap Buffer
@@ -1635,17 +1647,18 @@
16351647
/**
16361648
* Decodes a base6 encoded string to a ByteBuffer.
16371649
* @param {string} str Base64 encoded string
1650+
* @param {boolean=} littleEndian true to use little endian byte order, else false. Defaults to false.
16381651
* @returns {!ByteBuffer} ByteBuffer
16391652
* @throws {Error} If the argument is not a valid base64 encoded string
16401653
* @expose
16411654
*/
1642-
ByteBuffer.decode64 = function(str) {
1655+
ByteBuffer.decode64 = function(str, littleEndian) {
16431656
// ref: http://phpjs.org/functions/base64_decode/
16441657
if (typeof str !== 'string') {
16451658
throw(new Error("Illegal argument: Not a string"));
16461659
}
16471660
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
1648-
out = new ByteBuffer(Math.ceil(str.length / 3));
1661+
out = new ByteBuffer(Math.ceil(str.length / 3), littleEndian);
16491662
do {
16501663
h1 = b64.indexOf(str.charAt(i++));
16511664
h2 = b64.indexOf(str.charAt(i++));
@@ -1672,6 +1685,29 @@
16721685
return out.flip();
16731686
};
16741687

1688+
/**
1689+
* Decodes a hex encoded string to a ByteBuffer.
1690+
* @param {string} str Hex encoded string
1691+
* @param {boolean=} littleEndian true to use little endian byte order, else false. Defaults to false.
1692+
* @returns {!ByteBuffer} ByteBuffer
1693+
* @throws {Error} If the argument is not a valid hex encoded string
1694+
* @expose
1695+
*/
1696+
ByteBuffer.decodeHex = function(str, littleEndian) {
1697+
if (typeof str !== 'string') {
1698+
throw(new Error("Illegal argument: Not a string"));
1699+
}
1700+
if (str.length % 2 !== 0) {
1701+
throw(new Error("Illegal argument: Not a hex encoded string"));
1702+
}
1703+
var o,
1704+
out = new ByteBuffer(str.length/2, littleEndian);
1705+
for (var i=0; i<str.length; i+=2) {
1706+
out.writeUint8(parseInt(str.substring(i, i+2), 16));
1707+
}
1708+
return out.flip();
1709+
};
1710+
16751711

16761712
/**
16771713
* Writes an UTF8 string.

ByteBuffer.min.js

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

ByteBuffer.min.map

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

ByteBuffer.noexpose.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,26 @@
160160
* Wraps an ArrayBuffer, any object containing an ArrayBuffer or a string. Sets the created ByteBuffer's offset to 0
161161
* and its length to the wrapped objects byte length.
162162
* @param {!ArrayBuffer|!Buffer|string|{array: !ArrayBuffer}|{buffer: !ArrayBuffer}|string} buffer Anything that man be wrapped
163-
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to true.
163+
* @param {(string|boolean)=} enc String encoding if a string is provided (hex, utf8, defaults to base64)
164+
* @param {boolean=} littleEndian true to use little endian multi byte values, false for big endian. Defaults to false.
164165
* @returns {!ByteBuffer}
165166
* @throws {Error} If the specified object cannot be wrapped
166167
*/
167-
ByteBuffer.wrap = function(buffer, littleEndian) {
168+
ByteBuffer.wrap = function(buffer, enc, littleEndian) {
169+
if (typeof enc === 'boolean') {
170+
littleEndian = enc;
171+
enc = "utf8";
172+
}
168173
// Wrap a string
169174
if (typeof buffer === 'string') {
170-
return new ByteBuffer().writeUTF8String(buffer).flip();
175+
switch (enc) {
176+
case "hex":
177+
return ByteBuffer.decodeHex(buffer, littleEndian);
178+
case "base64":
179+
return ByteBuffer.decode64(buffer, littleEndian);
180+
default:
181+
return new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, littleEndian).writeUTF8String(buffer).flip();
182+
}
171183
}
172184
var b;
173185
// Wrap Buffer
@@ -1547,16 +1559,17 @@
15471559
/**
15481560
* Decodes a base6 encoded string to a ByteBuffer.
15491561
* @param {string} str Base64 encoded string
1562+
* @param {boolean=} littleEndian true to use little endian byte order, else false. Defaults to false.
15501563
* @returns {!ByteBuffer} ByteBuffer
15511564
* @throws {Error} If the argument is not a valid base64 encoded string
15521565
*/
1553-
ByteBuffer.decode64 = function(str) {
1566+
ByteBuffer.decode64 = function(str, littleEndian) {
15541567
// ref: http://phpjs.org/functions/base64_decode/
15551568
if (typeof str !== 'string') {
15561569
throw(new Error("Illegal argument: Not a string"));
15571570
}
15581571
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
1559-
out = new ByteBuffer(Math.ceil(str.length / 3));
1572+
out = new ByteBuffer(Math.ceil(str.length / 3), littleEndian);
15601573
do {
15611574
h1 = b64.indexOf(str.charAt(i++));
15621575
h2 = b64.indexOf(str.charAt(i++));
@@ -1583,6 +1596,28 @@
15831596
return out.flip();
15841597
};
15851598

1599+
/**
1600+
* Decodes a hex encoded string to a ByteBuffer.
1601+
* @param {string} str Hex encoded string
1602+
* @param {boolean=} littleEndian true to use little endian byte order, else false. Defaults to false.
1603+
* @returns {!ByteBuffer} ByteBuffer
1604+
* @throws {Error} If the argument is not a valid hex encoded string
1605+
*/
1606+
ByteBuffer.decodeHex = function(str, littleEndian) {
1607+
if (typeof str !== 'string') {
1608+
throw(new Error("Illegal argument: Not a string"));
1609+
}
1610+
if (str.length % 2 !== 0) {
1611+
throw(new Error("Illegal argument: Not a hex encoded string"));
1612+
}
1613+
var o,
1614+
out = new ByteBuffer(str.length/2, littleEndian);
1615+
for (var i=0; i<str.length; i+=2) {
1616+
out.writeUint8(parseInt(str.substring(i, i+2), 16));
1617+
}
1618+
return out.flip();
1619+
};
1620+
15861621

15871622
/**
15881623
* Writes an UTF8 string.

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![ByteBuffer.js - A Java-like ByteBuffer](https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png)
22
======================================
3-
Provides a full-features ByteBuffer implementation using typed arrays. It's one of the core components driving
3+
Provides a full-featured ByteBuffer implementation using typed arrays. It's one of the core components driving
44
[ProtoBuf.js](https://github.com/dcodeIO/ProtoBuf.js) and the [PSON](https://github.com/dcodeIO/PSON) reference
55
implementation.
66

@@ -13,14 +13,14 @@ What can it do?
1313
* Signed and unsigned integers (8, 16, 32, 64 bit through [Long.js](https://github.com/dcodeIO/Long.js)) with endianness support
1414
* Varints as known from protobuf including zig-zag encoding
1515
* Includes an UTF8 and Base64 en-/decoder
16-
* C-strings, Varint-prefixed strings and UTF8 length-prefixed strings
16+
* C-strings, V(arint-prefixed)-strings and UTF8 L(ength-prefixed)-strings
1717
* Rich string toolset (to hex, base64, utf8, debug, columns)
1818
* Relative and absolute zero-copy operations
1919
* Automatic resizing (always doubles)
2020
* Chaining of all operations that do not return a specific value
2121
* Slicing, appending, prepending, flip, reset, etc.
2222

23-
And much more... (see the API documentation)
23+
And much more...
2424

2525
Features
2626
--------
@@ -54,8 +54,8 @@ Optionally depends on [Long.js](https://github.com/dcodeIO/Long.js) for long (in
5454
support, you can skip the Long.js include.
5555

5656
```html
57-
<script src="//raw.github.com/dcodeIO/Long.js/master/Long.min.js"></script>
58-
<script src="//raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.min.js"></script>
57+
<script src="Long.min.js"></script>
58+
<script src="ByteBuffer.min.js"></script>
5959
```
6060

6161
```javascript
@@ -111,7 +111,7 @@ Tests (& Examples) [![Build Status](https://travis-ci.org/dcodeIO/ByteBuffer.js.
111111

112112
Support for IE<10, FF<15, Chrome<9 etc.
113113
---------------------------------------
114-
* Requires a working ArrayBuffer & DataView implementations (i.e. use a [polyfill](https://github.com/inexorabletash/polyfill#typed-arrays-polyfill))
114+
* Requires working ArrayBuffer & DataView implementations (i.e. use a [polyfill](https://github.com/inexorabletash/polyfill#typed-arrays-polyfill))
115115

116116
Contributors
117117
------------

0 commit comments

Comments
 (0)