Improve parsing of invalid data, throwing more specific messages
git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@108 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
@@ -37,9 +37,8 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo {
|
||||
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
}
|
||||
if (pos+length > buf.length) {
|
||||
throw new ParseException(String.format("Invalid ALPHA/NUM position %d", pos), pos);
|
||||
} else if (pos+length > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for %s field of length %d, pos %d",
|
||||
type, length, pos), pos);
|
||||
}
|
||||
|
||||
@@ -37,6 +37,12 @@ public class AlphaParseInfo extends AlphaNumericFieldParseInfo {
|
||||
|
||||
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin ALPHA position %d", pos), pos);
|
||||
} else if (pos+length > buf.length) {
|
||||
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<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null);
|
||||
} else {
|
||||
|
||||
@@ -38,7 +38,7 @@ public class AmountParseInfo extends FieldParseInfo {
|
||||
|
||||
public IsoValue<BigDecimal> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid AMOUNT position %d", pos), pos);
|
||||
}
|
||||
if (pos+12 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for AMOUNT field, pos %d", pos), pos);
|
||||
|
||||
@@ -41,7 +41,7 @@ public class BinaryParseInfo extends FieldParseInfo {
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid BINARY position %d", pos), pos);
|
||||
}
|
||||
if (pos+(length*2) > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for BINARY field of length %d, pos %d",
|
||||
|
||||
@@ -45,7 +45,7 @@ public class Date10ParseInfo extends FieldParseInfo {
|
||||
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid DATE10 position %d", pos), pos);
|
||||
}
|
||||
if (pos+10 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for DATE10 field, pos %d", pos), pos);
|
||||
|
||||
@@ -39,7 +39,7 @@ public class Date4ParseInfo extends FieldParseInfo {
|
||||
@Override
|
||||
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid DATE4 position %d", pos), pos);
|
||||
}
|
||||
if (pos+4 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for DATE4 field, pos %d", pos), pos);
|
||||
|
||||
@@ -39,7 +39,7 @@ public class DateExpParseInfo extends FieldParseInfo {
|
||||
@Override
|
||||
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid DATE_EXP position %d", pos), pos);
|
||||
}
|
||||
if (pos+4 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for DATE_EXP field, pos %d", pos), pos);
|
||||
|
||||
@@ -39,13 +39,15 @@ public class LlbinParseInfo extends FieldParseInfo {
|
||||
@Override
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid LLBIN position %d", pos), pos);
|
||||
} else if (pos+2 > buf.length) {
|
||||
throw new ParseException("Insufficient LLBIN header", pos);
|
||||
}
|
||||
length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLBIN length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (pos+2 > buf.length || length+pos+2 > buf.length) {
|
||||
if (length+pos+2 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d (LEN states '%s')", pos, new String(buf, pos, 2)), pos);
|
||||
}
|
||||
byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 2, length));
|
||||
@@ -64,12 +66,17 @@ public class LlbinParseInfo extends FieldParseInfo {
|
||||
|
||||
@Override
|
||||
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin LLBIN position %d", pos), pos);
|
||||
} else if (pos+1 > buf.length) {
|
||||
throw new ParseException("Insufficient bin LLBIN header", pos);
|
||||
}
|
||||
length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLBIN length %d pos %d", length, pos), pos);
|
||||
throw new ParseException(String.format("Invalid bin LLBIN length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (pos+1 > buf.length || length+pos+1 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d", pos), pos);
|
||||
if (length+pos+1 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin LLBIN field, pos %d", pos), pos);
|
||||
}
|
||||
byte[] _v = new byte[length];
|
||||
System.arraycopy(buf, pos+1, _v, 0, length);
|
||||
|
||||
@@ -39,7 +39,9 @@ public class LllbinParseInfo extends FieldParseInfo {
|
||||
@Override
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid LLLBIN position %d", pos), pos);
|
||||
} else if (pos+3 > buf.length) {
|
||||
throw new ParseException("Insufficient LLLBIN header", pos);
|
||||
}
|
||||
if (!(Character.isDigit(buf[pos]) && Character.isDigit(buf[pos+1]) && Character.isDigit(buf[pos+2]))) {
|
||||
throw new ParseException(String.format("Invalid LLLBIN length '%s' pos %d", new String(buf, pos, 3), pos), pos);
|
||||
@@ -47,8 +49,7 @@ public class LllbinParseInfo extends FieldParseInfo {
|
||||
length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLLBIN length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (length+pos+3 > buf.length) {
|
||||
} else if (length+pos+3 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos);
|
||||
}
|
||||
byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 3, length));
|
||||
@@ -68,12 +69,17 @@ public class LllbinParseInfo extends FieldParseInfo {
|
||||
|
||||
@Override
|
||||
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin LLLBIN position %d", pos), pos);
|
||||
} else if (pos+3 > buf.length) {
|
||||
throw new ParseException("Insufficient bin LLLBIN header", pos);
|
||||
}
|
||||
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLLBIN length %d pos %d", length, pos), pos);
|
||||
throw new ParseException(String.format("Invalid bin LLLBIN length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (length+pos+2 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos);
|
||||
throw new ParseException(String.format("Insufficient data for bin LLLBIN field, pos %d", pos), pos);
|
||||
}
|
||||
byte[] _v = new byte[length];
|
||||
System.arraycopy(buf, pos+2, _v, 0, length);
|
||||
|
||||
@@ -39,7 +39,9 @@ public class LllvarParseInfo extends FieldParseInfo {
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid LLLVAR position %d", pos), pos);
|
||||
} else if (pos+3 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLLVAR header, pos %d", pos), pos);
|
||||
}
|
||||
if (!(Character.isDigit(buf[pos]) && Character.isDigit(buf[pos+1]) && Character.isDigit(buf[pos+2]))) {
|
||||
throw new ParseException(String.format("Invalid LLLVAR length '%s' pos %d",
|
||||
@@ -48,8 +50,7 @@ public class LllvarParseInfo extends FieldParseInfo {
|
||||
length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (length+pos+3 > buf.length) {
|
||||
} 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());
|
||||
@@ -70,12 +71,16 @@ public class LllvarParseInfo extends FieldParseInfo {
|
||||
|
||||
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin LLLVAR position %d", pos), pos);
|
||||
} else if (pos+3 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin LLLVAR header, pos %d", pos), pos);
|
||||
}
|
||||
length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (length+pos+2 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid bin LLLVAR length %d pos %d", length, pos), pos);
|
||||
} else if (length+pos+2 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin LLLVAR field, pos %d", pos), pos);
|
||||
}
|
||||
if (custom == null) {
|
||||
return new IsoValue<String>(type, new String(buf, pos + 2, length, getCharacterEncoding()), null);
|
||||
|
||||
@@ -38,13 +38,14 @@ public class LlvarParseInfo extends FieldParseInfo {
|
||||
public IsoValue<?> parse(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
throw new ParseException(String.format("Invalid LLVAR position %d", pos), pos);
|
||||
} else if (pos+2 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLVAR header, pos %d", pos), pos);
|
||||
}
|
||||
length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLVAR length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (pos+2 > buf.length || length+pos+2 > buf.length) {
|
||||
} 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());
|
||||
@@ -68,13 +69,17 @@ public class LlvarParseInfo extends FieldParseInfo {
|
||||
|
||||
public IsoValue<?> parseBinary(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException, UnsupportedEncodingException {
|
||||
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin LLVAR position %d", pos), pos);
|
||||
} else if (pos+1 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin LLVAR header, pos %d", pos), pos);
|
||||
}
|
||||
length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f);
|
||||
if (length < 0) {
|
||||
throw new ParseException(String.format("Invalid LLVAR length %d pos %d", length, pos), pos);
|
||||
throw new ParseException(String.format("Invalid bin LLVAR length %d pos %d", length, pos), pos);
|
||||
}
|
||||
if (pos+1 > buf.length || length+pos+1 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for LLVAR field, pos %d", pos), pos);
|
||||
if (length+pos+1 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin LLVAR field, pos %d", pos), pos);
|
||||
}
|
||||
if (custom == null) {
|
||||
return new IsoValue<String>(type, new String(buf, pos + 1, length, getCharacterEncoding()), null);
|
||||
|
||||
@@ -36,6 +36,12 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo {
|
||||
}
|
||||
|
||||
public IsoValue<Number> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin NUMERIC position %d", pos), pos);
|
||||
} else if (pos+length > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d",
|
||||
type, length, pos), pos);
|
||||
}
|
||||
//A long covers up to 18 digits
|
||||
if (length < 19) {
|
||||
long l = 0;
|
||||
|
||||
@@ -40,9 +40,8 @@ public class TimeParseInfo extends FieldParseInfo {
|
||||
@Override
|
||||
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
}
|
||||
if (pos+6 > buf.length) {
|
||||
throw new ParseException(String.format("Invalid TIME position %d", pos), pos);
|
||||
} else if (pos+6 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for TIME field, pos %d", pos), pos);
|
||||
}
|
||||
Calendar cal = Calendar.getInstance();
|
||||
@@ -54,9 +53,14 @@ public class TimeParseInfo extends FieldParseInfo {
|
||||
|
||||
@Override
|
||||
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid bin TIME position %d", pos), pos);
|
||||
} else if (pos+3 > buf.length) {
|
||||
throw new ParseException(String.format("Insufficient data for bin TIME field, pos %d", pos), pos);
|
||||
}
|
||||
int[] tens = new int[3];
|
||||
int start = 0;
|
||||
for (int i = pos; i < pos + tens.length; i++) {
|
||||
for (int i = pos; i < pos + 3; i++) {
|
||||
tens[start++] = (((buf[i] & 0xf0) >> 4) * 10) + (buf[i] & 0x0f);
|
||||
}
|
||||
Calendar cal = Calendar.getInstance();
|
||||
|
||||
Reference in New Issue
Block a user