Fixed UTF-8 detection, again, and added unit test

git-svn-id: https://zxing.googlecode.com/svn/trunk@204 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-02-15 16:21:55 +00:00
parent 9c6a626b90
commit febdcf8d15
3 changed files with 13 additions and 8 deletions

View file

@ -208,28 +208,31 @@ final class DecodedBitStreamParser {
// If we see something else in that second byte, we'll make the risky guess // If we see something else in that second byte, we'll make the risky guess
// that it's UTF-8. // that it's UTF-8.
int length = bytes.length; int length = bytes.length;
boolean canBeISO88591 = true;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int value = bytes[i] & 0xFF; int value = bytes[i] & 0xFF;
if (value >= 0x80 && value <= 0x9F && i < length - 1) { if (value >= 0x80 && value <= 0x9F && i < length - 1) {
canBeISO88591 = false;
// ISO-8859-1 shouldn't use this, but before we decide it is Shift_JIS, // ISO-8859-1 shouldn't use this, but before we decide it is Shift_JIS,
// just double check that it is followed by a byte that's valid in // just double check that it is followed by a byte that's valid in
// the Shift_JIS encoding // the Shift_JIS encoding
int nextValue = bytes[i + 1] & 0xFF; int nextValue = bytes[i + 1] & 0xFF;
if ((value & 0x1) == 0) { if ((value & 0x1) == 0) {
// if even, // if even, next value should be in [0x9F,0xFC]
if (nextValue >= 0x9F && nextValue <= 0xFC) { // if not, we'll guess UTF-8
return SHIFT_JIS; if (nextValue < 0x9F || nextValue > 0xFC) {
return UTF8;
} }
} else { } else {
if (nextValue >= 0x40 && nextValue <= 0x9E) { // if odd, next value should be in [0x40,0x9E]
return SHIFT_JIS; // if not, we'll guess UTF-8
if (nextValue < 0x40 || nextValue > 0x9E) {
return UTF8;
} }
} }
// otherwise we're going to take a guess that it's UTF-8
return UTF8;
} }
} }
return ISO88591; return canBeISO88591 ? ISO88591 : SHIFT_JIS;
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

View file

@ -0,0 +1,2 @@
ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP
árvíztűrő tükörfúrógép