From dfb1bc1b4568cab27ca44e713af33594f8c2999d Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Fri, 30 Nov 2012 11:52:14 -0600 Subject: [PATCH] Handle binary bitmaps in text-encoded messages (thanks @dhinckle2) also catch a bug in LLVAR encoding --- .../java/com/solab/iso8583/IsoMessage.java | 26 ++++++++++++++++--- src/main/java/com/solab/iso8583/IsoValue.java | 2 +- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/solab/iso8583/IsoMessage.java b/src/main/java/com/solab/iso8583/IsoMessage.java index b4480a2..d8676de 100644 --- a/src/main/java/com/solab/iso8583/IsoMessage.java +++ b/src/main/java/com/solab/iso8583/IsoMessage.java @@ -50,6 +50,7 @@ public class IsoMessage { private int etx = -1; /** Flag to enforce secondary bitmap even if empty. */ private boolean forceb2; + private boolean binBitmap; private String encoding = System.getProperty("file.encoding"); /** Creates a new empty message with no values set. */ @@ -61,17 +62,36 @@ public class IsoMessage { isoHeader = header; } + /** Tells the message to encode its bitmap in binary format, even if the message + * itself is encoded as text. */ + public void setBinaryBitmap(boolean flag) { + binBitmap = flag; + } + /** Returns true if the message's bitmap is encoded in binary format, when the message + * is encoded as text. Default is false. */ + public boolean isBinaryBitmap() { + return binBitmap; + } + /** If set, this flag will cause the secondary bitmap to be written even if it's not needed. */ public void setForceSecondaryBitmap(boolean flag) { forceb2 = flag; } + /** Returns true if the secondary bitmap is always included in the message, even + * if it's not needed. Default is false. */ public boolean getForceSecondaryBitmap() { return forceb2; } + /** Sets the encoding to use. */ public void setCharacterEncoding(String value) { + if (value == null) { + throw new IllegalArgumentException("Cannot set null encoding."); + } encoding = value; } + /** Returns the character encoding for Strings inside the message. Default + * is taken from the file.encoding system property. */ public String getCharacterEncoding() { return encoding; } @@ -282,7 +302,7 @@ public class IsoMessage { ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (isoHeader != null) { try { - bout.write(isoHeader.getBytes()); + bout.write(isoHeader.getBytes(encoding)); } catch (IOException ex) { //should never happen, writing to a ByteArrayOutputStream } @@ -293,7 +313,7 @@ public class IsoMessage { bout.write(type & 0xff); } else { try { - bout.write(String.format("%04x", type).getBytes()); + bout.write(String.format("%04x", type).getBytes(encoding)); } catch (IOException ex) { //should never happen, writing to a ByteArrayOutputStream } @@ -316,7 +336,7 @@ public class IsoMessage { bs.set(0); } //Write bitmap to stream - if (binary) { + if (binary || binBitmap) { int pos = 128; int b = 0; for (int i = 0; i < bs.size(); i++) { diff --git a/src/main/java/com/solab/iso8583/IsoValue.java b/src/main/java/com/solab/iso8583/IsoValue.java index 8c0ffe8..d8f1f0d 100644 --- a/src/main/java/com/solab/iso8583/IsoValue.java +++ b/src/main/java/com/solab/iso8583/IsoValue.java @@ -184,7 +184,7 @@ public class IsoValue implements Cloneable { } } else if (type == IsoType.ALPHA) { return type.format(encoder == null ? value.toString() : encoder.encodeField(value), length); - } else if (type == IsoType.LLLVAR || type == IsoType.LLLVAR) { + } else if (type == IsoType.LLVAR || type == IsoType.LLLVAR) { return encoder == null ? value.toString() : encoder.encodeField(value); } else if (value instanceof Date) { return type.format((Date)value);