work on #3 - catch IOOBE

This commit is contained in:
Enrique Zamudio
2012-12-26 16:02:38 -06:00
parent 778501b2d6
commit 2e5911f261
7 changed files with 63 additions and 31 deletions
@@ -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", throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d",
type, length, pos), pos); type, length, pos), pos);
} }
if (custom == null) { try {
return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null); if (custom == null) {
} else { return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null);
@SuppressWarnings({"unchecked", "rawtypes"}) } else {
IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom); @SuppressWarnings({"unchecked", "rawtypes"})
if (v.getValue() == null) { IsoValue<?> v = new IsoValue(type, custom.decodeField(new String(buf, pos, length, getCharacterEncoding())), length, custom);
return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null); if (v.getValue() == null) {
} return new IsoValue<String>(type, new String(buf, pos, length, getCharacterEncoding()), length, null);
return v; }
} 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);
}
} }
} }
@@ -50,6 +50,8 @@ public class AmountParseInfo extends FieldParseInfo {
return new IsoValue<BigDecimal>(type, new BigDecimal(c).movePointLeft(2), null); return new IsoValue<BigDecimal>(type, new BigDecimal(c).movePointLeft(2), null);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
throw new ParseException(String.format("Cannot read amount '%s' pos %d", c, pos), pos); 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<BigDecimal>(IsoType.AMOUNT, new BigDecimal(new String(digits)), null); return new IsoValue<BigDecimal>(IsoType.AMOUNT, new BigDecimal(new String(digits)), null);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
throw new ParseException(String.format("Cannot read amount '%s' pos %d", new String(digits), pos), pos); 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);
} }
} }
@@ -54,13 +54,17 @@ public class LlbinParseInfo extends FieldParseInfo {
if (custom == null) { if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null); return new IsoValue<byte[]>(type, binval, binval.length, null);
} else { } else {
@SuppressWarnings({"unchecked", "rawtypes"}) try {
IsoValue<?> v = new IsoValue(type, custom.decodeField( @SuppressWarnings({"unchecked", "rawtypes"})
new String(buf, pos + 2, length)), binval.length, custom); IsoValue<?> v = new IsoValue(type, custom.decodeField(
if (v.getValue() == null) { new String(buf, pos + 2, length)), binval.length, custom);
return new IsoValue<byte[]>(type, binval, binval.length, null); if (v.getValue() == null) {
} return new IsoValue<byte[]>(type, binval, binval.length, null);
return v; }
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);
}
} }
} }
@@ -56,14 +56,18 @@ public class LllbinParseInfo extends FieldParseInfo {
if (custom == null) { if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null); return new IsoValue<byte[]>(type, binval, binval.length, null);
} else { } else {
@SuppressWarnings({"unchecked", "rawtypes"}) try {
IsoValue<?> v = new IsoValue(type, custom.decodeField( @SuppressWarnings({"unchecked", "rawtypes"})
length == 0 ? "" : new String(buf, pos + 3, length)), length, custom); IsoValue<?> v = new IsoValue(type, custom.decodeField(
if (v.getValue() == null) { length == 0 ? "" : new String(buf, pos + 3, length)), length, custom);
//problems decoding? return the string if (v.getValue() == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null); //problems decoding? return the string
} return new IsoValue<byte[]>(type, binval, binval.length, null);
return v; }
return v;
} catch (IndexOutOfBoundsException ex) {
throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos);
}
} }
} }
@@ -53,7 +53,12 @@ public class LllvarParseInfo extends FieldParseInfo {
} else 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); 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, //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, //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. //and then cut it to the specified length.
@@ -48,7 +48,12 @@ public class LlvarParseInfo extends FieldParseInfo {
} else if (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); 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, //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, //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. //and then cut it to the specified length.
@@ -57,10 +57,15 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo {
//Use a BigInteger //Use a BigInteger
char[] digits = new char[length]; char[] digits = new char[length];
int start = 0; int start = 0;
for (int i = pos; i < pos + (length / 2) + (length % 2); i++) { try {
digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48); for (int i = pos; i < pos + (length / 2) + (length % 2); i++) {
digits[start++] = (char)((buf[i] & 0x0f) + 48); 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<Number>(IsoType.NUMERIC, new BigInteger(new String(digits)), length, null); return new IsoValue<Number>(IsoType.NUMERIC, new BigInteger(new String(digits)), length, null);
} }
} }