Correct exception handling in certain situations so that routine decoding failures do not produce IllegalArgumentException

git-svn-id: https://zxing.googlecode.com/svn/trunk@799 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-12-21 18:40:41 +00:00
parent 031ceb7e69
commit fa59c4e093
2 changed files with 13 additions and 12 deletions

View file

@ -34,7 +34,7 @@ final class DataBlock {
}
/**
* <p>When QR Codes use multiple data blocks, they are actually interleave the bytes of each of them.
* <p>When QR Codes use multiple data blocks, they are actually interleaved.
* That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
* method will separate the data into original blocks.</p>
*
@ -47,6 +47,11 @@ final class DataBlock {
static DataBlock[] getDataBlocks(byte[] rawCodewords,
Version version,
ErrorCorrectionLevel ecLevel) {
if (rawCodewords.length != version.getTotalCodewords()) {
throw new IllegalArgumentException();
}
// Figure out the number and size of data blocks used by this version and
// error correction level
Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
@ -79,9 +84,6 @@ final class DataBlock {
if (numCodewords == shorterBlocksTotalCodewords) {
break;
}
if (numCodewords != shorterBlocksTotalCodewords + 1) {
throw new IllegalArgumentException("Data block sizes differ by more than 1");
}
longerBlocksStartAt--;
}
longerBlocksStartAt++;
@ -107,11 +109,6 @@ final class DataBlock {
result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
}
}
if (rawCodewordsOffset != rawCodewords.length) {
throw new IllegalArgumentException();
}
return result;
}

View file

@ -70,7 +70,11 @@ final class DecodedBitStreamParser {
// OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
mode = Mode.TERMINATOR;
} else {
mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
try {
mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
} catch (IllegalArgumentException iae) {
throw ReaderException.getInstance();
}
}
if (!mode.equals(Mode.TERMINATOR)) {
if (mode.equals(Mode.FNC1_FIRST_POSITION) || mode.equals(Mode.FNC1_SECOND_POSITION)) {
@ -78,11 +82,11 @@ final class DecodedBitStreamParser {
fc1InEffect = true;
} else if (mode.equals(Mode.ECI)) {
// Count doesn't apply to ECI
int value = parseECIValue(bits);
try {
int value = parseECIValue(bits);
currentCharacterSetECI = CharacterSetECI.getCharacterSetECIByValue(value);
} catch (IllegalArgumentException iae) {
// unsupported... just continue?
throw ReaderException.getInstance();
}
} else {
// How many characters will follow, encoded in this mode?