FieldParseInfo is now an abstract class, with a concrete subclass for each IsoType. This greatly improves the parsing speed.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@66 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2011-02-21 19:53:47 +00:00
parent 3b36a1dfe9
commit d291d41bbc
16 changed files with 781 additions and 395 deletions
@@ -0,0 +1,70 @@
package com.solab.iso8583.parse;
import java.text.ParseException;
import com.solab.iso8583.CustomField;
import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;
import com.solab.iso8583.util.HexCodec;
/** This class is used to parse fields of type LLBIN.
*
* @author Enrique Zamudio
*/
public class LlbinParseInfo extends FieldParseInfo {
public LlbinParseInfo() {
super(IsoType.LLBIN, 0);
}
@Override
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
throws ParseException {
if (pos < 0) {
throw new ParseException(String.format("Invalid position %d", pos), 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) {
throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d", pos), pos);
}
byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 2, length));
if (custom == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(
new String(buf, pos + 2, length)), binval.length, custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, binval, binval.length, null);
}
return v;
}
}
@Override
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
CustomField<T> custom) throws ParseException {
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);
}
if (pos+1 > buf.length || length+pos+1 > buf.length) {
throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d", pos), pos);
}
byte[] _v = new byte[length];
System.arraycopy(buf, pos+1, _v, 0, length);
if (custom == null) {
return new IsoValue<byte[]>(type, _v, null);
} else {
IsoValue<T> v = new IsoValue<T>(type, custom.decodeField(HexCodec.hexEncode(_v)), custom);
if (v.getValue() == null) {
return new IsoValue<byte[]>(type, _v, null);
}
return v;
}
}
}