diff --git a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java index d6794ca..368cab3 100644 --- a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java @@ -43,16 +43,21 @@ public class AlphaParseInfo extends AlphaNumericFieldParseInfo { throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d", type, length, pos), pos); } - if (custom == null) { - return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); - } else { - @SuppressWarnings({"unchecked", "rawtypes"}) - IsoValue v = new IsoValue(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom); - if (v.getValue() == null) { - return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); - } - return v; - } + try { + if (custom == null) { + return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); + } else { + @SuppressWarnings({"unchecked", "rawtypes"}) + IsoValue v = new IsoValue(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom); + if (v.getValue() == null) { + return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); + } + return v; + } + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d", + type, length, pos), pos); + } } } diff --git a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java index c724285..4453703 100644 --- a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java @@ -50,6 +50,8 @@ public class AmountParseInfo extends FieldParseInfo { return new IsoValue(type, new BigDecimal(c).movePointLeft(2), null); } catch (NumberFormatException ex) { throw new ParseException(String.format("Cannot read amount '%s' pos %d", c, pos), pos); + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for AMOUNT field, pos %d", pos), pos); } } @@ -68,6 +70,8 @@ public class AmountParseInfo extends FieldParseInfo { return new IsoValue(IsoType.AMOUNT, new BigDecimal(new String(digits)), null); } catch (NumberFormatException ex) { throw new ParseException(String.format("Cannot read amount '%s' pos %d", new String(digits), pos), pos); + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for AMOUNT field, pos %d", pos), pos); } } diff --git a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java index 65ed3c7..b62c7aa 100644 --- a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java @@ -54,13 +54,17 @@ public class LlbinParseInfo extends FieldParseInfo { if (custom == null) { return new IsoValue(type, binval, binval.length, null); } else { - @SuppressWarnings({"unchecked", "rawtypes"}) - IsoValue v = new IsoValue(type, custom.decodeField( - new String(buf, pos + 2, length)), binval.length, custom); - if (v.getValue() == null) { - return new IsoValue(type, binval, binval.length, null); - } - return v; + try { + @SuppressWarnings({"unchecked", "rawtypes"}) + IsoValue v = new IsoValue(type, custom.decodeField( + new String(buf, pos + 2, length)), binval.length, custom); + if (v.getValue() == null) { + return new IsoValue(type, binval, binval.length, null); + } + return v; + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d (LEN states '%s')", pos, new String(buf, pos, 2)), pos); + } } } diff --git a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java index 57c4fee..32c4d7b 100644 --- a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java @@ -56,14 +56,18 @@ public class LllbinParseInfo extends FieldParseInfo { if (custom == null) { return new IsoValue(type, binval, binval.length, null); } else { - @SuppressWarnings({"unchecked", "rawtypes"}) - IsoValue v = new IsoValue(type, custom.decodeField( - length == 0 ? "" : new String(buf, pos + 3, length)), length, custom); - if (v.getValue() == null) { - //problems decoding? return the string - return new IsoValue(type, binval, binval.length, null); - } - return v; + try { + @SuppressWarnings({"unchecked", "rawtypes"}) + IsoValue v = new IsoValue(type, custom.decodeField( + length == 0 ? "" : new String(buf, pos + 3, length)), length, custom); + if (v.getValue() == null) { + //problems decoding? return the string + return new IsoValue(type, binval, binval.length, null); + } + return v; + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos); + } } } diff --git a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java index 2b5b438..90337d7 100644 --- a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java @@ -53,7 +53,12 @@ public class LllvarParseInfo extends FieldParseInfo { } else if (length+pos+3 > buf.length) { throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos); } - String _v = length == 0 ? "" : new String(buf, pos + 3, length, getCharacterEncoding()); + String _v; + try { + _v = length == 0 ? "" : new String(buf, pos + 3, length, getCharacterEncoding()); + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for LLLVAR header, pos %d", pos), pos); + } //This is new: if the String's length is different from the specified length in the buffer, //there are probably some extended characters. So we create a String from the rest of the buffer, //and then cut it to the specified length. diff --git a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java index c7cdc21..3c8d3e3 100644 --- a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java @@ -48,7 +48,12 @@ public class LlvarParseInfo extends FieldParseInfo { } else if (length+pos+2 > buf.length) { throw new ParseException(String.format("Insufficient data for LLVAR field, pos %d", pos), pos); } - String _v = length == 0 ? "" : new String(buf, pos + 2, length, getCharacterEncoding()); + String _v; + try { + _v = length == 0 ? "" : new String(buf, pos + 2, length, getCharacterEncoding()); + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for LLVAR header, pos %d", pos), pos); + } //This is new: if the String's length is different from the specified length in the buffer, //there are probably some extended characters. So we create a String from the rest of the buffer, //and then cut it to the specified length. diff --git a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java index 4f1f356..4c87624 100644 --- a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java @@ -57,10 +57,15 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo { //Use a BigInteger char[] digits = new char[length]; int start = 0; - for (int i = pos; i < pos + (length / 2) + (length % 2); i++) { - digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48); - digits[start++] = (char)((buf[i] & 0x0f) + 48); - } + try { + for (int i = pos; i < pos + (length / 2) + (length % 2); i++) { + digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48); + digits[start++] = (char)((buf[i] & 0x0f) + 48); + } + } catch (IndexOutOfBoundsException ex) { + throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d", + type, length, pos), pos); + } return new IsoValue(IsoType.NUMERIC, new BigInteger(new String(digits)), length, null); } }