use of org.jpos.iso.ISOException in project jPOS by jpos.
the class TagSequenceBase method writeTo.
@Override
public synchronized void writeTo(ISOMsg isoMsg) throws ISOException {
int maxField = isoMsg.getMaxField();
List<TagValue> tagValueList = getOrderedList();
int fieldNumber = 0;
for (TagValue tagValue : tagValueList) {
Object value = tagValue.getValue();
if (value != null) {
ISOComponent subField;
if (value instanceof byte[]) {
subField = new ISOBinaryField(fieldNumber + maxField + 1, (byte[]) value);
} else if (value instanceof String) {
subField = new ISOField(fieldNumber + maxField + 1, (String) value);
} else if (value instanceof TagSequence) {
TagSequence subSequence = (TagSequence) tagValue;
subField = new ISOMsg(fieldNumber + maxField + 1);
subSequence.writeTo((ISOMsg) subField);
} else if (value instanceof ISOMsg) {
ISOMsgTagValue subSequence = (ISOMsgTagValue) tagValue;
subField = subSequence.getValue();
subField.setFieldNumber(fieldNumber + maxField + 1);
} else {
throw new ISOException("Unknown TagValue subclass: " + tagValue.getClass());
}
isoMsg.set(new ISOTaggedField(tagValue.getTag(), subField));
}
fieldNumber++;
}
}
use of org.jpos.iso.ISOException in project jPOS by jpos.
the class IF_FSTBINARY method unpack.
/**
* @param c - the Component to unpack
* @param b - binary image
* @param offset - starting offset within the binary image
* @return consumed bytes
* @throws org.jpos.iso.ISOException
*/
public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
if (!(c instanceof ISOField))
throw new ISOException(c.getClass().getName() + " is not an ISOField");
int length = -1;
for (int i = 0; i < getMaxPackedLength(); i++) {
byte dataByte = b[offset + i];
if (dataByte == terminator) {
length = i;
break;
}
}
if (length >= 0) {
byte[] value = new byte[length];
System.arraycopy(b, offset, value, 0, length);
c.setValue(value);
return length + 1;
} else {
throw new ISOException("Terminating Backslash does not exist");
}
}
use of org.jpos.iso.ISOException in project jPOS by jpos.
the class IF_FSTCHAR method unpack.
/**
* @param c - the Component to unpack
* @param b - binary image
* @param offset - starting offset within the binary image
* @return consumed bytes
* @throws org.jpos.iso.ISOException
*/
public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
if (!(c instanceof ISOField))
throw new ISOException(c.getClass().getName() + " is not an ISOField");
int length = -1;
for (int i = 0; i < getMaxPackedLength(); i++) {
byte dataByte = b[offset + i];
if ((char) dataByte == terminator) {
length = i;
break;
}
}
if (length >= 0) {
String value = new String(b, offset, length);
c.setValue(value);
return length + 1;
} else {
throw new ISOException("Terminating Backslash does not exist");
}
}
use of org.jpos.iso.ISOException in project jPOS by jpos.
the class IF_FSTCHAR method unpack.
public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException {
if (!(c instanceof ISOField))
throw new ISOException(c.getClass().getName() + " is not an ISOField");
boolean endFound = false;
if (in.markSupported()) {
in.mark(getMaxPackedLength());
}
ByteBuffer buf = ByteBuffer.allocate(getMaxPackedLength());
for (int i = 0; i < getMaxPackedLength() && in.available() > 0; i++) {
byte dataByte = (byte) in.read();
if ((char) dataByte == terminator) {
endFound = true;
break;
} else {
buf.put(dataByte);
}
}
if (endFound) {
byte[] data = byteBufferToBytes(buf);
String value = new String(data);
c.setValue(value);
} else {
if (in.markSupported()) {
in.reset();
}
throw new ISOException("Terminating Backslash does not exist");
}
}
use of org.jpos.iso.ISOException in project jPOS by jpos.
the class BERTLVPackager method unpack.
public int unpack(ISOComponent m, byte[] b, boolean nested) throws ISOException {
LogEvent evt = new LogEvent(this, "unpack");
try {
if (m.getComposite() == null)
throw new ISOException("Can't call packager on non Composite");
if (b.length == 0)
// nothing to do
return 0;
if (// save a few CPU cycle if no logger available
logger != null)
evt.addMessage(ISOUtil.hexString(b));
int tlvDataLength = b.length;
int consumed = 0;
int subFieldNumber = 1;
if (!nested && fld.length > 1) {
ISOFieldPackager packager = fld[1];
if (packager != null) {
ISOComponent subField = packager.createComponent(1);
consumed = consumed + packager.unpack(subField, b, consumed);
m.set(subField);
}
subFieldNumber++;
}
while (consumed < tlvDataLength) {
ISOFieldPackager packager;
if (!nested && fld.length > 1 && (packager = fld[fld.length - 1]) != null && packager.getLength() == tlvDataLength - consumed) {
ISOComponent subField = packager.createComponent(fld.length - 1);
consumed = consumed + packager.unpack(subField, b, consumed);
m.set(subField);
subFieldNumber++;
} else {
// Read the Tag per BER
UnpackResult tagUnpackResult = unpackTag(b, consumed);
consumed = consumed + tagUnpackResult.consumed;
final byte[] tagBytes = tagUnpackResult.value;
String tag = ISOUtil.byte2hex(tagBytes).toUpperCase();
UnpackResult lengthUnpackResult = unpackLength(b, consumed);
consumed = consumed + lengthUnpackResult.consumed;
int length = ISOUtil.byte2int(lengthUnpackResult.value);
final ISOComponent tlvSubFieldData;
byte[] value = new byte[length];
if (length > 0) {
System.arraycopy(b, consumed, value, 0, value.length);
}
int uninterpretLength = getUninterpretLength(length, valueInterpreter);
byte[] rawValueBytes = valueInterpreter.uninterpret(value, 0, uninterpretLength);
tlvSubFieldData = unpackValue(tag, rawValueBytes, subFieldNumber, length);
consumed = consumed + length;
ISOTaggedField tlv = new ISOTaggedField(tag, tlvSubFieldData);
m.set(tlv);
subFieldNumber++;
}
}
if (b.length != consumed) {
evt.addMessage("WARNING: unpack len=" + b.length + " consumed=" + consumed);
}
return consumed;
} catch (ISOException e) {
evt.addMessage(e);
throw e;
} catch (Exception e) {
evt.addMessage(e);
throw new ISOException(e);
} finally {
Logger.log(evt);
}
}
Aggregations