use of org.jpos.iso.ISOFieldPackager 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);
}
}
use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.
the class Base1SubFieldPackagerTest method testGetFirstField.
@Test
public void testGetFirstField() throws Throwable {
Base1SubFieldPackager base1SubFieldPackager = new Base1SubFieldPackager();
ISOFieldPackager[] fld = new ISOFieldPackager[2];
fld[0] = new X92_BITMAP(100, "testBase1SubFieldPackagerDescription");
base1SubFieldPackager.setFieldPackager(fld);
int result = base1SubFieldPackager.getFirstField();
assertEquals("result", 1, result);
}
use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.
the class CTCSubElementPackagerTest method testUnpackThrowsISOException.
@Test
public void testUnpackThrowsISOException() throws Throwable {
CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
ISOFieldPackager[] fld = new ISOFieldPackager[2];
cTCSubElementPackager.setFieldPackager(fld);
cTCSubElementPackager.setLogger(new Logger(), "testCTCSubElementPackagerRealm");
cTCSubElementPackager.setFieldPackager(0, new IFE_CHAR());
byte[] b = new byte[2];
try {
cTCSubElementPackager.unpack(new ISOVField(new ISOField(100, "testCTCSubElementPackagerv"), null), b);
fail("Expected ISOException to be thrown");
} catch (ISOException ex) {
assertEquals("ex.getMessage()", "Can't add to Leaf", ex.getMessage());
assertNull("ex.getNested()", ex.getNested());
}
}
use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.
the class CTCSubElementPackagerTest method testPackThrowsISOException.
@Test
public void testPackThrowsISOException() throws Throwable {
CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
ISOFieldPackager[] fld = new ISOFieldPackager[2];
cTCSubElementPackager.setFieldPackager(fld);
try {
cTCSubElementPackager.pack(new ISOMsg("testCTCSubElementPackagerMti"));
fail("Expected ISOException to be thrown");
} catch (ISOException ex) {
assertEquals("ex.getNested().getClass()", ClassCastException.class, ex.getNested().getClass());
}
}
use of org.jpos.iso.ISOFieldPackager in project jPOS by jpos.
the class CTCSubElementPackagerTest method testPack1.
@Test
public void testPack1() throws Throwable {
CTCSubElementPackager cTCSubElementPackager = new CTCSubElementPackager();
ISOFieldPackager[] fld = new ISOFieldPackager[0];
cTCSubElementPackager.setFieldPackager(fld);
byte[] result = cTCSubElementPackager.pack(new ISOMsg());
assertEquals("result.length", 0, result.length);
}
Aggregations