From 5333413e30d8819d98cbcafc6e6aa057d69bae55 Mon Sep 17 00:00:00 2001 From: Enrique Zamudio Date: Wed, 27 Nov 2013 11:02:19 -0600 Subject: [PATCH] improvements to CompositeField --- .../solab/iso8583/codecs/CompositeField.java | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/solab/iso8583/codecs/CompositeField.java b/src/main/java/com/solab/iso8583/codecs/CompositeField.java index 51e9049..e7f5b8b 100644 --- a/src/main/java/com/solab/iso8583/codecs/CompositeField.java +++ b/src/main/java/com/solab/iso8583/codecs/CompositeField.java @@ -23,19 +23,11 @@ import java.util.List; public class CompositeField implements CustomBinaryField { private static final Logger log = LoggerFactory.getLogger(CompositeField.class); - private boolean binary; /** Stores the subfields. */ private List> values; /** Stores the parsers for the subfields. */ private List parsers; - public void setBinary(boolean flag) { - binary = flag; - } - public boolean isBinary() { - return binary; - } - public void setValues(List> values) { this.values = values; } @@ -49,6 +41,10 @@ public class CompositeField implements CustomBinaryField { values.add(v); return this; } + public Object getObjectValue(int idx) { + if (idx < 0 || idx > values.size())return null; + return values.get(idx).getValue(); + } public void setParsers(List fpis) { parsers = fpis; @@ -133,8 +129,8 @@ public class CompositeField implements CustomBinaryField { public byte[] encodeBinaryField(CompositeField value) { final ByteArrayOutputStream bout = new ByteArrayOutputStream(); try { - for (IsoValue v : values) { - v.write(bout, binary, true); + for (IsoValue v : value.getValues()) { + v.write(bout, true, true); } } catch (IOException ex) { log.error("Encoding binary CompositeField", ex); @@ -145,17 +141,35 @@ public class CompositeField implements CustomBinaryField { @Override public String encodeField(CompositeField value) { - if (binary)return null; - final byte[] buf = encodeBinaryField(value); - String encoding = null; - for (IsoValue v : values) { - if (encoding == null)encoding = v.getCharacterEncoding(); - } + final ByteArrayOutputStream bout = new ByteArrayOutputStream(); + byte[] buf = null; try { + String encoding = null; + for (IsoValue v : value.getValues()) { + v.write(bout, true, true); + if (encoding == null)encoding = v.getCharacterEncoding(); + } + buf = bout.toByteArray(); return new String(buf, encoding==null?"UTF-8":encoding); } catch (UnsupportedEncodingException ex) { - return new String(buf); + log.error("Encoding text CompositeField", ex); + } catch (IOException ex) { + log.error("Encoding text CompositeField", ex); + //shouldn't happen } + return new String(buf); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("CompositeField["); + if (values!=null) { + boolean first=true; + for (IsoValue v : values) { + if (first)first=false;else sb.append(','); + sb.append(v.getType()); + } + } + return sb.append(']').toString(); + } }