Minor changes from code inspection

This commit is contained in:
Sean Owen 2021-07-27 09:20:28 -05:00
parent 1499c92178
commit 0d08915628
6 changed files with 35 additions and 59 deletions

View file

@ -174,7 +174,7 @@ public final class CodaBarReader extends OneDReader {
// We break out of this loop in the middle, in order to handle // We break out of this loop in the middle, in order to handle
// inter-character spaces properly. // inter-character spaces properly.
int pos = start; int pos = start;
for (int i = 0; true; i++) { for (int i = 0; i <= end; i++) {
int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)]; int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)];
for (int j = 6; j >= 0; j--) { for (int j = 6; j >= 0; j--) {
// Even j = bars, while odd j = spaces. Categories 2 and 3 are for // Even j = bars, while odd j = spaces. Categories 2 and 3 are for
@ -184,9 +184,6 @@ public final class CodaBarReader extends OneDReader {
counts[category]++; counts[category]++;
pattern >>= 1; pattern >>= 1;
} }
if (i >= end) {
break;
}
// We ignore the inter-character space - it could be of any size. // We ignore the inter-character space - it could be of any size.
pos += 8; pos += 8;
} }
@ -206,7 +203,7 @@ public final class CodaBarReader extends OneDReader {
// Now verify that all of the stripes are within the thresholds. // Now verify that all of the stripes are within the thresholds.
pos = start; pos = start;
for (int i = 0; true; i++) { for (int i = 0; i <= end; i++) {
int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)]; int pattern = CHARACTER_ENCODINGS[decodeRowResult.charAt(i)];
for (int j = 6; j >= 0; j--) { for (int j = 6; j >= 0; j--) {
// Even j = bars, while odd j = spaces. Categories 2 and 3 are for // Even j = bars, while odd j = spaces. Categories 2 and 3 are for
@ -218,9 +215,6 @@ public final class CodaBarReader extends OneDReader {
} }
pattern >>= 1; pattern >>= 1;
} }
if (i >= end) {
break;
}
pos += 8; pos += 8;
} }
} }

View file

@ -26,13 +26,10 @@ final class ExpandedRow {
private final List<ExpandedPair> pairs; private final List<ExpandedPair> pairs;
private final int rowNumber; private final int rowNumber;
/** Did this row of the image have to be reversed (mirrored) to recognize the pairs? */
private final boolean wasReversed;
ExpandedRow(List<ExpandedPair> pairs, int rowNumber, boolean wasReversed) { ExpandedRow(List<ExpandedPair> pairs, int rowNumber) {
this.pairs = new ArrayList<>(pairs); this.pairs = new ArrayList<>(pairs);
this.rowNumber = rowNumber; this.rowNumber = rowNumber;
this.wasReversed = wasReversed;
} }
List<ExpandedPair> getPairs() { List<ExpandedPair> getPairs() {
@ -61,12 +58,12 @@ final class ExpandedRow {
return false; return false;
} }
ExpandedRow that = (ExpandedRow) o; ExpandedRow that = (ExpandedRow) o;
return this.pairs.equals(that.pairs) && wasReversed == that.wasReversed; return this.pairs.equals(that.pairs);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return pairs.hashCode() ^ Boolean.valueOf(wasReversed).hashCode(); return pairs.hashCode();
} }
} }

View file

@ -291,7 +291,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
return; return;
} }
this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber, false)); this.rows.add(insertPos, new ExpandedRow(this.pairs, rowNumber));
removePartialRows(this.pairs, this.rows); removePartialRows(this.pairs, this.rows);
} }

View file

