460
// that is using 4 bytes to encode a character).
461
for (let i = 0; i < bytesRead && i < ZERO_BYTE_DETECTION_BUFFER_MAX_LEN; i++) {
462
>
const isEndian = (i % 2 === 1); // assume 2-byte sequences typical for UTF-16
encoding.ts
463
>
const isZeroByte = (buffer.readUInt8(i) === 0);
464
>
465
>
if (isZeroByte) {
466
containsZeroByte = true;
467
}
469
>
// UTF-16 LE: expect e.g. 0xAA 0x00
470
>
if (couldBeUTF16LE && (isEndian && !isZeroByte || !isEndian && isZeroByte)) {
471
couldBeUTF16LE = false;
472
}
474
>
// UTF-16 BE: expect e.g. 0x00 0xAA
475
>
if (couldBeUTF16BE && (isEndian && isZeroByte || !isEndian && !isZeroByte)) {
476
couldBeUTF16BE = false;
477
}
479
>
// Return if this is neither UTF16-LE nor UTF16-BE and thus treat as binary
480
>
if (isZeroByte && !couldBeUTF16LE && !couldBeUTF16BE) {
481
break;
482
}
484
485
// Handle case of 0-byte included