"Split" ReaderException into subclasses to enable more useful error reporting
[zxing.git] / core / src / com / google / zxing / pdf417 / decoder / Decoder.java
index fb722b6..1936c5a 100644 (file)
@@ -16,7 +16,9 @@
 \r
 package com.google.zxing.pdf417.decoder;\r
 \r
-import com.google.zxing.ReaderException;\r
+import com.google.zxing.ChecksumException;\r
+import com.google.zxing.FormatException;\r
+import com.google.zxing.NotFoundException;\r
 import com.google.zxing.common.BitMatrix;\r
 import com.google.zxing.common.DecoderResult;\r
 //import com.google.zxing.pdf417.reedsolomon.ReedSolomonDecoder;\r
@@ -44,9 +46,9 @@ public final class Decoder {
    *\r
    * @param image booleans representing white/black PDF417 modules\r
    * @return text and bytes encoded within the PDF417 Code\r
-   * @throws ReaderException if the PDF417 Code cannot be decoded\r
+   * @throws NotFoundException if the PDF417 Code cannot be decoded\r
    */\r
-  public DecoderResult decode(boolean[][] image) throws ReaderException {\r
+  public DecoderResult decode(boolean[][] image) throws FormatException {\r
     int dimension = image.length;\r
     BitMatrix bits = new BitMatrix(dimension);\r
     for (int i = 0; i < dimension; i++) {\r
@@ -65,14 +67,14 @@ public final class Decoder {
    *\r
    * @param bits booleans representing white/black PDF417 Code modules\r
    * @return text and bytes encoded within the PDF417 Code\r
-   * @throws ReaderException if the PDF417 Code cannot be decoded\r
+   * @throws FormatException if the PDF417 Code cannot be decoded\r
    */\r
-  public DecoderResult decode(BitMatrix bits) throws ReaderException {\r
+  public DecoderResult decode(BitMatrix bits) throws FormatException {\r
     // Construct a parser to read the data codewords and error-correction level\r
     BitMatrixParser parser = new BitMatrixParser(bits);\r
     int[] codewords = parser.readCodewords();\r
     if (codewords == null || codewords.length == 0) {\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
 \r
     int ecLevel = parser.getECLevel();\r
@@ -91,27 +93,27 @@ public final class Decoder {
    *\r
    * @param codewords\r
    * @return an index to the first data codeword.\r
-   * @throws ReaderException\r
+   * @throws FormatException\r
    */\r
-  private static void verifyCodewordCount(int[] codewords, int numECCodewords) throws ReaderException {\r
+  private static void verifyCodewordCount(int[] codewords, int numECCodewords) throws FormatException {\r
     if (codewords.length < 4) {\r
       // Codeword array size should be at least 4 allowing for\r
       // Count CW, At least one Data CW, Error Correction CW, Error Correction CW\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
     // The first codeword, the Symbol Length Descriptor, shall always encode the total number of data\r
     // codewords in the symbol, including the Symbol Length Descriptor itself, data codewords and pad\r
     // codewords, but excluding the number of error correction codewords.\r
     int numberOfCodewords = codewords[0];\r
     if (numberOfCodewords > codewords.length) {\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
     if (numberOfCodewords == 0) {\r
       // Reset to the length of the array - 8 (Allow for at least level 3 Error Correction (8 Error Codewords)\r
       if (numECCodewords < codewords.length) {\r
         codewords[0] = codewords.length - numECCodewords;\r
       } else {\r
-        throw ReaderException.getInstance();\r
+        throw FormatException.getFormatInstance();\r
       }\r
     }\r
   }\r
@@ -121,15 +123,16 @@ public final class Decoder {
    * correct the errors in-place using Reed-Solomon error correction.</p>\r
    *\r
    * @param codewords   data and error correction codewords\r
-   * @throws ReaderException if error correction fails\r
+   * @throws ChecksumException if error correction fails\r
    */\r
-  private static int correctErrors(int[] codewords, int[] erasures, int numECCodewords) throws ReaderException {\r
+  private static int correctErrors(int[] codewords, int[] erasures, int numECCodewords) throws FormatException {\r
     if ((erasures != null && erasures.length > numECCodewords / 2 + MAX_ERRORS) ||\r
         (numECCodewords < 0 || numECCodewords > MAX_EC_CODEWORDS)) {\r
       // Too many errors or EC Codewords is corrupted\r
-      throw ReaderException.getInstance();\r
+      throw FormatException.getFormatInstance();\r
     }\r
     // Try to correct the errors\r
+    // TODO enable error correction\r
     int result = 0; // rsDecoder.correctErrors(codewords, numECCodewords);\r
     if (erasures != null) {\r
       int numErasures = erasures.length;\r
@@ -138,7 +141,7 @@ public final class Decoder {
       }\r
       if (numErasures > MAX_ERRORS) {\r
         // Still too many errors\r
-        throw ReaderException.getInstance();\r
+        throw FormatException.getFormatInstance();\r
       }\r
     }\r
     return result;\r