forked from protobufjs/bytebuffer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteBuffer.noexpose.js
More file actions
2207 lines (2057 loc) · 97.1 KB
/
Copy pathByteBuffer.noexpose.js
File metadata and controls
2207 lines (2057 loc) · 97.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @license ByteBuffer.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
* Released under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/ByteBuffer.js for details
*/ //
(function(global) {
"use strict";
// Note that this library carefully avoids using the array access operator
// (i.e. buffer[x]) on ArrayBufferView subclasses (e.g. Uint8Array), and
// uses DataView instead. This is required for IE 8 compatibility.
/**
* @param {Function=} Long
* @returns {Function}
* @inner
*/
function loadByteBuffer(Long) {
// Support node's Buffer if available, see http://nodejs.org/api/buffer.html
var Buffer = null;
if (typeof require === 'function') {
try {
var nodeBuffer = require("buffer");
Buffer = nodeBuffer && typeof nodeBuffer['Buffer'] === 'function' &&
typeof nodeBuffer['Buffer']['isBuffer'] === 'function' ? nodeBuffer['Buffer'] : null;
} catch (e) {}
}
/**
* Constructs a new ByteBuffer.
* @class A full-featured ByteBuffer implementation in JavaScript using typed arrays.
* @exports ByteBuffer
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
* endian.
* @param {boolean=} sparse If set to `true`, a ByteBuffer with array=view=null will be created which have to be
* set manually afterwards. Defaults to `false`.
*/
var ByteBuffer = function(capacity, littleEndian, sparse) {
capacity = typeof capacity !== 'undefined' ? parseInt(capacity, 10) : ByteBuffer.DEFAULT_CAPACITY;
if (capacity < 1) capacity = ByteBuffer.DEFAULT_CAPACITY;
/**
* Backing ArrayBuffer.
* @type {?ArrayBuffer}
*/
this.array = sparse ? null : new ArrayBuffer(capacity);
/**
* DataView to mess with the ArrayBuffer.
* @type {?DataView}
*/
this.view = sparse ? null : new DataView(this.array);
/**
* Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset
* and length, which are both absolute indexes. There is no capacity property, use
* {@link ByteBuffer#capacity} instead.
* @type {number}
*/
this.offset = 0;
/**
* Marked offset set through {@link ByteBuffer#mark}. Defaults to `-1` (no marked offset).
* @type {number}
*/
this.markedOffset = -1;
/**
* Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between
* offset and length, which are both absolute indexes. There is no capacity property, use
* {@link ByteBuffer#capacity} instead.
* @type {number}
*/
this.length = 0;
/**
* Whether to use little endian multi byte values, defaults to `false` for big endian.
* @type {boolean}
*/
this.littleEndian = typeof littleEndian != 'undefined' ? !!littleEndian : false;
};
/**
* Version string.
* @type {string}
* @const
*/
ByteBuffer.VERSION = "2.3.1";
/**
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
* @type {number}
* @const
*/
ByteBuffer.DEFAULT_CAPACITY = 16;
/**
* Little endian constant for usage in constructors instead of a boolean value. Evaluates to `true`.
* @type {boolean}
* @const
*/
ByteBuffer.LITTLE_ENDIAN = true;
/**
* Big endian constant for usage in constructors instead of a boolean value. Evaluates to `false`.
* @type {boolean}
* @const
*/
ByteBuffer.BIG_ENDIAN = false;
/**
* Long class for int64 support. May be `null` if the Long class has not been loaded and int64 support is
* not available.
* @type {?Long}
* @const
*/
ByteBuffer.Long = Long || null;
/**
* Tests if the specified type is a ByteBuffer or ByteBuffer-like.
* @param {*} bb ByteBuffer to test
* @returns {boolean} true if it is a ByteBuffer or ByteBuffer-like, otherwise false
*/
ByteBuffer.isByteBuffer = function(bb) {
return bb && (
(bb instanceof ByteBuffer) || (
typeof bb === 'object' &&
(bb.array === null || bb.array instanceof ArrayBuffer) &&
(bb.view === null || bb.view instanceof DataView) &&
typeof bb.offset === 'number' &&
typeof bb.markedOffset === 'number' &&
typeof bb.length === 'number' &&
typeof bb.littleEndian === 'boolean'
)
);
};
/**
* Allocates a new ByteBuffer.
* @param {number=} capacity Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
* endian.
* @returns {!ByteBuffer}
*/
ByteBuffer.allocate = function(capacity, littleEndian) {
return new ByteBuffer(capacity, littleEndian);
};
/**
* Converts a node.js <= 0.8 Buffer to an ArrayBuffer.
* @param {!Buffer} b Buffer to convert
* @returns {?ArrayBuffer} Converted buffer
* @inner
*/
function b2ab(b) {
var ab = new ArrayBuffer(b.length),
view = new Uint8Array(ab);
for (var i=0, k=b.length; i < k; ++i) view[i] = b[i];
return ab;
}
/**
* Wraps an ArrayBuffer, any object containing an ArrayBuffer, a node buffer or a string. Sets the created
* ByteBuffer's offset to 0 and its length to the wrapped object's byte length.
* @param {!ArrayBuffer|!Buffer|!{array: !ArrayBuffer}|!{buffer: !ArrayBuffer}|string} buffer Anything that can
* be wrapped
* @param {(string|boolean)=} enc String encoding if a string is provided (hex, utf8, binary, defaults to base64)
* @param {boolean=} littleEndian `true` to use little endian multi byte values, defaults to `false` for big
* endian.
* @returns {!ByteBuffer}
* @throws {Error} If the specified object cannot be wrapped
*/
ByteBuffer.wrap = function(buffer, enc, littleEndian) {
if (typeof enc === 'boolean') {
littleEndian = enc;
enc = "utf8";
}
// Wrap a string
if (typeof buffer === 'string') {
switch (enc) {
case "base64":
return ByteBuffer.decode64(buffer, littleEndian);
case "hex":
return ByteBuffer.decodeHex(buffer, littleEndian);
case "binary":
return ByteBuffer.decodeBinary(buffer, littleEndian);
default:
return new ByteBuffer(ByteBuffer.DEFAULT_CAPACITY, littleEndian).writeUTF8String(buffer).flip();
}
}
var b;
// Wrap Buffer
if (Buffer && Buffer.isBuffer(buffer)) {
b = new Uint8Array(buffer).buffer; // noop on node <= 0.8
buffer = (b === buffer) ? b2ab(buffer) : b;
}
// Refuse to wrap anything that's null or not an object
if (buffer === null || typeof buffer !== 'object') {
throw(new Error("Cannot wrap null or non-object"));
}
// Wrap ByteBuffer by cloning (preserve offsets)
if (ByteBuffer.isByteBuffer(buffer)) {
return ByteBuffer.prototype.clone.call(buffer); // Also makes ByteBuffer-like a ByteBuffer
}
// Wrap any object that is or contains an ArrayBuffer
if (!!buffer["array"]) {
buffer = buffer["array"];
} else if (!!buffer["buffer"]) {
buffer = buffer["buffer"];
}
if (!(buffer instanceof ArrayBuffer)) {
throw(new Error("Cannot wrap buffer of type "+typeof(buffer)+", "+buffer.constructor.name));
}
b = new ByteBuffer(0, littleEndian, true);
b.array = buffer;
b.view = b.array.byteLength > 0 ? new DataView(b.array) : null;
b.offset = 0;
b.length = buffer.byteLength;
return b;
};
/**
* Switches little endian byte order.
* @param {boolean=} littleEndian Defaults to `true`, otherwise uses big endian
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.LE = function(littleEndian) {
this.littleEndian = typeof littleEndian !== 'undefined' ? !!littleEndian : true;
return this;
};
/**
* Switches big endian byte order.
* @param {boolean=} bigEndian Defaults to `true`, otherwise uses little endian
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.BE = function(bigEndian) {
this.littleEndian = typeof bigEndian !== 'undefined' ? !bigEndian : false;
return this;
};
/**
* Resizes the ByteBuffer to the given capacity. Will do nothing if already that large or larger.
* @param {number} capacity New capacity
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.resize = function(capacity) {
if (capacity < 1) return false;
if (this.array === null) { // Silently recreate
this.array = new ArrayBuffer(capacity);
this.view = new DataView(this.array);
}
if (this.array.byteLength < capacity) {
var src = this.array;
var srcView = new Uint8Array(src);
var dst = new ArrayBuffer(capacity);
var dstView = new Uint8Array(dst);
dstView.set(srcView);
this.array = dst;
this.view = new DataView(dst);
}
return this;
};
/**
* Slices the ByteBuffer. This is independent of the ByteBuffer's actual offsets. Does not compact the underlying
* ArrayBuffer (use {@link ByteBuffer#compact} or {@link ByteBuffer.wrap} instead).
* @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
* @param {number=} end End offset, defaults to {@link ByteBuffer#length}.
* @returns {!ByteBuffer} Clone of this ByteBuffer with slicing applied, backed by the same ArrayBuffer
* @throws {Error} If the buffer cannot be sliced
*/
ByteBuffer.prototype.slice = function(begin, end) {
if (this.array == null) {
throw(new Error(this+" cannot be sliced: Already destroyed"));
}
if (typeof begin === 'undefined') begin = this.offset;
if (typeof end === 'undefined') end = this.length;
if (end <= begin) {
var t = end; end = begin; begin = t;
}
if (begin < 0 || begin > this.array.byteLength || end < 1 || end > this.array.byteLength) {
throw(new Error(this+" cannot be sliced: Index out of bounds (0-"+this.array.byteLength+" -> "+begin+"-"+end+")"));
}
var b = this.clone();
b.offset = begin;
b.length = end;
return b;
};
/**
* Makes sure that the specified capacity is available. If the current capacity is exceeded, it will be doubled.
* If double the previous capacity is less than the required capacity, the required capacity will be used.
* @param {number} capacity Required capacity
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.ensureCapacity = function(capacity) {
if (this.array === null)
return this.resize(capacity);
if (this.array.byteLength < capacity)
return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
return this;
};
/**
* Makes the buffer ready for a new sequence of write or relative read operations. Sets `length=offset` and
* `offset=0`. Always make sure to flip a buffer when all relative writing operations are complete.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.flip = function() {
this.length = this.array == null ? 0 : this.offset;
this.offset = 0;
return this;
};
/**
* Marks an offset to be used with {@link ByteBuffer#reset}.
* @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
* @returns {!ByteBuffer} this
* @throws {Error} If the mark cannot be set
* @see ByteBuffer#reset
*/
ByteBuffer.prototype.mark = function(offset) {
if (this.array == null) {
throw(new Error(this+" cannot be marked: Already destroyed"));
}
offset = typeof offset !== 'undefined' ? parseInt(offset, 10) : this.offset;
if (offset < 0 || offset > this.array.byteLength) {
throw(new Error(this+" cannot be marked: Offset to mark is less than 0 or bigger than the capacity ("+this.array.byteLength+"): "+offset));
}
this.markedOffset = offset;
return this;
};
/**
* Resets the ByteBuffer. If an offset has been marked through {@link ByteBuffer#mark} before, the offset will
* be set to the marked offset and the marked offset will be discarded. Length will not be altered. If there is
* no marked offset, sets `offset=0` and `length=0`.
* @returns {!ByteBuffer} this
* @see ByteBuffer#mark
*/
ByteBuffer.prototype.reset = function() {
if (this.array === null) {
throw(new Error(this+" cannot be reset: Already destroyed"));
}
if (this.markedOffset >= 0) {
this.offset = this.markedOffset;
this.markedOffset = -1;
} else {
this.offset = 0;
this.length = 0;
}
return this;
};
/**
* Clones this ByteBuffer. The returned cloned ByteBuffer shares the same backing array but will have its own
* offsets.
* @returns {!ByteBuffer} Clone
*/
ByteBuffer.prototype.clone = function() {
var b = new ByteBuffer(-1, this.littleEndian, /* no init, undocumented */ true);
b.array = this.array;
b.view = this.view;
b.offset = this.offset;
b.markedOffset = this.markedOffset;
b.length = this.length;
return b;
};
/**
* Copies this ByteBuffer. The copy has its own backing array and uses the same offsets as this one.
* @returns {!ByteBuffer} Copy
*/
ByteBuffer.prototype.copy = function() {
if (this.array == null) {
return this.clone();
}
var b = new ByteBuffer(this.array.byteLength, this.littleEndian);
var src = new Uint8Array(this.array);
var dst = new Uint8Array(b.array);
dst.set(src);
b.offset = this.offset;
b.markedOffset = this.markedOffset;
b.length = this.length;
return b;
};
/**
* Gets the number of remaining readable bytes. Contents are the bytes between offset and length, so this
* returns `length-offset`.
* @returns {number} Remaining readable bytes. May be negative if `offset>length`.
*/
ByteBuffer.prototype.remaining = function() {
if (this.array === null) return 0;
return this.length - this.offset;
};
/**
* Gets the capacity of the backing buffer. This is independent from {@link ByteBuffer#length} and returns the
* size of the entire backing array.
* @returns {number} Capacity of the backing array or 0 if destroyed
*/
ByteBuffer.prototype.capacity = function() {
return this.array != null ? this.array.byteLength : 0;
};
/**
* Compacts the ByteBuffer to be backed by an ArrayBuffer of its actual length. Will set `offset=0` and
* `length=capacity`.
* @returns {!ByteBuffer} this
* @throws {Error} If the buffer cannot be compacted
*/
ByteBuffer.prototype.compact = function() {
if (this.array == null) {
throw(new Error(this+" cannot be compacted: Already destroyed"));
}
if (this.offset > this.length) {
this.flip();
}
if (this.offset === this.length) {
this.array = new ArrayBuffer(0);
this.view = null; // A DataView on a zero-length AB would throw
return this;
}
if (this.offset === 0 && this.length === this.array.byteLength) {
return this; // Already compacted
}
var srcView = new Uint8Array(this.array);
var dst = new ArrayBuffer(this.length-this.offset);
var dstView = new Uint8Array(dst);
dstView.set(srcView.subarray(this.offset, this.length));
this.array = dst;
if (this.markedOffset >= this.offset) {
this.markedOffset -= this.offset;
} else {
this.markedOffset = -1;
}
this.offset = 0;
this.length = this.array.byteLength;
return this;
};
/**
* Manually destroys the ByteBuffer, releasing references to the backing array. Manually destroying a ByteBuffer
* is usually not required but may be useful in limited memory environments. Most successive operations will
* rise an error until {@link ByteBuffer#resize} or {@link ByteBuffer#ensureCapacity} is called to reinitialize
* the backing array.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.destroy = function() {
if (this.array !== null) {
this.array = null;
this.view = null;
this.offset = 0;
this.markedOffset = -1;
this.length = 0;
}
return this;
};
/**
* Reverses the backing array and adapts offset and length to retain the same relative position on the reversed
* data in inverse order. Example: "00<01 02>03 04".reverse() = "04 03<02 01>00". Also clears the marked
* offset.
* @returns {!ByteBuffer} this
* @throws {Error} If the buffer is already destroyed
*/
ByteBuffer.prototype.reverse = function() {
if (this.array === null) {
throw(new Error(this+" cannot be reversed: Already destroyed"));
}
Array.prototype.reverse.call(new Uint8Array(this.array));
var o = this.offset;
this.offset = this.array.byteLength - this.length;
this.markedOffset = -1;
this.length = this.array.byteLength - o;
this.view = new DataView(this.array);
return this;
};
/**
* Appends another ByteBuffer to this one. Appends only the portion between offset and length of the specified
* ByteBuffer and overwrites any contents behind the specified offset up to the number of bytes contained in
* the specified ByteBuffer. Offset and length of the specified ByteBuffer will remain the same.
* @param {!*} src ByteBuffer or any object that can be wrapped to append
* @param {number=} offset Offset to append at. Defaults to {@link ByteBuffer#offset}.
* @returns {!ByteBuffer} this
* @throws {Error} If the specified buffer is already destroyed
*/
ByteBuffer.prototype.append = function(src, offset) {
if (!(src instanceof ByteBuffer)) {
src = ByteBuffer.wrap(src);
}
if (src.array === null) {
throw(new Error(src+" cannot be appended to "+this+": Already destroyed"));
}
var n = src.length - src.offset;
if (n == 0) return this; // Nothing to append
if (n < 0) {
src = src.clone().flip();
n = src.length - src.offset;
}
offset = typeof offset !== 'undefined' ? offset : (this.offset+=n)-n;
this.ensureCapacity(offset+n); // Reinitializes if required
var srcView = new Uint8Array(src.array);
var dstView = new Uint8Array(this.array);
dstView.set(srcView.subarray(src.offset, src.length), offset);
return this;
};
/**
* Prepends another ByteBuffer to this one. Prepends only the portion between offset and length of the specified
* ByteBuffer and overwrites any contents before the specified offsets up to the number of bytes contained in
* the specified ByteBuffer. Offset and length of the specified ByteBuffer will remain the same.
* @param {!*} src ByteBuffer or any object that can be wrapped to prepend
* @param {number=} offset Offset to prepend at. Defaults to {@link ByteBuffer#offset}.
* @returns {!ByteBuffer} this
* @throws {Error} If the specified buffer is already destroyed
*/
ByteBuffer.prototype.prepend = function(src, offset) {
if (!(src instanceof ByteBuffer)) {
src = ByteBuffer.wrap(src);
}
if (src.array === null) {
throw(src+" cannot be prepended to "+this+": Already destroyed");
}
var n = src.length - src.offset;
if (n == 0) return this; // Nothing to prepend
if (n < 0) {
src = src.clone().flip();
n = src.length - src.offset;
}
var modify = typeof offset === 'undefined';
offset = typeof offset !== 'undefined' ? offset : this.offset;
var diff = n-offset;
if (diff > 0) {
// Doesn't fit, so maybe resize and move the contents that are already contained
this.ensureCapacity(this.length+diff);
this.append(this, n);
this.offset += diff;
this.length += diff;
this.append(src, 0);
} else {
this.append(src, offset-n);
}
if (modify) {
this.offset -= n;
}
return this;
};
/**
* Writes an 8bit signed integer.
* @param {number} value Value
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if
* omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeInt8 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setInt8(offset, value);
return this;
};
/**
* Reads an 8bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readInt8 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=1)-1;
if (offset >= this.array.byteLength) {
throw(new Error("Cannot read int8 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getInt8(offset);
};
/**
* Writes a byte. This is an alias of {ByteBuffer#writeInt8}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeByte = ByteBuffer.prototype.writeInt8;
/**
* Reads a byte. This is an alias of {@link ByteBuffer#readInt8}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readByte = ByteBuffer.prototype.readInt8;
/**
* Writes an 8bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeUint8 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=1)-1;
this.ensureCapacity(offset+1);
this.view.setUint8(offset, value);
return this;
};
/**
* Reads an 8bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readUint8 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=1)-1;
if (offset+1 > this.array.byteLength) {
throw(new Error("Cannot read uint8 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getUint8(offset);
};
/**
* Writes a 16bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeInt16 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setInt16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readInt16 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=2)-2;
if (offset+2 > this.array.byteLength) {
throw(new Error("Cannot read int16 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getInt16(offset, this.littleEndian);
};
/**
* Writes a short value. This is an alias of {@link ByteBuffer#writeInt16}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeShort = ByteBuffer.prototype.writeInt16;
/**
* Reads a short value. This is an alias of {@link ByteBuffer#readInt16}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readShort = ByteBuffer.prototype.readInt16;
/**
* Writes a 16bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeUint16 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=2)-2;
this.ensureCapacity(offset+2);
this.view.setUint16(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 16bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readUint16 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=2)-2;
if (offset+2 > this.array.byteLength) {
throw(new Error("Cannot read int16 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getUint16(offset, this.littleEndian);
};
/**
* Writes a 32bit signed integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeInt32 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setInt32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit signed integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readInt32 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4;
if (offset+4 > this.array.byteLength) {
throw(new Error("Cannot read int32 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getInt32(offset, this.littleEndian);
};
/**
* Writes an integer. This is an alias of {@link ByteBuffer#writeInt32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeInt = ByteBuffer.prototype.writeInt32;
/**
* Reads an integer. This is an alias of {@link ByteBuffer#readInt32}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readInt = ByteBuffer.prototype.readInt32;
/**
* Writes a 32bit unsigned integer.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeUint32 = function(value, offset) {
offset = typeof offset != 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setUint32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit unsigned integer.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readUint32 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4;
if (offset+4 > this.array.byteLength) {
throw(new Error("Cannot read uint32 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getUint32(offset, this.littleEndian);
};
/**
* Writes a 32bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat32 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4;
this.ensureCapacity(offset+4);
this.view.setFloat32(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 32bit float.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readFloat32 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=4)-4;
if (this.array === null || offset+4 > this.array.byteLength) {
throw(new Error("Cannot read float32 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getFloat32(offset, this.littleEndian);
};
/**
* Writes a float. This is an alias of {@link ByteBuffer#writeFloat32}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat = ByteBuffer.prototype.writeFloat32;
/**
* Reads a float. This is an alias of {@link ByteBuffer#readFloat32}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readFloat = ByteBuffer.prototype.readFloat32;
/**
* Writes a 64bit float.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeFloat64 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
this.ensureCapacity(offset+8);
this.view.setFloat64(offset, value, this.littleEndian);
return this;
};
/**
* Reads a 64bit float.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readFloat64 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
if (this.array === null || offset+8 > this.array.byteLength) {
throw(new Error("Cannot read float64 from "+this+" at "+offset+": Capacity overflow"));
}
return this.view.getFloat64(offset, this.littleEndian);
};
/**
* Writes a double. This is an alias of {@link ByteBuffer#writeFloat64}.
* @function
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeDouble = ByteBuffer.prototype.writeFloat64;
/**
* Reads a double. This is an alias of {@link ByteBuffer#readFloat64}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {number}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readDouble = ByteBuffer.prototype.readFloat64;
// Available with Long.js only
if (Long) {
/**
* Writes a 64bit integer. Requires Long.js.
* @function
* @param {number|!Long} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeInt64 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, false);
this.ensureCapacity(offset+8);
if (this.littleEndian) {
this.view.setInt32(offset, value.getLowBits(), true);
this.view.setInt32(offset+4, value.getHighBits(), true);
} else {
this.view.setInt32(offset, value.getHighBits(), false);
this.view.setInt32(offset+4, value.getLowBits(), false);
}
return this;
};
/**
* Reads a 64bit integer. Requires Long.js.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!Long}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readInt64 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
if (this.array === null || offset+8 > this.array.byteLength) {
this.offset -= 8;
throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow"));
}
var value;
if (this.littleEndian) {
value = Long.fromBits(this.view.getInt32(offset, true), this.view.getInt32(offset+4, true), false);
} else {
value = Long.fromBits(this.view.getInt32(offset+4, false), this.view.getInt32(offset, false), false);
}
return value;
};
/**
* Writes a 64bit unsigned integer. Requires Long.js.
* @function
* @param {number|!Long} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeUint64 = function(value, offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
if (!(typeof value === 'object' && value instanceof Long)) value = Long.fromNumber(value, true);
this.ensureCapacity(offset+8);
if (this.littleEndian) {
this.view.setUint32(offset, value.getLowBitsUnsigned(), true);
this.view.setUint32(offset+4, value.getHighBitsUnsigned(), true);
} else {
this.view.setUint32(offset, value.getHighBitsUnsigned(), false);
this.view.setUint32(offset+4, value.getLowBitsUnsigned(), false);
}
return this;
};
/**
* Reads a 64bit unsigned integer. Requires Long.js.
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!Long}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readUint64 = function(offset) {
offset = typeof offset !== 'undefined' ? offset : (this.offset+=8)-8;
if (this.array === null || offset+8 > this.array.byteLength) {
this.offset -= 8;
throw(new Error("Cannot read int64 from "+this+" at "+offset+": Capacity overflow"));
}
var value;
if (this.littleEndian) {
value = Long.fromBits(this.view.getUint32(offset, true), this.view.getUint32(offset+4, true), true);
} else {
value = Long.fromBits(this.view.getUint32(offset+4, false), this.view.getUint32(offset, false), true);
}
return value;
};
/**
* Writes a long. This is an alias of {@link ByteBuffer#writeInt64}.
* @function
* @param {number|!Long} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer} this
*/
ByteBuffer.prototype.writeLong = ByteBuffer.prototype.writeInt64;
/**
* Reads a long. This is an alias of {@link ByteBuffer#readInt64}.
* @function
* @param {number=} offset Offset to read from. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!Long}
* @throws {Error} If offset is out of bounds
*/
ByteBuffer.prototype.readLong = ByteBuffer.prototype.readInt64;
}
/**
* Maximum number of bytes used by 32bit base 128 variable-length integer.
* @type {number}
* @const
*/
ByteBuffer.MAX_VARINT32_BYTES = 5;
/**
* Writes a 32bit base 128 variable-length integer as used in protobuf.
* @param {number} value Value to write
* @param {number=} offset Offset to write to. Will use and advance {@link ByteBuffer#offset} if omitted.
* @returns {!ByteBuffer|number} this if offset is omitted, else the actual number of bytes written.
*/
ByteBuffer.prototype.writeVarint32 = function(value, offset) {
var advance = typeof offset === 'undefined';
offset = typeof offset !== 'undefined' ? offset : this.offset;