Skip to content

Commit dd75744

Browse files
committed
Accept parameter to LE/BE
1 parent 2730b57 commit dd75744

File tree

9 files changed

+205
-82
lines changed

9 files changed

+205
-82
lines changed

ByteBuffer.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
* @const
112112
* @expose
113113
*/
114-
ByteBuffer.VERSION = "2.0.0";
114+
ByteBuffer.VERSION = "2.0.1";
115115

116116
/**
117117
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -167,7 +167,7 @@
167167
function b2ab(b) {
168168
var ab = new ArrayBuffer(b.length),
169169
view = new Uint8Array(ab);
170-
for (var i= 0, k=b.length; i < k; ++i) view[i] = b[i];
170+
for (var i=0, k=b.length; i < k; ++i) view[i] = b[i];
171171
return ab;
172172
}
173173

@@ -231,22 +231,24 @@
231231
};
232232

233233
/**
234-
* Switches to little endian byte order.
234+
* Switches little endian byte order.
235+
* @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
235236
* @returns {!ByteBuffer} this
236237
* @expose
237238
*/
238-
ByteBuffer.prototype.LE = function() {
239-
this.littleEndian = true;
239+
ByteBuffer.prototype.LE = function(littleEndian) {
240+
this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
240241
return this;
241242
};
242243

243244
/**
244-
* Switches to bid endian byte order.
245+
* Switches big endian byte order.
246+
* @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
245247
* @returns {!ByteBuffer} this
246248
* @expose
247249
*/
248-
ByteBuffer.prototype.BE = function() {
249-
this.littleEndian = false;
250+
ByteBuffer.prototype.BE = function(bigEndian) {
251+
this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
250252
return this;
251253
};
252254

@@ -1686,7 +1688,7 @@
16861688
}
16871689
var o,
16881690
out = new ByteBuffer(str.length/2, littleEndian);
1689-
for (var i= 0, k=str.length; i<k; i+=2) {
1691+
for (var i=0, k=str.length; i<k; i+=2) {
16901692
out.writeUint8(parseInt(str.substring(i, i+2), 16));
16911693
}
16921694
return out.flip();

ByteBuffer.min.js

Lines changed: 47 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: 2 additions & 2 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
* @type {string}
104104
* @const
105105
*/
106-
ByteBuffer.VERSION = "2.0.0";
106+
ByteBuffer.VERSION = "2.0.1";
107107

108108
/**
109109
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -154,7 +154,7 @@
154154
function b2ab(b) {
155155
var ab = new ArrayBuffer(b.length),
156156
view = new Uint8Array(ab);
157-
for (var i= 0, k=b.length; i < k; ++i) view[i] = b[i];
157+
for (var i=0, k=b.length; i < k; ++i) view[i] = b[i];
158158
return ab;
159159
}
160160

@@ -217,20 +217,22 @@
217217
};
218218

219219
/**
220-
* Switches to little endian byte order.
220+
* Switches little endian byte order.
221+
* @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
221222
* @returns {!ByteBuffer} this
222223
*/
223-
ByteBuffer.prototype.LE = function() {
224-
this.littleEndian = true;
224+
ByteBuffer.prototype.LE = function(littleEndian) {
225+
this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
225226
return this;
226227
};
227228

228229
/**
229-
* Switches to bid endian byte order.
230+
* Switches big endian byte order.
231+
* @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
230232
* @returns {!ByteBuffer} this
231233
*/
232-
ByteBuffer.prototype.BE = function() {
233-
this.littleEndian = false;
234+
ByteBuffer.prototype.BE = function(bigEndian) {
235+
this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
234236
return this;
235237
};
236238

@@ -1597,7 +1599,7 @@
15971599
}
15981600
var o,
15991601
out = new ByteBuffer(str.length/2, littleEndian);
1600-
for (var i= 0, k=str.length; i<k; i+=2) {
1602+
for (var i=0, k=str.length; i<k; i+=2) {
16011603
out.writeUint8(parseInt(str.substring(i, i+2), 16));
16021604
}
16031605
return out.flip();

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![ByteBuffer.js - A Java-like ByteBuffer](https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png)
1+
![ByteBuffer.js - A full-featured ByteBuffer implementation in JavaScript](https://raw.github.com/dcodeIO/ByteBuffer.js/master/ByteBuffer.png)
22
======================================
33
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
@@ -9,6 +9,7 @@ favor of making this more intuitive.
99

1010
What can it do?
1111
---------------
12+
* Mimics Java ByteBuffers as close as reasonable while using typed array terms
1213
* Signed and unsigned integers (8, 16, 32, 64 bit through [Long.js](https://github.com/dcodeIO/Long.js)) with endianness support
1314
* 32 and 64 bit floats
1415
* Varints as known from protobuf including zig-zag encoding

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

docs/ByteBuffer.html

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3366,15 +3366,15 @@ <h5>Returns:</h5>
33663366

33673367

33683368
<dt>
3369-
<h4 class="name" id="BE"><span class="type-signature"></span>BE<span class="signature">()</span><span class="type-signature"> &rarr; {<a href="ByteBuffer.html">ByteBuffer</a>}</span></h4>
3369+
<h4 class="name" id="BE"><span class="type-signature"></span>BE<span class="signature">(<span class="optional">bigEndian</span>)</span><span class="type-signature"> &rarr; {<a href="ByteBuffer.html">ByteBuffer</a>}</span></h4>
33703370

33713371

33723372
</dt>
33733373
<dd>
33743374

33753375

33763376
<div class="description">
3377-
<p>Switches to bid endian byte order.</p>
3377+
<p>Switches big endian byte order.</p>
33783378
</div>
33793379

33803380

@@ -3383,6 +3383,64 @@ <h4 class="name" id="BE"><span class="type-signature"></span>BE<span class="sign
33833383

33843384

33853385

3386+
<h5>Parameters:</h5>
3387+
3388+
3389+
<table class="params">
3390+
<thead>
3391+
<tr>
3392+
3393+
<th>Name</th>
3394+
3395+
3396+
<th>Type</th>
3397+
3398+
3399+
<th>Argument</th>
3400+
3401+
3402+
3403+
3404+
<th class="last">Description</th>
3405+
</tr>
3406+
</thead>
3407+
3408+
<tbody>
3409+
3410+
3411+
<tr>
3412+
3413+
<td class="name"><code>bigEndian</code></td>
3414+
3415+
3416+
<td class="type">
3417+
3418+
3419+
<span class="param-type">boolean</span>
3420+
3421+
3422+
3423+
</td>
3424+
3425+
3426+
<td class="attributes">
3427+
3428+
&lt;optional><br>
3429+
3430+
3431+
3432+
</td>
3433+
3434+
3435+
3436+
3437+
<td class="description last"><p>Defaults to <code>true</code>, otherwise uses little endian</p></td>
3438+
</tr>
3439+
3440+
3441+
</tbody>
3442+
</table>
3443+
33863444

33873445

33883446
<dl class="details">
@@ -4084,15 +4142,15 @@ <h5>Returns:</h5>
40844142

40854143

40864144
<dt>
4087-
<h4 class="name" id="LE"><span class="type-signature"></span>LE<span class="signature">()</span><span class="type-signature"> &rarr; {<a href="ByteBuffer.html">ByteBuffer</a>}</span></h4>
4145+
<h4 class="name" id="LE"><span class="type-signature"></span>LE<span class="signature">(<span class="optional">littleEndian</span>)</span><span class="type-signature"> &rarr; {<a href="ByteBuffer.html">ByteBuffer</a>}</span></h4>
40884146

40894147

40904148
</dt>
40914149
<dd>
40924150

40934151

40944152
<div class="description">
4095-
<p>Switches to little endian byte order.</p>
4153+
<p>Switches little endian byte order.</p>
40964154
</div>
40974155

40984156

@@ -4101,6 +4159,64 @@ <h4 class="name" id="LE"><span class="type-signature"></span>LE<span class="sign
41014159

41024160

41034161

4162+
<h5>Parameters:</h5>
4163+
4164+
4165+
<table class="params">
4166+
<thead>
4167+
<tr>
4168+
4169+
<th>Name</th>
4170+
4171+
4172+
<th>Type</th>
4173+
4174+
4175+
<th>Argument</th>
4176+
4177+
4178+
4179+
4180+
<th class="last">Description</th>
4181+
</tr>
4182+
</thead>
4183+
4184+
<tbody>
4185+
4186+
4187+
<tr>
4188+
4189+
<td class="name"><code>littleEndian</code></td>
4190+
4191+
4192+
<td class="type">
4193+
4194+
4195+
<span class="param-type">boolean</span>
4196+
4197+
4198+
4199+
</td>
4200+
4201+
4202+
<td class="attributes">
4203+
4204+
&lt;optional><br>
4205+
4206+
4207+
4208+
</td>
4209+
4210+
4211+
4212+
4213+
<td class="description last"><p>Defaults to <code>true</code>, otherwise uses big endian</p></td>
4214+
</tr>
4215+
4216+
4217+
</tbody>
4218+
</table>
4219+
41044220

41054221

41064222
<dl class="details">

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

src/ByteBuffer.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
function b2ab(b) {
168168
var ab = new ArrayBuffer(b.length),
169169
view = new Uint8Array(ab);
170-
for (var i= 0, k=b.length; i < k; ++i) view[i] = b[i];
170+
for (var i=0, k=b.length; i < k; ++i) view[i] = b[i];
171171
return ab;
172172
}
173173

@@ -231,22 +231,24 @@
231231
};
232232

233233
/**
234-
* Switches to little endian byte order.
234+
* Switches little endian byte order.
235+
* @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
235236
* @returns {!ByteBuffer} this
236237
* @expose
237238
*/
238-
ByteBuffer.prototype.LE = function() {
239-
this.littleEndian = true;
239+
ByteBuffer.prototype.LE = function(littleEndian) {
240+
this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
240241
return this;
241242
};
242243

243244
/**
244-
* Switches to bid endian byte order.
245+
* Switches big endian byte order.
246+
* @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
245247
* @returns {!ByteBuffer} this
246248
* @expose
247249
*/
248-
ByteBuffer.prototype.BE = function() {
249-
this.littleEndian = false;
250+
ByteBuffer.prototype.BE = function(bigEndian) {
251+
this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
250252
return this;
251253
};
252254

@@ -1686,7 +1688,7 @@
16861688
}
16871689
var o,
16881690
out = new ByteBuffer(str.length/2, littleEndian);
1689-
for (var i= 0, k=str.length; i<k; i+=2) {
1691+
for (var i=0, k=str.length; i<k; i+=2) {
16901692
out.writeUint8(parseInt(str.substring(i, i+2), 16));
16911693
}
16921694
return out.flip();

0 commit comments

Comments
 (0)