decoding CompositeField for #17
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
package com.solab.iso8583.codecs;
|
package com.solab.iso8583.codecs;
|
||||||
|
|
||||||
import com.solab.iso8583.CustomBinaryField;
|
import com.solab.iso8583.CustomBinaryField;
|
||||||
|
import com.solab.iso8583.IsoType;
|
||||||
import com.solab.iso8583.IsoValue;
|
import com.solab.iso8583.IsoValue;
|
||||||
import com.solab.iso8583.parse.FieldParseInfo;
|
import com.solab.iso8583.parse.FieldParseInfo;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -18,6 +22,7 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class CompositeField implements CustomBinaryField<CompositeField> {
|
public class CompositeField implements CustomBinaryField<CompositeField> {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(CompositeField.class);
|
||||||
private boolean binary;
|
private boolean binary;
|
||||||
/** Stores the subfields. */
|
/** Stores the subfields. */
|
||||||
private List<IsoValue<?>> values;
|
private List<IsoValue<?>> values;
|
||||||
@@ -31,7 +36,7 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
|||||||
return binary;
|
return binary;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(List<IsoValue<?>> values) {
|
public void setValues(List<IsoValue<?>> values) {
|
||||||
this.values = values;
|
this.values = values;
|
||||||
}
|
}
|
||||||
public List<IsoValue<?>> getValues() {
|
public List<IsoValue<?>> getValues() {
|
||||||
@@ -60,8 +65,62 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompositeField decodeBinaryField(byte[] value, int offset, int length) {
|
public CompositeField decodeBinaryField(byte[] buf, int offset, int length) {
|
||||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
List<IsoValue<?>> vals = new ArrayList<IsoValue<?>>(parsers.size());
|
||||||
|
int pos = 0;
|
||||||
|
try {
|
||||||
|
for (FieldParseInfo fpi : parsers) {
|
||||||
|
IsoValue<?> v = fpi.parseBinary(0, buf, pos, null);
|
||||||
|
if (v != null) {
|
||||||
|
pos += v.getLength();
|
||||||
|
if (v.getType() == IsoType.LLVAR || v.getType() == IsoType.LLBIN) {
|
||||||
|
pos++;
|
||||||
|
} else if (v.getType() == IsoType.LLLVAR || v.getType() == IsoType.LLLBIN) {
|
||||||
|
pos+=2;
|
||||||
|
}
|
||||||
|
vals.add(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final CompositeField f = new CompositeField();
|
||||||
|
f.setValues(vals);
|
||||||
|
return f;
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
log.error("Decoding binary CompositeField", ex);
|
||||||
|
return null;
|
||||||
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
log.error("Decoding binary CompositeField", ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompositeField decodeField(String value) {
|
||||||
|
List<IsoValue<?>> vals = new ArrayList<IsoValue<?>>(parsers.size());
|
||||||
|
byte[] buf = value.getBytes();
|
||||||
|
int pos = 0;
|
||||||
|
try {
|
||||||
|
for (FieldParseInfo fpi : parsers) {
|
||||||
|
IsoValue<?> v = fpi.parse(0, buf, pos, null);
|
||||||
|
if (v != null) {
|
||||||
|
pos += v.getLength();
|
||||||
|
if (v.getType() == IsoType.LLVAR || v.getType() == IsoType.LLBIN) {
|
||||||
|
pos+=2;
|
||||||
|
} else if (v.getType() == IsoType.LLLVAR || v.getType() == IsoType.LLLBIN) {
|
||||||
|
pos+=3;
|
||||||
|
}
|
||||||
|
vals.add(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final CompositeField f = new CompositeField();
|
||||||
|
f.setValues(vals);
|
||||||
|
return f;
|
||||||
|
} catch (ParseException ex) {
|
||||||
|
log.error("Decoding CompositeField", ex);
|
||||||
|
return null;
|
||||||
|
} catch (UnsupportedEncodingException ex) {
|
||||||
|
log.error("Decoding CompositeField", ex);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -72,16 +131,12 @@ public class CompositeField implements CustomBinaryField<CompositeField> {
|
|||||||
v.write(bout, binary, true);
|
v.write(bout, binary, true);
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
|
log.error("Encoding binary CompositeField", ex);
|
||||||
//shouldn't happen
|
//shouldn't happen
|
||||||
}
|
}
|
||||||
return bout.toByteArray();
|
return bout.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompositeField decodeField(String value) {
|
|
||||||
return null; //To change body of implemented methods use File | Settings | File Templates.
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String encodeField(CompositeField value) {
|
public String encodeField(CompositeField value) {
|
||||||
if (binary)return null;
|
if (binary)return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user