@ -182,16 +182,16 @@ final class DecodedBitStreamParser {
// Decoding the fileId codewords as 0-899 numbers, each 0-filled to width 3. This follows the spec // Decoding the fileId codewords as 0-899 numbers, each 0-filled to width 3. This follows the spec
// (See ISO/IEC 15438:2015 Annex H.6) and preserves all info, but some generators (e.g. TEC-IT) write // (See ISO/IEC 15438:2015 Annex H.6) and preserves all info, but some generators (e.g. TEC-IT) write
// the fileId using text compaction, so in those cases the fileId will appear mangled. // the fileId using text compaction, so in those cases the fileId will appear mangled.
String fileId = ""; StringBuilder fileId = new StringBuilder();
for (int i = 0; codeIndex < codewords[0] && codewords[codeIndex] != MACRO_PDF417_TERMINATOR for (int i = 0; codeIndex < codewords[0] && codewords[codeIndex] != MACRO_PDF417_TERMINATOR
&& codewords[codeIndex] != BEGIN_MACRO_PDF417_OPTIONAL_FIELD; i++, codeIndex++) { && codewords[codeIndex] != BEGIN_MACRO_PDF417_OPTIONAL_FIELD; i++, codeIndex++) {
fileId += String.format("%03d", codewords[codeIndex]); fileId.append(String.format("%03d", codewords[codeIndex]));
} }
if (fileId.length() == 0) { if (fileId.length() == 0) {
// at least one fileId codeword is required (Annex H.2) // at least one fileId codeword is required (Annex H.2)
throw FormatException.getFormatInstance(); throw FormatException.getFormatInstance();
} }
resultMetadata.setFileId(fileId); resultMetadata.setFileId(fileId.toString());
int optionalFieldsStart = -1; int optionalFieldsStart = -1;
if (codewords[codeIndex] == BEGIN_MACRO_PDF417_OPTIONAL_FIELD) { if (codewords[codeIndex] == BEGIN_MACRO_PDF417_OPTIONAL_FIELD) {

View file

@ -425,8 +425,17 @@ public final class EncoderTest extends Assert {
"...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X......."); "...X. XXXXX ..X.. X....... ..X.XXX. ..X..... X.......");
} }
@Test @Test(expected = IllegalArgumentException.class)
public void testUserSpecifiedLayers() { public void testUserSpecifiedLayers() {
doTestUserSpecifiedLayers(33);
}
@Test(expected = IllegalArgumentException.class)
public void testUserSpecifiedLayers2() {
doTestUserSpecifiedLayers(-1);
}
private void doTestUserSpecifiedLayers(int userSpecifiedLayers) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
AztecCode aztec = Encoder.encode(alphabet, 25, -2); AztecCode aztec = Encoder.encode(alphabet, 25, -2);
assertEquals(2, aztec.getLayers()); assertEquals(2, aztec.getLayers());
@ -436,19 +445,17 @@ public final class EncoderTest extends Assert {
assertEquals(32, aztec.getLayers()); assertEquals(32, aztec.getLayers());
assertFalse(aztec.isCompact()); assertFalse(aztec.isCompact());
try { Encoder.encode(alphabet, 25, userSpecifiedLayers);
Encoder.encode(alphabet, 25, 33); }
fail("Encode should have failed. No such thing as 33 layers");
} catch (IllegalArgumentException expected) {
// continue
}
try { @Test(expected = IllegalArgumentException.class)
Encoder.encode(alphabet, 25, -1); public void testBorderCompact4CaseFailed() {
fail("Encode should have failed. Text can't fit in 1-layer compact"); // Compact(4) con hold 608 bits of information, but at most 504 can be data. Rest must
} catch (IllegalArgumentException expected) { // be error correction
// continue String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
} // encodes as 26 * 5 * 4 = 520 bits of data
String alphabet4 = alphabet + alphabet + alphabet + alphabet;
Encoder.encode(alphabet4, 0, -4);
} }
@Test @Test
@ -458,12 +465,6 @@ public final class EncoderTest extends Assert {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// encodes as 26 * 5 * 4 = 520 bits of data // encodes as 26 * 5 * 4 = 520 bits of data
String alphabet4 = alphabet + alphabet + alphabet + alphabet; String alphabet4 = alphabet + alphabet + alphabet + alphabet;
try {
Encoder.encode(alphabet4, 0, -4);
fail("Encode should have failed. Text can't fit in 1-layer compact");
} catch (IllegalArgumentException expected) {
// continue
}
// If we just try to encode it normally, it will go to a non-compact 4 layer // If we just try to encode it normally, it will go to a non-compact 4 layer
AztecCode aztecCode = Encoder.encode(alphabet4, 0, Encoder.DEFAULT_AZTEC_LAYERS); AztecCode aztecCode = Encoder.encode(alphabet4, 0, Encoder.DEFAULT_AZTEC_LAYERS);

View file

@ -155,40 +155,24 @@ public class PDF417DecoderTestCase extends Assert {
assertNull(resultMetadata.getOptionalData()); assertNull(resultMetadata.getOptionalData());
} }
@Test @Test(expected = FormatException.class)
public void testSampleWithBadSequenceIndexMacro() throws FormatException { public void testSampleWithBadSequenceIndexMacro() throws FormatException {
int[] sampleCodes = {3, 928, 222, 0}; int[] sampleCodes = {3, 928, 222, 0};
PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata(); PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata();
DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata);
try {
DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata);
} catch (FormatException expected) {
// continue
}
} }
@Test @Test(expected = FormatException.class)
public void testSampleWithNoFileIdMacro() throws FormatException { public void testSampleWithNoFileIdMacro() throws FormatException {
int[] sampleCodes = {4, 928, 222, 198, 0}; int[] sampleCodes = {4, 928, 222, 198, 0};
PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata(); PDF417ResultMetadata resultMetadata = new PDF417ResultMetadata();
DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata);
try {
DecodedBitStreamParser.decodeMacroBlock(sampleCodes, 2, resultMetadata);
} catch (FormatException expected) {
// continue
}
} }
@Test @Test(expected = FormatException.class)
public void testSampleWithNoDataNoMacro() throws FormatException { public void testSampleWithNoDataNoMacro() throws FormatException {
int[] sampleCodes = {3, 899, 899, 0}; int[] sampleCodes = {3, 899, 899, 0};
DecodedBitStreamParser.decode(sampleCodes, "0");
try {
DecodedBitStreamParser.decode(sampleCodes, "0");
} catch (FormatException expected) {
// continue
}
} }
} }