diff --git a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java index 44b545b13..f8b1b1ab5 100644 --- a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java +++ b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java @@ -176,24 +176,13 @@ final class PDF417HighLevelEncoder { } if (Compaction.TEXT == compaction) { - for (int i = 0; i < msg.length(); i++) { - if (msg.charAt(i) > 255) { - throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " + - (int) msg.charAt(i) + - "). Consider specifying Compaction.AUTO instead of Compaction.TEXT"); - } - } + checkCharset(msg,255,"Consider specifying Compaction.AUTO instead of Compaction.TEXT"); } if (encoding == null && !autoECI) { - for (int i = 0; i < msg.length(); i++) { - if (msg.charAt(i) > 255) { - throw new WriterException("Non-encodable character detected: " + msg.charAt(i) + " (Unicode: " + - (int) msg.charAt(i) + - "). Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET."); - } - } + checkCharset(msg,255,"Consider specifying EncodeHintType.PDF417_AUTO_ECI and/or EncodeTypeHint.CHARACTER_SET."); } + //the codewords 0..928 are encoded as Unicode characters StringBuilder sb = new StringBuilder(msg.length()); @@ -293,6 +282,22 @@ final class PDF417HighLevelEncoder { return sb.toString(); } + + /** + * Check if input is only made of characters between 0 and the upper limit + * @param input the input + * @param max the upper limit for charset + * @param errorMessage the message to explain the error + * @throws WriterException exception highlighting the offending character and a suggestion to avoid the error + */ + protected static void checkCharset(String input,int max, String errorMessage) throws WriterException { + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) > max) { + throw new WriterException("Non-encodable character detected: " + input.charAt(i) + " (Unicode: " + + (int) input.charAt(i) + "). " + errorMessage); + } + } + } /** * Encode parts of the message using Text Compaction as described in ISO/IEC 15438:2001(E), diff --git a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java index 99be2b06d..e4ded829c 100644 --- a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java @@ -18,6 +18,7 @@ package com.google.zxing.pdf417.encoder; import java.util.HashMap; import java.util.Map; +import java.util.UUID; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; @@ -78,6 +79,26 @@ public final class PDF417EncoderTestCase extends Assert { assertTrue(e.getMessage().contains("Compaction.AUTO")); } } + + @Test + public void testCheckCharset() throws Exception { + String input = "Hello!"; + String errorMessage = UUID.randomUUID().toString(); + + // no exception + PDF417HighLevelEncoder.checkCharset(input,255,errorMessage); + PDF417HighLevelEncoder.checkCharset(input,1255,errorMessage); + PDF417HighLevelEncoder.checkCharset(input,111,errorMessage); + + try { + // should throw an exception for character 'o' because it exceeds upper limit 110 + PDF417HighLevelEncoder.checkCharset(input,110,errorMessage); + } catch (WriterException e) { + assertNotNull(e.getMessage()); + assertTrue(e.getMessage().contains("111")); + assertTrue(e.getMessage().contains(errorMessage)); + } + } @Test public void testEncodeIso88591WithSpecialChars() throws Exception {