UNPKG

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