diff --git a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java index 6e6989d..a6ef867 100644 --- a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java @@ -49,22 +49,23 @@ public class LlbinParseInfo extends FieldParseInfo { throw new ParseException(String.format("Insufficient LLBIN header field %d", field), pos); } - length = decodeLength(buf, pos, 2); - if (length < 0) { + final int len = decodeLength(buf, pos, 2); + if (len < 0) { throw new ParseException(String.format("Invalid LLBIN field %d length %d pos %d", - field, length, pos), pos); + field, len, pos), pos); } - if (length+pos+2 > buf.length) { + if (len+pos+2 > buf.length) { throw new ParseException(String.format( "Insufficient data for LLBIN field %d, pos %d (LEN states '%s')", field, pos, new String(buf, pos, 2)), pos); } - byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 2, length)); + byte[] binval = len == 0 ? new byte[0] : HexCodec.hexDecode( + new String(buf, pos + 2, len)); if (custom == null) { return new IsoValue(type, binval, binval.length, null); } else if (custom instanceof CustomBinaryField) { try { - T dec = ((CustomBinaryField)custom).decodeBinaryField(buf, pos + 2, length); + T dec = ((CustomBinaryField)custom).decodeBinaryField(buf, pos + 2, len); return dec == null ? new IsoValue(type, binval, binval.length, null) : new IsoValue(type, dec, 0, custom); } catch (IndexOutOfBoundsException ex) { @@ -74,7 +75,7 @@ public class LlbinParseInfo extends FieldParseInfo { } } else { try { - T dec = custom.decodeField(new String(buf, pos + 2, length)); + T dec = custom.decodeField(new String(buf, pos + 2, len)); return dec == null ? new IsoValue(type, binval, binval.length, null) : new IsoValue(type, dec, binval.length, custom); } catch (IndexOutOfBoundsException ex) { @@ -98,7 +99,7 @@ public class LlbinParseInfo extends FieldParseInfo { } final int l = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f); if (l < 0) { - throw new ParseException(String.format("Invalid bin LLBIN length %d pos %d", length, pos), pos); + throw new ParseException(String.format("Invalid bin LLBIN length %d pos %d", l, pos), pos); } if (l+pos+1 > buf.length) { throw new ParseException(String.format( diff --git a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java index de7956c..daec5fc 100644 --- a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java @@ -46,34 +46,35 @@ public class LllvarParseInfo extends FieldParseInfo { throw new ParseException(String.format( "Insufficient data for LLLVAR header field %d pos %d", field, pos), pos); } - length = decodeLength(buf, pos, 3); - if (length < 0) { + final int len = decodeLength(buf, pos, 3); + if (len < 0) { throw new ParseException(String.format("Invalid LLLVAR length %d field %d pos %d", - length, field, pos), pos); - } else if (length+pos+3 > buf.length) { + len, field, pos), pos); + } else if (len+pos+3 > buf.length) { throw new ParseException(String.format("Insufficient data for LLLVAR field %d, pos %d", field, pos), pos); } String _v; try { - _v = length == 0 ? "" : new String(buf, pos + 3, length, getCharacterEncoding()); + _v = len == 0 ? "" : new String(buf, pos + 3, len, getCharacterEncoding()); } catch (IndexOutOfBoundsException ex) { throw new ParseException(String.format( "Insufficient data for LLLVAR header, field %d pos %d", field, 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. - if (_v.length() != length) { - _v = new String(buf, pos + 3, buf.length-pos-3, getCharacterEncoding()).substring(0, length); + //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. + if (_v.length() != len) { + _v = new String(buf, pos + 3, buf.length-pos-3, + getCharacterEncoding()).substring(0, len); } if (custom == null) { - return new IsoValue(type, _v, length, null); + return new IsoValue(type, _v, len, null); } else { T decoded = custom.decodeField(_v); //If decode fails, return string; otherwise use the decoded object and its codec - return decoded == null ? new IsoValue(type, _v, length, null) : - new IsoValue(type, decoded, length, custom); + return decoded == null ? new IsoValue(type, _v, len, null) : + new IsoValue(type, decoded, len, custom); } } @@ -86,20 +87,22 @@ public class LllvarParseInfo extends FieldParseInfo { throw new ParseException(String.format( "Insufficient data for bin LLLVAR header, field %d pos %d", field, pos), pos); } - length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f); - if (length < 0) { + final int len = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f); + if (len < 0) { throw new ParseException(String.format( - "Invalid bin LLLVAR length %d, field %d pos %d", length, field, pos), pos); - } else if (length+pos+2 > buf.length) { + "Invalid bin LLLVAR length %d, field %d pos %d", len, field, pos), pos); + } else if (len+pos+2 > buf.length) { throw new ParseException(String.format( "Insufficient data for bin LLLVAR field %d, pos %d", field, pos), pos); } if (custom == null) { - return new IsoValue(type, new String(buf, pos + 2, length, getCharacterEncoding()), null); + return new IsoValue(type, new String(buf, pos + 2, len, getCharacterEncoding()), null); } else { - IsoValue v = new IsoValue(type, custom.decodeField(new String(buf, pos + 2, length, getCharacterEncoding())), custom); + IsoValue v = new IsoValue(type, custom.decodeField( + new String(buf, pos + 2, len, getCharacterEncoding())), custom); if (v.getValue() == null) { - return new IsoValue(type, new String(buf, pos + 2, length, getCharacterEncoding()), null); + return new IsoValue(type, + new String(buf, pos + 2, len, getCharacterEncoding()), null); } return v; } diff --git a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java index b97576c..6841522 100644 --- a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java @@ -40,37 +40,41 @@ public class LlvarParseInfo extends FieldParseInfo { final int pos, final CustomField custom) throws ParseException, UnsupportedEncodingException { if (pos < 0) { - throw new ParseException(String.format("Invalid LLVAR field %d %d", field, pos), pos); - } else if (pos+2 > buf.length) { - throw new ParseException(String.format("Insufficient data for LLVAR header, pos %d", pos), pos); - } - length = decodeLength(buf, pos, 2); - if (length < 0) { throw new ParseException(String.format( - "Invalid LLVAR length %d, field %d pos %d", length, field, pos), pos); - } else if (length+pos+2 > buf.length) { + "Invalid LLVAR field %d %d", field, pos), pos); + } else if (pos+2 > buf.length) { + throw new ParseException(String.format( + "Insufficient data for LLVAR header, pos %d", pos), pos); + } + final int len = decodeLength(buf, pos, 2); + if (len < 0) { + throw new ParseException(String.format( + "Invalid LLVAR length %d, field %d pos %d", len, field, pos), pos); + } else if (len+pos+2 > buf.length) { throw new ParseException(String.format( "Insufficient data for LLVAR field %d, pos %d", field, pos), pos); } String _v; try { - _v = length == 0 ? "" : new String(buf, pos + 2, length, getCharacterEncoding()); + _v = len == 0 ? "" : new String(buf, pos + 2, len, getCharacterEncoding()); } catch (IndexOutOfBoundsException ex) { throw new ParseException(String.format( "Insufficient data for LLVAR header, field %d pos %d", field, 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. - if (_v.length() != length) { - _v = new String(buf, pos + 2, buf.length-pos-2, getCharacterEncoding()).substring(0, length); + //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. + if (_v.length() != len) { + _v = new String(buf, pos + 2, buf.length-pos-2, + getCharacterEncoding()).substring(0, len); } if (custom == null) { - return new IsoValue(type, _v, length, null); + return new IsoValue(type, _v, len, null); } else { T dec = custom.decodeField(_v); - return dec == null ? new IsoValue(type, _v, length, null) : - new IsoValue(type, dec, length, custom); + return dec == null ? new IsoValue(type, _v, len, null) : + new IsoValue(type, dec, len, custom); } } @@ -83,22 +87,25 @@ public class LlvarParseInfo extends FieldParseInfo { field, pos), pos); } else if (pos+1 > buf.length) { throw new ParseException(String.format( - "Insufficient data for bin LLVAR header, field %d pos %d", field, pos), pos); + "Insufficient data for bin LLVAR header, field %d pos %d", + field, pos), pos); } - length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f); - if (length < 0) { + final int len = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f); + if (len < 0) { throw new ParseException(String.format( - "Invalid bin LLVAR length %d, field %d pos %d", length, field, pos), pos); + "Invalid bin LLVAR length %d, field %d pos %d", len, field, pos), pos); } - if (length+pos+1 > buf.length) { + if (len+pos+1 > buf.length) { throw new ParseException(String.format( "Insufficient data for bin LLVAR field %d, pos %d", field, pos), pos); } if (custom == null) { - return new IsoValue(type, new String(buf, pos + 1, length, getCharacterEncoding()), null); + return new IsoValue(type, new String(buf, pos + 1, len, + getCharacterEncoding()), null); } else { - T dec = custom.decodeField(new String(buf, pos + 1, length, getCharacterEncoding())); - return dec == null ? new IsoValue(type, new String(buf, pos + 1, length, getCharacterEncoding()), null) : + T dec = custom.decodeField(new String(buf, pos + 1, len, getCharacterEncoding())); + return dec == null ? new IsoValue(type, + new String(buf, pos + 1, len, getCharacterEncoding()), null) : new IsoValue(type, dec, custom); } }