Added new IsoTypes: BINARY, LLBIN and LLLBIN. Similar to ALPHA, LLVAR and LLLVAR but they handle binary values that get translated to byte arrays, directly if using binary coding, or hex-encoded if using ASCII.

git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@63 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
chochos
2010-12-06 18:42:02 +00:00
parent 64989e5168
commit 3c8cacbb8d
4 changed files with 189 additions and 4 deletions
+57 -2
View File
@@ -23,6 +23,8 @@ import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import com.solab.iso8583.util.HexCodec;
/** Represents a value that is stored in a field inside an ISO8583 message.
* It can format the value when the message is generated.
* Some values have a fixed length, other values require a length to be specified
@@ -72,6 +74,22 @@ public class IsoValue<T> implements Cloneable {
} else if (t == IsoType.LLLVAR && length > 999) {
throw new IllegalArgumentException("LLLVAR can only hold values up to 999 chars");
}
} else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) {
if (custom == null) {
length = ((byte[])value).length;
} else {
//TODO special encoder, NO strings
String enc = custom.encodeField(value);
if (enc == null) {
enc = value == null ? "" : value.toString();
}
length = enc.length();
}
if (t == IsoType.LLBIN && length > 99) {
throw new IllegalArgumentException("LLBIN can only hold values up to 99 chars");
} else if (t == IsoType.LLLBIN && length > 999) {
throw new IllegalArgumentException("LLLBIN can only hold values up to 999 chars");
}
} else {
length = type.getLength();
}
@@ -104,6 +122,16 @@ public class IsoValue<T> implements Cloneable {
} else if (t == IsoType.LLLVAR && length > 999) {
throw new IllegalArgumentException("LLLVAR can only hold values up to 999 chars");
}
} else if (t == IsoType.LLBIN || t == IsoType.LLLBIN) {
if (len == 0) {
//TODO customfield binary!
length = custom == null ? ((byte[])val).length : custom.encodeField(value).length();
}
if (t == IsoType.LLBIN && length > 99) {
throw new IllegalArgumentException("LLBIN can only hold values up to 99 chars");
} else if (t == IsoType.LLLBIN && length > 999) {
throw new IllegalArgumentException("LLLBIN can only hold values up to 999 chars");
}
}
}
@@ -148,6 +176,10 @@ public class IsoValue<T> implements Cloneable {
return encoder == null ? value.toString() : encoder.encodeField(value);
} else if (value instanceof Date) {
return type.format((Date)value);
} else if (type == IsoType.BINARY) {
return type.format(encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value), length * 2);
} else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) {
return encoder == null ? HexCodec.hexEncode((byte[])value) : encoder.encodeField(value);
}
return encoder == null ? value.toString() : encoder.encodeField(value);
}
@@ -204,6 +236,25 @@ public class IsoValue<T> implements Cloneable {
}
outs.write((length % 10) + 48);
}
} else if (type == IsoType.LLBIN || type == IsoType.LLLBIN) {
if (binary) {
if (type == IsoType.LLLBIN ) {
outs.write(length / 100); //00 to 09 automatically in BCD
}
//BCD encode the rest of the length
outs.write((((length % 100) / 10) << 4) | (length % 10));
} else {
//write the length in ASCII
if (type == IsoType.LLLBIN) {
outs.write((length / 50) + 48);
}
if (length >= 10) {
outs.write((((length*2) % 100) / 10) + 48);
} else {
outs.write(48);
}
outs.write(((length*2) % 10) + 48);
}
} else if (binary) {
//numeric types in binary are coded like this
byte[] buf = null;
@@ -221,8 +272,12 @@ public class IsoValue<T> implements Cloneable {
return;
}
}
//Just write the value as text
outs.write(toString().getBytes());
if (binary && (type == IsoType.BINARY || type == IsoType.LLBIN || type == IsoType.LLLBIN)) {
outs.write((byte[])value);
} else {
//Just write the value as text
outs.write(toString().getBytes());
}
}
/** Encode the value as BCD and put it in the buffer. The buffer must be big enough