Detect big endian as well

This commit is contained in:
M66B 2022-06-11 11:05:50 +02:00
parent 6dca1355a3
commit 46bf44fe03
1 changed files with 10 additions and 1 deletions

View File

@ -133,6 +133,7 @@ public class CharsetHelper {
}
static Boolean isUTF16LE(BufferedInputStream bis) throws IOException {
// https://en.wikipedia.org/wiki/Endianness
byte[] bytes = new byte[64];
bis.mark(bytes.length);
try {
@ -153,7 +154,15 @@ public class CharsetHelper {
even++;
else
odd++;
return (even < 30 * count / 100 / 2 && odd > 70 * count / 100 / 2);
int low = 30 * count / 100 / 2;
int high = 70 * count / 100 / 2;
if (even < low && odd > high)
return true; // Little endian
if (odd < low && even > high)
return false; // Big endian
return null; // Undetermined
} finally {
bis.reset();
}