summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/npm-registry-fetch/node_modules/smart-buffer/build/smartbuffer.js
blob: ef53b9fd4d063d292e4cd06a128d8313e33b7a88 (plain)
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
"use strict";
// The default Buffer size if one is not provided.
const DEFAULT_SMARTBUFFER_SIZE = 4096;
// The default string encoding to use for reading/writing strings.
const DEFAULT_SMARTBUFFER_ENCODING = 'utf8';
class SmartBuffer {
    /**
     * Creates a new SmartBuffer instance.
     *
     * @param arg1 { Number | BufferEncoding | Buffer | SmartBufferOptions }
     * @param arg2 { BufferEncoding }
     */
    constructor(arg1, arg2) {
        this.length = 0;
        this.encoding = DEFAULT_SMARTBUFFER_ENCODING;
        this.writeOffset = 0;
        this.readOffset = 0;
        // Initial buffer size provided
        if (typeof arg1 === 'number') {
            if (Number.isFinite(arg1) && Number.isInteger(arg1) && arg1 > 0) {
                this.buff = Buffer.allocUnsafe(arg1);
            }
            else {
                throw new Error('Invalid size provided. Size must be a valid integer greater than zero.');
            }
        }
        else if (typeof arg1 === 'string') {
            if (Buffer.isEncoding(arg1)) {
                this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
                this.encoding = arg1;
            }
            else {
                throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
            }
        }
        else if (arg1 instanceof Buffer) {
            this.buff = arg1;
            this.length = arg1.length;
        }
        else if (SmartBuffer.isSmartBufferOptions(arg1)) {
            // Checks for encoding
            if (arg1.encoding) {
                if (Buffer.isEncoding(arg1.encoding)) {
                    this.encoding = arg1.encoding;
                }
                else {
                    throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
                }
            }
            // Checks for initial size length
            if (arg1.size) {
                if (Number.isFinite(arg1.size) && Number.isInteger(arg1.size) && arg1.size > 0) {
                    this.buff = Buffer.allocUnsafe(arg1.size);
                }
                else {
                    throw new Error('Invalid size provided. Size must be a valid integer greater than zero.');
                }
            }
            else if (arg1.buff) {
                if (arg1.buff instanceof Buffer) {
                    this.buff = arg1.buff;
                    this.length = arg1.buff.length;
                }
                else {
                    throw new Error('Invalid buffer provided in SmartBufferOptions.');
                }
            }
            else {
                this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
            }
        }
        else if (typeof arg1 === 'object') {
            throw new Error('Invalid object supplied to SmartBuffer constructor.');
        }
        else {
            this.buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
        }
        // Check for encoding (Buffer, Encoding) constructor.
        if (typeof arg2 === 'string') {
            if (Buffer.isEncoding(arg2)) {
                this.encoding = arg2;
            }
            else {
                throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
            }
        }
    }
    /**
     * Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
     *
     * @param size { Number } The size of the internal Buffer.
     * @param encoding { String } The BufferEncoding to use for strings.
     *
     * @return { SmartBuffer }
     */
    static fromSize(size, encoding) {
        return new this({
            size: size,
            encoding: encoding
        });
    }
    /**
     * Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
     *
     * @param buffer { Buffer } The Buffer to use as the internal Buffer value.
     * @param encoding { String } The BufferEncoding to use for strings.
     *
     * @return { SmartBuffer }
     */
    static fromBuffer(buff, encoding) {
        return new this({
            buff: buff,
            encoding: encoding
        });
    }
    /**
     * Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
     *
     * @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
     */
    static fromOptions(options) {
        return new this(options);
    }
    /**
     * Ensures that the internal Buffer is large enough to write data.
     *
     * @param minLength { Number } The minimum length of the data that needs to be written.
     * @param offset { Number } The offset of the data to be written.
     */
    ensureWriteable(minLength, offset) {
        const offsetVal = typeof offset === 'number' ? offset : 0;
        // Ensure there is enough internal Buffer capacity.
        this.ensureCapacity(this.length + minLength + offsetVal);
        // If offset is provided, copy data into appropriate location in regards to the offset.
        if (typeof offset === 'number') {
            this.buff.copy(this.buff, offsetVal + minLength, offsetVal, this.buff.length);
        }
        // Adjust instance length.
        this.length = Math.max(this.length + minLength, offsetVal + minLength);
    }
    /**
     * Ensures that the internal Buffer is large enough to write at least the given amount of data.
     *
     * @param minLength { Number } The minimum length of the data needs to be written.
     */
    ensureCapacity(minLength) {
        const oldLength = this.buff.length;
        if (minLength > oldLength) {
            let data = this.buff;
            let newLength = (oldLength * 3) / 2 + 1;
            if (newLength < minLength) {
                newLength = minLength;
            }
            this.buff = Buffer.allocUnsafe(newLength);
            data.copy(this.buff, 0, 0, oldLength);
        }
    }
    /**
     * Reads a numeric number value using the provided function.
     *
     * @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
     * @param byteSize { Number } The number of bytes read.
     *
     * @param { Number }
     */
    readNumberValue(func, byteSize) {
        // Call Buffer.readXXXX();
        const value = func.call(this.buff, this.readOffset);
        // Adjust internal read offset
        this.readOffset += byteSize;
        return value;
    }
    /**
     * Writes a numeric number value using the provided function.
     *
     * @param func { Function(offset: number, offset?) => number} The function to write data on the internal Buffer with.
     * @param byteSize { Number } The number of bytes written.
     * @param value { Number } The number value to write.
     * @param offset { Number } the offset to write the number at.
     *
     */
    writeNumberValue(func, byteSize, value, offset) {
        const offsetVal = typeof offset === 'number' ? offset : this.writeOffset;
        // Ensure there is enough internal Buffer capacity. (raw offset is passed)
        this.ensureWriteable(byteSize, offset);
        // Call buffer.writeXXXX();
        func.call(this.buff, value, offsetVal);
        // Adjusts internal write offset
        this.writeOffset += byteSize;
    }
    // Signed integers
    /**
     * Reads an Int8 value from the current read position.
     *
     * @return { Number }
     */
    readInt8() {
        return this.readNumberValue(Buffer.prototype.readUInt8, 1);
    }
    /**
     * Reads an Int16BE value from the current read position.
     *
     * @return { Number }
     */
    readInt16BE() {
        return this.readNumberValue(Buffer.prototype.readUInt16BE, 2);
    }
    /**
     * Reads an Int16LE value from the current read position.
     *
     * @return { Number }
     */
    readInt16LE() {
        return this.readNumberValue(Buffer.prototype.readUInt16LE, 2);
    }
    /**
     * Reads an Int32BE value from the current read position.
     *
     * @return { Number }
     */
    readInt32BE() {
        return this.readNumberValue(Buffer.prototype.readUInt32BE, 4);
    }
    /**
     * Reads an Int32LE value from the current read position.
     *
     * @return { Number }
     */
    readInt32LE() {
        return this.readNumberValue(Buffer.prototype.readUInt32LE, 4);
    }
    /**
     * Writes an Int8 value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeInt8(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
        return this;
    }
    /**
     * Writes an Int16BE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeInt16BE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
        return this;
    }
    /**
     * Writes an Int16LE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeInt16LE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
        return this;
    }
    /**
     * Writes an Int32BE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeInt32BE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
        return this;
    }
    /**
     * Writes an Int32LE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeInt32LE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
        return this;
    }
    // Unsigned Integers
    /**
     * Reads an UInt8 value from the current read position.
     *
     * @return { Number }
     */
    readUInt8() {
        return this.readNumberValue(Buffer.prototype.readUInt8, 1);
    }
    /**
     * Reads an UInt16BE value from the current read position.
     *
     * @return { Number }
     */
    readUInt16BE() {
        return this.readNumberValue(Buffer.prototype.readUInt16BE, 2);
    }
    /**
     * Reads an UInt16LE value from the current read position.
     *
     * @return { Number }
     */
    readUInt16LE() {
        return this.readNumberValue(Buffer.prototype.readUInt16LE, 2);
    }
    /**
     * Reads an UInt32BE value from the current read position.
     *
     * @return { Number }
     */
    readUInt32BE() {
        return this.readNumberValue(Buffer.prototype.readUInt32BE, 4);
    }
    /**
     * Reads an UInt32LE value from the current read position.
     *
     * @return { Number }
     */
    readUInt32LE() {
        return this.readNumberValue(Buffer.prototype.readUInt32LE, 4);
    }
    /**
     * Writes an UInt8 value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeUInt8(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
        return this;
    }
    /**
     * Writes an UInt16BE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeUInt16BE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
        return this;
    }
    /**
     * Writes an UInt16LE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeUInt16LE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
        return this;
    }
    /**
     * Writes an UInt32BE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeUInt32BE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
        return this;
    }
    /**
     * Writes an UInt32LE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeUInt32LE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
        return this;
    }
    // Floating Point
    /**
     * Reads an FloatBE value from the current read position.
     *
     * @return { Number }
     */
    readFloatBE() {
        return this.readNumberValue(Buffer.prototype.readFloatBE, 4);
    }
    /**
     * Reads an FloatLE value from the current read position.
     *
     * @return { Number }
     */
    readFloatLE() {
        return this.readNumberValue(Buffer.prototype.readFloatLE, 4);
    }
    /**
     * Writes a FloatBE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeFloatBE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
        return this;
    }
    /**
     * Writes a FloatLE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeFloatLE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
        return this;
    }
    // Double Floating Point
    /**
     * Reads an DoublEBE value from the current read position.
     *
     * @return { Number }
     */
    readDoubleBE() {
        return this.readNumberValue(Buffer.prototype.readDoubleBE, 8);
    }
    /**
     * Reads an DoubleLE value from the current read position.
     *
     * @return { Number }
     */
    readDoubleLE() {
        return this.readNumberValue(Buffer.prototype.readDoubleLE, 8);
    }
    /**
     * Writes a DoubleBE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeDoubleBE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
        return this;
    }
    /**
     * Writes a DoubleLE value to the current write position (or at optional offset).
     *
     * @param value { Number } The value to write.
     * @param offset { Number } The offset to write the value at.
     *
     * @return this
     */
    writeDoubleLE(value, offset) {
        this.writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
        return this;
    }
    // Strings
    /**
     * Reads a String from the current read position.
     *
     * @param length { Number } The number of bytes to read as a String.
     * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
     *
     * @return { String }
     */
    readString(length, encoding) {
        const lengthVal = Math.min(length, this.length - this.readOffset) || this.length - this.readOffset;
        const value = this.buff.slice(this.readOffset, this.readOffset + lengthVal).toString(encoding || this.encoding);
        this.readOffset += lengthVal;
        return value;
    }
    /**
     * Writes a String to the current write position.
     *
     * @param value { String } The String value to write.
     * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
     * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
     */
    writeString(value, arg2, encoding) {
        let offsetVal = this.writeOffset;
        let encodingVal = this.encoding;
        // Check for offset
        if (typeof arg2 === 'number') {
            offsetVal = arg2;
        }
        else if (typeof arg2 === 'string') {
            if (Buffer.isEncoding(arg2)) {
                encodingVal = arg2;
            }
            else {
                throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
            }
        }
        // Check for encoding (third param)
        if (typeof encoding === 'string') {
            if (Buffer.isEncoding(encoding)) {
                encodingVal = encoding;
            }
            else {
                throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
            }
        }
        // Calculate bytelength of string.
        const byteLength = Buffer.byteLength(value, encodingVal);
        // Ensure there is enough internal Buffer capacity.
        this.ensureWriteable(byteLength, offsetVal);
        // Write value
        this.buff.write(value, offsetVal, byteLength, encodingVal);
        // Increment internal Buffer write offset;
        this.writeOffset += byteLength;
        return this;
    }
    /**
     * Reads a null-terminated String from the current read position.
     *
     * @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
     *
     * @return { String }
     */
    readStringNT(encoding) {
        // Set null character position to the end SmartBuffer instance.
        let nullPos = this.length;
        // Find next null character (if one is not found, default from above is used)
        for (let i = this.readOffset; i < this.length; i++) {
            if (this.buff[i] === 0x00) {
                nullPos = i;
                break;
            }
        }
        // Read string value
        const value = this.buff.slice(this.readOffset, nullPos);
        // Increment internal Buffer read offset
        this.readOffset = nullPos + 1;
        return value.toString(encoding || this.encoding);
    }
    /**
     * Writes a null-terminated String to the current write position.
     *
     * @param value { String } The String value to write.
     * @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
     * @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
     */
    writeStringNT(value, offset, encoding) {
        // Write Values
        this.writeString(value, offset, encoding);
        this.writeUInt8(0x00, (typeof offset === 'number' ? offset + value.length : this.writeOffset));
    }
    // Buffers
    /**
     * Reads a Buffer from the internal read position.
     *
     * @param length { Number } The length of data to read as a Buffer.
     *
     * @return { Buffer }
     */
    readBuffer(length) {
        const lengthVal = typeof length === 'number' ? length : this.length;
        const endPoint = Math.min(this.length, this.readOffset + lengthVal);
        // Read buffer value
        const value = this.buff.slice(this.readOffset, endPoint);
        // Increment internal Buffer read offset
        this.readOffset = endPoint;
        return value;
    }
    /**
     * Writes a Buffer to the current write position.
     *
     * @param value { Buffer } The Buffer to write.
     * @param offset { Number } The offset to write the Buffer to.
     */
    writeBuffer(value, offset) {
        const offsetVal = typeof offset === 'number' ? offset : this.writeOffset;
        // Ensure there is enough internal Buffer capacity.
        this.ensureWriteable(value.length, offsetVal);
        // Write buffer value
        value.copy(this.buff, offsetVal);
        // Increment internal Buffer write offset
        this.writeOffset += value.length;
        return this;
    }
    /**
     * Reads a null-terminated Buffer from the current read poisiton.
     *
     * @return { Buffer }
     */
    readBufferNT() {
        // Set null character position to the end SmartBuffer instance.
        let nullPos = this.length;
        // Find next null character (if one is not found, default from above is used)
        for (let i = this.readOffset; i < this.length; i++) {
            if (this.buff[i] === 0x00) {
                nullPos = i;
                break;
            }
        }
        // Read value
        const value = this.buff.slice(this.readOffset, nullPos);
        // Increment internal Buffer read offset
        this.readOffset = nullPos + 1;
        return value;
    }
    /**
     * Writes a null-terminated Buffer to the current write position.
     *
     * @param value { Buffer } The Buffer to write.
     * @param offset { Number } The offset to write the Buffer to.
     */
    writeBufferNT(value, offset) {
        // Write Values
        this.writeBuffer(value, offset);
        this.writeUInt8(0, (typeof offset === 'number' ? offset + value.length : this.writeOffset));
        return this;
    }
    /**
     * Clears the SmartBuffer instance to its original empty state.
     */
    clear() {
        this.writeOffset = 0;
        this.readOffset = 0;
        this.length = 0;
    }
    /**
     * Gets the remaining data left to be read from the SmartBuffer instance.
     *
     * @return { Number }
     */
    remaining() {
        return this.length - this.readOffset;
    }
    /**
     * Moves the read offset forward.
     *
     * @param amount { Number } The amount to move the read offset forward by.
     */
    skip(amount) {
        if (this.readOffset + amount > this.length) {
            throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
        }
        this.readOffset += amount;
    }
    /**
     * Moves the read offset backwards.
     *
     * @param amount { Number } The amount to move the read offset backwards by.
     */
    rewind(amount) {
        if (this.readOffset - amount < 0) {
            throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
        }
        this.readOffset -= amount;
    }
    /**
     * Moves the read offset to a specific position.
     *
     * @param position { Number } The position to move the read offset to.
     */
    skipTo(position) {
        this.moveTo(position);
    }
    /**
     * Moves the read offset to a specific position.
     *
     * @param position { Number } The position to move the read offset to.
     */
    moveTo(position) {
        if (position > this.length) {
            throw new Error('Target position is beyond the bounds of the SmartBuffer size.');
        }
        this.readOffset = position;
    }
    /**
     * Gets the value of the internal managed Buffer
     *
     * @param { Buffer }
     */
    toBuffer() {
        return this.buff.slice(0, this.length);
    }
    /**
     * Gets the String value of the internal managed Buffer
     *
     * @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
     */
    toString(encoding) {
        const encodingVal = typeof encoding === 'string' ? encoding : this.encoding;
        if (Buffer.isEncoding(encodingVal)) {
            return this.buff.toString(encodingVal, 0, this.length);
        }
        else {
            throw new Error('Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.');
        }
    }
    /**
     * Destroys the SmartBuffer instance.
     */
    destroy() {
        this.clear();
    }
    /**
     * Type checking function that determines if an object is a SmartBufferOptions object.
     */
    static isSmartBufferOptions(options) {
        const castOptions = options;
        return castOptions && (castOptions.encoding !== undefined || castOptions.size !== undefined || castOptions.buff !== undefined);
    }
}
module.exports = SmartBuffer;
//# sourceMappingURL=smartbuffer.js.map