From cf1e5053b1156f3f4bac799e812341d45c866ca0 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Fri, 30 Nov 2012 11:54:00 -0600 Subject: [PATCH] Handle the encoding of binary bitmaps in text-encoded messages --- .../com/solab/iso8583/MessageFactory.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/solab/iso8583/MessageFactory.java b/src/main/java/com/solab/iso8583/MessageFactory.java index e312d49..74ee87d 100644 --- a/src/main/java/com/solab/iso8583/MessageFactory.java +++ b/src/main/java/com/solab/iso8583/MessageFactory.java @@ -74,10 +74,25 @@ public class MessageFactory { /** Flag to specify if missing fields should be ignored as long as they're at the end of the message. */ private boolean ignoreLast; private boolean forceb2; + private boolean binBitmap; private String encoding = System.getProperty("file.encoding"); + /** Tells the factory to create messages that encode their bitmaps in binary format + * even when they're encoded as text. Has no effect on binary messages. */ + public void setUseBinaryBitmap(boolean flag) { + binBitmap = flag; + } + /** Returns true if the factory is set to create and parse bitmaps in binary format + * when the messages are encoded as text. */ + public boolean isUseBinaryBitmap() { + return binBitmap; + } + /** Sets the character encoding used for parsing ALPHA, LLVAR and LLLVAR fields. */ public void setCharacterEncoding(String value) { + if (encoding == null) { + throw new IllegalArgumentException("Cannot set null encoding."); + } encoding = value; if (parseMap.size() > 0) { for (Map pt : parseMap.values()) { @@ -175,6 +190,7 @@ public class MessageFactory { m.setEtx(etx); m.setBinary(useBinary); m.setForceSecondaryBitmap(forceb2); + m.setBinaryBitmap(binBitmap); m.setCharacterEncoding(encoding); //Copy the values from the template @@ -205,6 +221,7 @@ public class MessageFactory { T resp = createIsoMessage(isoHeaders.get(request.getType() + 16)); resp.setCharacterEncoding(request.getCharacterEncoding()); resp.setBinary(request.isBinary()); + resp.setBinaryBitmap(request.isBinaryBitmap()); resp.setType(request.getType() + 16); resp.setEtx(etx); resp.setForceSecondaryBitmap(forceb2); @@ -236,7 +253,7 @@ public class MessageFactory { * and the rest of the message must come. */ public T parseMessage(byte[] buf, int isoHeaderLength) throws ParseException, UnsupportedEncodingException { - final int minlength = isoHeaderLength+(useBinary ? 10 : 20); + final int minlength = isoHeaderLength+(useBinary||binBitmap ? 10 : 20); if (buf.length < minlength) { throw new ParseException("Insufficient buffer length, needs to be at least " + minlength, 0); } @@ -255,7 +272,7 @@ public class MessageFactory { //Parse the bitmap (primary first) final BitSet bs = new BitSet(64); int pos = 0; - if (useBinary) { + if (useBinary || binBitmap) { for (int i = isoHeaderLength + 2; i < isoHeaderLength + 10; i++) { int bit = 128; for (int b = 0; b < 8; b++) { @@ -357,7 +374,7 @@ public class MessageFactory { for (Integer i : index) { FieldParseInfo fpi = parseGuide.get(i); if (bs.get(i - 1)) { - if (ignoreLast && pos >= buf.length && i == index.get(index.size() -1)) { + if (ignoreLast && pos >= buf.length && i.intValue() == index.get(index.size() -1)) { log.warn("Field {} is not really in the message even though it's in the bitmap", i); bs.clear(i - 1); } else { @@ -384,7 +401,7 @@ public class MessageFactory { for (Integer i : index) { FieldParseInfo fpi = parseGuide.get(i); if (bs.get(i - 1)) { - if (ignoreLast && pos >= buf.length && i == index.get(index.size() -1)) { + if (ignoreLast && pos >= buf.length && i.intValue() == index.get(index.size() -1)) { log.warn("Field {} is not really in the message even though it's in the bitmap", i); bs.clear(i - 1); } else {