449
let seemsBinary = false;
450
if (encoding !== UTF16be && encoding !== UTF16le && buffer) {
451
>
let couldBeUTF16LE = true; // e.g. 0xAA 0x00
encoding.ts
452
>
let couldBeUTF16BE = true; // e.g. 0x00 0xAA
453
>
let containsZeroByte = false;
454
>
455
>
// This is a simplified guess to detect UTF-16 BE or LE by just checking if
456
>
// the first 512 bytes have the 0-byte at a specific location. For UTF-16 LE
457
>
// this would be the odd byte index and for UTF-16 BE the even one.
458
>
// Note: this can produce false positives (a binary file that uses a 2-byte
459
>
// encoding of the same format as UTF-16) and false negatives (a UTF-16 file
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
463
const isZeroByte = (buffer.readUInt8(i) === 0);