"Split" ReaderException into subclasses to enable more useful error reporting
[zxing.git] / core / src / com / google / zxing / oned / Code39Reader.java
index 36b7446..c997ec6 100644 (file)
@@ -17,7 +17,9 @@
 package com.google.zxing.oned;
 
 import com.google.zxing.BarcodeFormat;
-import com.google.zxing.ReaderException;
+import com.google.zxing.ChecksumException;
+import com.google.zxing.FormatException;
+import com.google.zxing.NotFoundException;
 import com.google.zxing.Result;
 import com.google.zxing.ResultPoint;
 import com.google.zxing.common.BitArray;
@@ -88,7 +90,8 @@ public final class Code39Reader extends OneDReader {
     this.extendedMode = extendedMode;
   }
 
-  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints) throws ReaderException {
+  public Result decodeRow(int rowNumber, BitArray row, Hashtable hints)
+      throws NotFoundException, ChecksumException, FormatException {
 
     int[] start = findAsteriskPattern(row);
     int nextStart = start[1];
@@ -107,7 +110,7 @@ public final class Code39Reader extends OneDReader {
       recordPattern(row, nextStart, counters);
       int pattern = toNarrowWidePattern(counters);
       if (pattern < 0) {
-        throw ReaderException.getInstance();
+        throw NotFoundException.getNotFoundInstance();
       }
       decodedChar = patternToChar(pattern);
       result.append(decodedChar);
@@ -131,7 +134,7 @@ public final class Code39Reader extends OneDReader {
     // If 50% of last pattern size, following last pattern, is not whitespace, fail
     // (but if it's whitespace to the very end of the image, that's OK)
     if (nextStart != end && whiteSpaceAfterEnd / 2 < lastPatternSize) {
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
 
     if (usingCheckDigit) {
@@ -141,7 +144,7 @@ public final class Code39Reader extends OneDReader {
         total += ALPHABET_STRING.indexOf(result.charAt(i));
       }
       if (total % 43 != ALPHABET_STRING.indexOf(result.charAt(max))) {
-        throw ReaderException.getInstance();
+        throw ChecksumException.getChecksumInstance();
       }
       result.deleteCharAt(max);
     }
@@ -153,7 +156,7 @@ public final class Code39Reader extends OneDReader {
 
     if (resultString.length() == 0) {
       // Almost surely a false positive
-      throw ReaderException.getInstance();
+      throw NotFoundException.getNotFoundInstance();
     }
 
     float left = (float) (start[1] + start[0]) / 2.0f;
@@ -168,7 +171,7 @@ public final class Code39Reader extends OneDReader {
 
   }
 
-  private static int[] findAsteriskPattern(BitArray row) throws ReaderException {
+  private static int[] findAsteriskPattern(BitArray row) throws NotFoundException {
     int width = row.getSize();
     int rowOffset = 0;
     while (rowOffset < width) {
@@ -210,7 +213,7 @@ public final class Code39Reader extends OneDReader {
         isWhite = !isWhite;
       }
     }
-    throw ReaderException.getInstance();
+    throw NotFoundException.getNotFoundInstance();
   }
 
   // For efficiency, returns -1 on failure. Not throwing here saved as many as 700 exceptions
@@ -259,16 +262,16 @@ public final class Code39Reader extends OneDReader {
     return -1;
   }
 
-  private static char patternToChar(int pattern) throws ReaderException {
+  private static char patternToChar(int pattern) throws NotFoundException {
     for (int i = 0; i < CHARACTER_ENCODINGS.length; i++) {
       if (CHARACTER_ENCODINGS[i] == pattern) {
         return ALPHABET[i];
       }
     }
-    throw ReaderException.getInstance();
+    throw NotFoundException.getNotFoundInstance();
   }
 
-  private static String decodeExtended(String encoded) throws ReaderException {
+  private static String decodeExtended(String encoded) throws FormatException {
     int length = encoded.length();
     StringBuffer decoded = new StringBuffer(length);
     for (int i = 0; i < length; i++) {
@@ -282,7 +285,7 @@ public final class Code39Reader extends OneDReader {
             if (next >= 'A' && next <= 'Z') {
               decodedChar = (char) (next + 32);
             } else {
-              throw ReaderException.getInstance();
+              throw FormatException.getFormatInstance();
             }
             break;
           case '$':
@@ -290,7 +293,7 @@ public final class Code39Reader extends OneDReader {
             if (next >= 'A' && next <= 'Z') {
               decodedChar = (char) (next - 64);
             } else {
-              throw ReaderException.getInstance();
+              throw FormatException.getFormatInstance();
             }
             break;
           case '%':
@@ -300,7 +303,7 @@ public final class Code39Reader extends OneDReader {
             } else if (next >= 'F' && next <= 'W') {
               decodedChar = (char) (next - 11);
             } else {
-              throw ReaderException.getInstance();
+              throw FormatException.getFormatInstance();
             }
             break;
           case '/':
@@ -310,7 +313,7 @@ public final class Code39Reader extends OneDReader {
             } else if (next == 'Z') {
               decodedChar = ':';
             } else {
-              throw ReaderException.getInstance();
+              throw FormatException.getFormatInstance();
             }
             break;
         }