UNPKG

21.2 kBTypeScriptView Raw
1// Type definitions for bytebuffer.js 5.0.0
2// Project: https://github.com/dcodeIO/bytebuffer.js
3// Definitions by: DefinitelyTyped <https://github.com/DefinitelyTyped>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5// Definitions by: SINTEF-9012 <https://github.com/SINTEF-9012>
6// Definitions by: Marek Urbanowicz <https://github.com/murbanowicz>
7
8/// <reference types="node" />
9import Long = require('long');
10
11declare namespace ByteBuffer {}
12export = ByteBuffer;
13export as namespace ByteBuffer;
14
15declare class ByteBuffer {
16 /**
17 * Constructs a new ByteBuffer.
18 */
19 constructor(capacity?: number, littleEndian?: boolean, noAssert?: boolean);
20
21 /**
22 * Big endian constant that can be used instead of its boolean value. Evaluates to false.
23 */
24 static BIG_ENDIAN: boolean;
25
26 /**
27 * Default initial capacity of 16.
28 */
29 static DEFAULT_CAPACITY: number;
30
31 /**
32 * Default endianess of false for big endian.
33 */
34 static DEFAULT_ENDIAN: boolean;
35
36 /**
37 * Default no assertions flag of false.
38 */
39 static DEFAULT_NOASSERT: boolean;
40
41 /**
42 * Little endian constant that can be used instead of its boolean value. Evaluates to true.
43 */
44 static LITTLE_ENDIAN: boolean;
45
46 /**
47 * Maximum number of bytes required to store a 32bit base 128 variable-length integer.
48 */
49 static MAX_VARINT32_BYTES: number;
50
51 /**
52 * Maximum number of bytes required to store a 64bit base 128 variable-length integer.
53 */
54 static MAX_VARINT64_BYTES: number;
55
56 /**
57 * Metrics representing number of bytes.Evaluates to 2.
58 */
59 static METRICS_BYTES: number;
60
61 /**
62 * Metrics representing number of UTF8 characters.Evaluates to 1.
63 */
64 static METRICS_CHARS: number;
65
66 /**
67 * ByteBuffer version.
68 */
69 static VERSION: string;
70
71 /**
72 * Backing buffer.
73 */
74 buffer: Buffer;
75
76 /**
77 * Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.
78 */
79 limit: number;
80
81 /**
82 * Whether to use little endian byte order, defaults to false for big endian.
83 */
84 littleEndian: boolean;
85
86 /**
87 * Marked offset.
88 */
89 markedOffset: number;
90
91 /**
92 * Whether to skip assertions of offsets and values, defaults to false.
93 */
94 noAssert: boolean;
95
96 /**
97 * Absolute read/write offset.
98 */
99 offset: number;
100
101 /**
102 * Data view to manipulate the backing buffer. Becomes null if the backing buffer has a capacity of 0.
103 */
104 view: DataView;
105
106 /**
107 * Allocates a new ByteBuffer backed by a buffer of the specified capacity.
108 */
109 static allocate(capacity?: number, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
110
111 /**
112 * Decodes a base64 encoded string to binary like window.atob does.
113 */
114 static atob(b64: string): string;
115
116 /**
117 * Encodes a binary string to base64 like window.btoa does.
118 */
119 static btoa(str: string): string;
120
121 /**
122 * Calculates the number of UTF8 bytes of a string.
123 */
124 static calculateUTF8Bytes(str: string): number;
125
126 /**
127 * Calculates the number of UTF8 characters of a string.JavaScript itself uses UTF- 16, so that a string's length property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.
128 */
129 static calculateUTF8Chars(str: string): number;
130
131 /**
132 * Calculates the number of UTF8 bytes of a string. This is an alias of ByteBuffer#calculateUTF8Bytes.
133 */
134 static calculateString(str: string): number;
135
136 /**
137 * Calculates the actual number of bytes required to store a 32bit base 128 variable-length integer.
138 */
139 static calculateVarint32(value: number): number;
140
141 /**
142 * Calculates the actual number of bytes required to store a 64bit base 128 variable-length integer.
143 */
144 static calculateVarint64(value: number | Long): number;
145
146 /**
147 * Concatenates multiple ByteBuffers into one.
148 */
149 static concat(
150 buffers: Array<ByteBuffer | Buffer | ArrayBuffer | Uint8Array | string>,
151 encoding?: string | boolean,
152 litteEndian?: boolean,
153 noAssert?: boolean,
154 ): ByteBuffer;
155
156 /**
157 * Decodes a base64 encoded string to a ByteBuffer.
158 */
159 static fromBase64(str: string, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
160
161 /**
162 * Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.
163 */
164 static fromBinary(str: string, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
165
166 /**
167 * Decodes a hex encoded string with marked offsets to a ByteBuffer.
168 */
169 static fromDebug(str: string, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
170
171 /**
172 * Decodes a hex encoded string to a ByteBuffer.
173 */
174 static fromHex(str: string, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
175
176 /**
177 * Decodes an UTF8 encoded string to a ByteBuffer.
178 */
179 static fromUTF8(str: string, littleEndian?: boolean, noAssert?: boolean): ByteBuffer;
180
181 /**
182 * Gets the backing buffer type.
183 */
184 static isByteBuffer(bb: any): boolean;
185
186 /**
187 * Wraps a buffer or a string. Sets the allocated ByteBuffer's ByteBuffer#offset to 0 and its ByteBuffer#limit to the length of the wrapped data.
188 * @param buffer Anything that can be wrapped
189 * @param encoding String encoding if buffer is a string ("base64", "hex", "binary", defaults to "utf8")
190 * @param littleEndian Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
191 * @param noAssert Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
192 */
193 static wrap(
194 buffer: ByteBuffer | Buffer | ArrayBuffer | Uint8Array | string,
195 enc?: string | boolean,
196 littleEndian?: boolean,
197 noAssert?: boolean,
198 ): ByteBuffer;
199
200 /**
201 * Decodes a zigzag encoded signed 32bit integer.
202 */
203 static zigZagDecode32(n: number): number;
204
205 /**
206 * Decodes a zigzag encoded signed 64bit integer.
207 */
208 static zigZagDecode64(n: number | Long): Long;
209
210 /**
211 * Zigzag encodes a signed 32bit integer so that it can be effectively used with varint encoding.
212 */
213 static zigZagEncode32(n: number): number;
214
215 /**
216 * Zigzag encodes a signed 64bit integer so that it can be effectively used with varint encoding.
217 */
218 static zigZagEncode64(n: number | Long): Long;
219
220 /**
221 * Switches (to) big endian byte order.
222 */
223 BE(bigEndian?: boolean): this;
224
225 /**
226 * Switches (to) little endian byte order.
227 */
228 LE(bigEndian?: boolean): this;
229
230 /**
231 * Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended data's length.
232 */
233 append(
234 source: ByteBuffer | Buffer | ArrayBuffer | Uint8Array | string,
235 encoding?: string | number,
236 offset?: number,
237 ): this;
238
239 /**
240 * Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents behind the specified offset up to the length of this ByteBuffer's data.
241 */
242 appendTo(target: ByteBuffer, offset?: number): this;
243
244 /**
245 * Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to disable them if your code already makes sure that everything is valid.
246 */
247 assert(assert: boolean): this;
248
249 /**
250 * Gets the capacity of this ByteBuffer's backing buffer.
251 */
252 capacity(): number;
253
254 /**
255 * Clears this ByteBuffer's offsets by setting ByteBuffer#offset to 0 and
256 * ByteBuffer#limit to the backing buffer's capacity. Discards ByteBuffer#markedOffset.
257 */
258 clear(): this;
259
260 /**
261 * Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for ByteBuffer#offset, ByteBuffer#markedOffset and ByteBuffer#limit.
262 */
263 clone(copy?: boolean): ByteBuffer;
264
265 /**
266 * Compacts this ByteBuffer to be backed by a ByteBuffer#buffer of its contents' length. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will set offset = 0 and limit = capacity and adapt ByteBuffer#markedOffset to the same relative position if set.
267 */
268 compact(begin?: number, end?: number): this;
269
270 /**
271 * Creates a copy of this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.
272 */
273 copy(begin?: number, end?: number): ByteBuffer;
274
275 /**
276 * Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.
277 */
278 copyTo(target: ByteBuffer, targetOffset?: number, sourceOffset?: number, sourceLimit?: number): this;
279
280 /**
281 * Makes sure that this ByteBuffer is backed by a ByteBuffer#buffer of at least the specified capacity. If the current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity, the required capacity will be used instead.
282 */
283 ensureCapacity(capacity: number): this;
284
285 /**
286 * Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.
287 */
288 fill(value: number | string, begin?: number, end?: number): this;
289
290 /**
291 * Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets limit = offset and offset = 0. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.
292 */
293 flip(): this;
294
295 /**
296 * Marks an offset on this ByteBuffer to be used later.
297 */
298 mark(offset?: number): this;
299
300 /**
301 * Sets the byte order.
302 */
303 order(littleEndian: boolean): this;
304
305 /**
306 * Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the prepended data's length. If there is not enough space available before the specified offset, the backing buffer will be resized and its contents moved accordingly.
307 */
308 prepend(
309 source: ByteBuffer | string | ArrayBuffer | Buffer,
310 encoding?: string | number,
311 offset?: number,
312 ): this;
313
314 /**
315 * Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the prepended data's length. If there is not enough space available before the specified offset, the backing buffer will be resized and its contents moved accordingly.
316 */
317 prependTo(target: ByteBuffer, offset?: number): this;
318
319 /**
320 * Prints debug information about this ByteBuffer's contents.
321 */
322 printDebug(out?: (text: string) => void): void;
323
324 /**
325 * Reads an 8bit signed integer. This is an alias of ByteBuffer#readInt8.
326 */
327 readByte(offset?: number): number;
328
329 /**
330 * Reads the specified number of bytes
331 */
332 readBytes(length: number, offset?: number): ByteBuffer;
333
334 /**
335 * Reads a NULL-terminated UTF8 encoded string. For this to work the string read must not contain any NULL characters itself.
336 */
337 readCString(): string;
338 readCString(offset: number): { string: string; length: number };
339
340 /**
341 * Reads a 64bit float. This is an alias of ByteBuffer#readFloat64.
342 */
343 readDouble(offset?: number): number;
344
345 /**
346 * Reads a 32bit float. This is an alias of ByteBuffer#readFloat32.
347 */
348 readFloat(offset?: number): number;
349
350 /**
351 * Reads a 32bit float.
352 */
353 readFloat32(offset?: number): number;
354
355 /**
356 * Reads a 64bit float.
357 */
358 readFloat64(offset?: number): number;
359
360 /**
361 * Reads a length as uint32 prefixed UTF8 encoded string.
362 */
363 readIString(): string;
364 readIString(offset: number): { string: string; length: number };
365
366 /**
367 * Reads a 32bit signed integer.This is an alias of ByteBuffer#readInt32.
368 */
369 readInt(offset?: number): number;
370
371 /**
372 * Reads a 16bit signed integer.
373 */
374 readInt16(offset?: number): number;
375
376 /**
377 * Reads a 32bit signed integer.
378 */
379 readInt32(offset?: number): number;
380
381 /**
382 * Reads a 64bit signed integer.
383 */
384 readInt64(offset?: number): Long;
385
386 /**
387 * Reads an 8bit signed integer.
388 */
389 readInt8(offset?: number): number;
390
391 /**
392 * Reads a 64bit signed integer. This is an alias of ByteBuffer#readInt64.
393 */
394 readLong(offset?: number): Long;
395
396 /**
397 * Reads a 16bit signed integer. This is an alias of ByteBuffer#readInt16.
398 */
399 readShort(offset?: number): number;
400
401 /**
402 * Reads an UTF8 encoded string. This is an alias of ByteBuffer#readUTF8String.
403 */
404 readString(length: number, metrics?: number): string;
405 readString(length: number, metrics: number, offset: number): { string: string; length: number };
406
407 /**
408 * Reads an UTF8 encoded string.
409 */
410 readUTF8String(chars: number, metrics?: number): string;
411 readUTF8String(chars: number, metrics: number, offset: number): { string: string; length: number };
412
413 /**
414 * Reads a 16bit unsigned integer.
415 */
416 readUint16(offset?: number): number;
417
418 /**
419 * Reads a 32bit unsigned integer.
420 */
421 readUint32(offset?: number): number;
422
423 /**
424 * Reads a 64bit unsigned integer.
425 */
426 readUint64(offset?: number): Long;
427 /**
428 * Reads an 8bit unsigned integer.
429 */
430 readUint8(offset?: number): number;
431
432 /**
433 * Reads a length as varint32 prefixed UTF8 encoded string.
434 */
435 readVString(): string;
436 readVString(offset: number): { string: string; length: number };
437
438 /**
439 * Reads a 32bit base 128 variable-length integer.
440 */
441 readVarint32(): number;
442 readVarint32(offset: number): { value: number; length: number };
443
444 /**
445 * Reads a zig-zag encoded 32bit base 128 variable-length integer.
446 */
447 readVarint32ZigZag(): number;
448 readVarint32ZigZag(offset: number): { value: number; length: number };
449
450 /**
451 * Reads a 64bit base 128 variable-length integer. Requires Long.js.
452 */
453 readVarint64(): Long;
454 readVarint64(offset: number): { value: Long; length: number };
455
456 /**
457 * Reads a zig-zag encoded 64bit base 128 variable-length integer. Requires Long.js.
458 */
459 readVarint64ZigZag(): Long;
460 readVarint64ZigZag(offset: number): { value: Long; length: number };
461
462 /**
463 * Gets the number of remaining readable bytes. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit, so this returns limit - offset.
464 */
465 remaining(): number;
466
467 /**
468 * Resets this ByteBuffer's ByteBuffer#offset. If an offset has been marked through ByteBuffer#mark before, offset will be set to ByteBuffer#markedOffset, which will then be discarded. If no offset has been marked, sets offset = 0.
469 */
470 reset(): this;
471
472 /**
473 * Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that large or larger.
474 */
475 resize(capacity: number): this;
476
477 /**
478 * Reverses this ByteBuffer's contents
479 */
480 reverse(begin?: number, end?: number): this;
481
482 /**
483 * Skips the next length bytes. This will just advance
484 */
485 skip(length: number): this;
486
487 /**
488 * Slices this ByteBuffer by creating a cloned instance with offset = begin and limit = end.
489 */
490 slice(begin?: number, end?: number): ByteBuffer;
491
492 /**
493 * Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will transparently ByteBuffer#flip this ByteBuffer if offset > limit but the actual offsets remain untouched. This is an alias of ByteBuffer#toBuffer.
494 */
495 toArrayBuffer(forceCopy?: boolean): ArrayBuffer;
496
497 /**
498 * Encodes this ByteBuffer's contents to a base64 encoded string.
499 */
500 toBase64(begin?: number, end?: number): string;
501
502 /**
503 * Encodes this ByteBuffer to a binary encoded string, that is using only characters 0x00-0xFF as bytes.
504 */
505 toBinary(begin?: number, end?: number): string;
506
507 /**
508 * Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will transparently ByteBuffer#flip this ByteBuffer if offset > limit but the actual offsets remain untouched.
509 */
510 toBuffer(forceCopy?: boolean): Buffer;
511
512 /**
513 *Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:
514 * < : offset,
515 * ' : markedOffset,
516 * > : limit,
517 * | : offset and limit,
518 * [ : offset and markedOffset,
519 * ] : markedOffset and limit,
520 * ! : offset, markedOffset and limit
521 */
522 toDebug(columns?: boolean): string | Array<string>;
523
524 /**
525 * Encodes this ByteBuffer's contents to a hex encoded string.
526 */
527 toHex(begin?: number, end?: number): string;
528
529 /**
530 * Converts the ByteBuffer's contents to a string.
531 */
532 toString(encoding?: string): string;
533
534 /**
535 * Encodes this ByteBuffer's contents between ByteBuffer#offset and ByteBuffer#limit to an UTF8 encoded string.
536 */
537 toUTF8(): string;
538
539 /**
540 * Writes an 8bit signed integer. This is an alias of ByteBuffer#writeInt8.
541 */
542 writeByte(value: number, offset?: number): this;
543
544 /**
545 * Writes an array of bytes. This is an alias for append
546 */
547 writeBytes(
548 source: ByteBuffer | Buffer | ArrayBuffer | Uint8Array | string,
549 encoding?: string | number,
550 offset?: number,
551 ): this;
552
553 /**
554 * Writes a NULL-terminated UTF8 encoded string. For this to work the specified string must not contain any NULL characters itself.
555 */
556 writeCString(str: string, offset?: number): this;
557
558 /**
559 * Writes a 64bit float. This is an alias of ByteBuffer#writeFloat64.
560 */
561 writeDouble(value: number, offset?: number): this;
562
563 /**
564 * Writes a 32bit float. This is an alias of ByteBuffer#writeFloat32.
565 */
566 writeFloat(value: number, offset?: number): this;
567
568 /**
569 * Writes a 32bit float.
570 */
571 writeFloat32(value: number, offset?: number): this;
572
573 /**
574 * Writes a 64bit float.
575 */
576 writeFloat64(value: number, offset?: number): this;
577
578 /**
579 * Writes a length as uint32 prefixed UTF8 encoded string.
580 */
581 writeIString(str: string, offset?: number): this;
582
583 /**
584 * Writes a 32bit signed integer. This is an alias of ByteBuffer#writeInt32.
585 */
586 writeInt(value: number, offset?: number): this;
587
588 /**
589 * Writes a 16bit signed integer.
590 */
591 writeInt16(value: number, offset?: number): this;
592
593 /**
594 * Writes a 32bit signed integer.
595 */
596 writeInt32(value: number, offset?: number): this;
597
598 /**
599 * Writes a 64bit signed integer.
600 */
601 writeInt64(value: number | Long, offset?: number): this;
602
603 /**
604 * Writes an 8bit signed integer.
605 */
606 writeInt8(value: number, offset?: number): this;
607
608 /**
609 * Write a 64bit signed integer. This is an alias of ByteBuffer#writeInt64.
610 */
611 writeLong(value: number | Long, offset?: number): this;
612
613 /**
614 * Writes a 16bit signed integer. This is an alias of ByteBuffer#writeInt16.
615 */
616 writeShort(value: number, offset?: number): this;
617
618 /**
619 * Writes an UTF8 encoded string. This is an alias of ByteBuffer#writeUTF8String.
620 */
621 writeString(str: string): this;
622 writeString(str: string, offset: number): number;
623
624 /**
625 * Writes an UTF8 encoded string.
626 */
627 writeUTF8String(str: string): this;
628 writeUTF8String(str: string, offset?: number): number;
629
630 /**
631 * Writes a 16bit unsigned integer.
632 */
633 writeUint16(value: number, offset?: number): this;
634
635 /**
636 * Writes a 32bit unsigned integer.
637 */
638 writeUint32(value: number, offset?: number): this;
639
640 /**
641 * Writes a 64bit unsigned integer.
642 */
643 writeUint64(value: number | Long, offset?: number): this;
644
645 /**
646 * Writes an 8bit unsigned integer.
647 */
648 writeUint8(value: number, offset?: number): this;
649
650 /**
651 * Writes a length as varint32 prefixed UTF8 encoded string.
652 */
653 writeVString(str: string): this;
654 writeVString(str: string, offset: number): number;
655
656 /**
657 * Writes a 32bit base 128 variable-length integer.
658 */
659 writeVarint32(value: number): this;
660 writeVarint32(value: number, offset: number): number;
661
662 /**
663 * Writes a zig-zag encoded 32bit base 128 variable-length integer.
664 */
665 writeVarint32ZigZag(value: number): this;
666 writeVarint32ZigZag(value: number, offset: number): number;
667
668 /**
669 * Writes a 64bit base 128 variable-length integer.
670 */
671 writeVarint64(value: number | Long): this;
672 writeVarint64(value: number | Long, offset: number): number;
673
674 /**
675 * Writes a zig-zag encoded 64bit base 128 variable-length integer.
676 */
677 writeVarint64ZigZag(value: number | Long): this;
678 writeVarint64ZigZag(value: number | Long, offset: number): number;
679}