From 63e89e708b89e58fde4a6b08f829c19d7a16bcf6 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Mon, 13 May 2013 19:47:48 -0500 Subject: [PATCH] add forceStringEncoding property to pass to messages for #13 --- .../java/com/solab/iso8583/MessageFactory.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/solab/iso8583/MessageFactory.java b/src/main/java/com/solab/iso8583/MessageFactory.java index a8d621d..4f35f46 100644 --- a/src/main/java/com/solab/iso8583/MessageFactory.java +++ b/src/main/java/com/solab/iso8583/MessageFactory.java @@ -75,8 +75,20 @@ public class MessageFactory { private boolean ignoreLast; private boolean forceb2; private boolean binBitmap; + private boolean forceStringEncoding; private String encoding = System.getProperty("file.encoding"); + /** This flag gets passed on to newly created messages and also sets this value for all + * field parsers in parsing guides. */ + public void setForceStringEncoding(boolean flag) { + forceStringEncoding = flag; + for (Map pm : parseMap.values()) { + for (FieldParseInfo parser : pm.values()) { + parser.setForceStringDecoding(flag); + } + } + } + /** 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) { @@ -268,11 +280,13 @@ public class MessageFactory { if (buf.length < minlength) { throw new ParseException("Insufficient buffer length, needs to be at least " + minlength, 0); } - final T m = createIsoMessage(isoHeaderLength > 0 ? new String(buf, 0, isoHeaderLength) : null); + final T m = createIsoMessage(isoHeaderLength > 0 ? new String(buf, 0, isoHeaderLength, encoding) : null); m.setCharacterEncoding(encoding); final int type; if (useBinary) { type = ((buf[isoHeaderLength] & 0xff) << 8) | (buf[isoHeaderLength + 1] & 0xff); + } else if (forceStringEncoding) { + type = Integer.parseInt(new String(buf, isoHeaderLength, 4, encoding), 16); } else { type = ((buf[isoHeaderLength] - 48) << 12) | ((buf[isoHeaderLength + 1] - 48) << 8)