8000 Added ByteBuffer#readBytes/writeBytes, fixes #9 · adon-at-work/bytebuffer.js@222ed49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 222ed49

Browse files
committed
Added ByteBuffer#readBytes/writeBytes, fixes protobufjs#9
1 parent 89f84e8 commit 222ed49

14 files changed

+320
-175
lines changed

dist/ByteBufferAB.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,41 @@
371371
return bb;
372372
};
373373

374+
/**
375+
* Reads the specified number of bytes.
376+
* @param {number} length Number of bytes to read
377+
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
378+
* @returns {!ByteBuffer}
379+
* @expose
380+
*/
381+
ByteBufferPrototype.readBytes = function(length, offset) {
382+
var relative = typeof offset === 'undefined';
383+
if (relative) offset = this.offset;
384+
if (!this.noAssert) {
385+
if (typeof offset !== 'number' || offset % 1 !== 0)
386+
throw TypeError("Illegal offset: "+offset+" (not an integer)");
387+
offset >>>= 0;
388+
if (offset < 0 || offset + length > this.buffer.byteLength)
389+
throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
390+
}
391+
var slice = this.slice(offset, offset + length);
392+
if (relative) this.offset += length;
393+
return slice;
394+
};
395+
396+
/**
397+
* Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
398+
* @function
399+
* @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
400+
* will be modified according to the performed read operation.
401+
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
402+
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
403+
* written if omitted.
404+
* @returns {!ByteBuffer} this
405+
* @expose
406+
*/
407+
ByteBufferPrototype.writeBytes = ByteBufferPrototype.append;
408+
374409
// types/ints/int8
375410

376411
/**
@@ -1152,7 +1187,7 @@
11521187
*/
11531188

11541189
/**
1155-
* Reads an IEEE754 float from an array.
1190+
* Reads an IEEE754 float from a byte array.
11561191
* @param {!Array} buffer
11571192
* @param {number} offset
11581193
* @param {boolean} isLE
@@ -1195,7 +1230,7 @@
11951230
}
11961231

11971232
/**
1198-
* Writes an IEEE754 float to an array.
1233+
* Writes an IEEE754 float to a byte array.
11991234
* @param {!Array} buffer
12001235
* @param {number} value
12011236
* @param {number} offset
@@ -2266,7 +2301,7 @@
22662301
* will be modified according to the performed read operation.
22672302
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
22682303
* @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
2269-
* read if omitted.
2304+
* written if omitted.
22702305
* @returns {!ByteBuffer} this
22712306
* @expose
22722307
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`

dist/ByteBufferAB.min.js

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

dist/ByteBufferAB.min.js.gz

35 Bytes
Binary file not shown.

dist/ByteBufferAB.min.map

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

dist/ByteBufferAB_DataView.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,41 @@
372372
return bb;
373373
};
374374

375+
/**
376+
* Reads the specified number of bytes.
377+
* @param {number} length Number of bytes to read
378+
* @param {number=} offset Offset to read from. Will use and increase {@link ByteBuffer#offset} by `length` if omitted.
379+
* @returns {!ByteBuffer}
380+
* @expose
381+
*/
382+
ByteBufferPrototype.readBytes = function(length, offset) {
383+
var relative = typeof offset === 'undefined';
384+
if (relative) offset = this.offset;
385+
if (!this.noAssert) {
386+
if (typeof offset !== 'number' || offset % 1 !== 0)
387+
throw TypeError("Illegal offset: "+offset+" (not an integer)");
388+
offset >>>= 0;
389+
if (offset < 0 || offset + length > this.buffer.byteLength)
390+
throw RangeError("Illegal offset: 0 <= "+offset+" (+"+length+") <= "+this.buffer.byteLength);
391+
}
392+
var slice = this.slice(offset, offset + length);
393+
if (relative) this.offset += length;
394+
return slice;
395+
};
396+
397+
/**
398+
* Writes a payload of bytes. This is an alias of {@link ByteBuffer#append}.
399+
* @function
400+
* @param {!ByteBuffer|!ArrayBuffer|!Uint8Array|string} source Data to write. If `source` is a ByteBuffer, its offsets
401+
* will be modified according to the performed read operation.
402+
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
403+
* @param {number=} offset Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
404+
* written if omitted.
405+
* @returns {!ByteBuffer} this
406+
* @expose
407+
*/
408+
ByteBufferPrototype.writeBytes = ByteBufferPrototype.append;
409+
375410
// types/ints/int8
376411

377412
/**
@@ -1971,7 +2006,7 @@
19712006
* will be modified according to the performed read operation.
19722007
* @param {(string|number)=} encoding Encoding if `data` is a string ("base64", "hex", "binary", defaults to "utf8")
19732008
* @param {number=} offset Offset to append at. Will use and increase {@link ByteBuffer#offset} by the number of bytes
1974-
* read if omitted.
2009+
* written if omitted.
19752010
* @returns {!ByteBuffer} this
19762011
* @expose
19772012
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`

0 commit comments

Comments
 (0)
0