private byte[] packLength(final byte[] valueBytes) { final byte[] lengthBytes; int length = valueBytes.length; if (length > 0x7F) { byte[] lengthBytesSuffix = ISOUtil.int2byte(length); lengthBytes = new byte[lengthBytesSuffix.length + 1]; lengthBytes[0] = (byte) (0x80 | lengthBytesSuffix.length); System.arraycopy(lengthBytesSuffix, 0, lengthBytes, 1, lengthBytesSuffix.length); } else { lengthBytes = new byte[]{(byte) length}; } byte[] packedLengthBytes = new byte[lengthInterpreter.getPackedLength(lengthBytes.length)]; lengthInterpreter.interpret(lengthBytes, packedLengthBytes, 0); return packedLengthBytes; }
private UnpackResult unpackLength(final byte[] tlvData, final int offset) { byte[] tlvBytesHex = lengthInterpreter.uninterpret( tlvData, offset, tlvData.length >= offset + MAX_LENGTH_BYTES ? MAX_LENGTH_BYTES : tlvData.length - offset); final byte length = tlvBytesHex[0]; final int lengthLength; final byte[] lengthBytes; if ((length & 0x80) == 0x80) { //Long Form int lengthOctetsCount = length & 0x7F; lengthLength = lengthOctetsCount + 1; lengthBytes = new byte[lengthOctetsCount]; System.arraycopy(tlvBytesHex, 1, lengthBytes, 0, lengthOctetsCount); } else { //Short Form lengthLength = 1; lengthBytes = new byte[]{length}; } return new UnpackResult(lengthBytes, lengthInterpreter.getPackedLength(lengthLength)); }
@Test public void uninterpret() { int offset = 0; int length = EBCDICDATA.length; byte[] result = interpreter.uninterpret(EBCDICDATA, offset, length); assertThat(result, is(binaryData)); } }
@Test public void interpret() { byte[] result = new byte[binaryData.length]; interpreter.interpret(binaryData, result, 0); assertThat(result, is(EBCDICDATA)); }
private int getUninterpretLength(int length, BinaryInterpreter interpreter) { int lengthAdjusted = length + length % 2; return length * (lengthAdjusted / interpreter.getPackedLength(lengthAdjusted)); }
valueInterpreter.uninterpret(value, 0, uninterpretLength);
public int getMaxPackedLength() { return prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
private UnpackResult unpackTag(final byte[] tlvData, final int offset) { byte[] tlvBytesHex = tagInterpreter.uninterpret( tlvData, offset, tlvData.length >= offset + MAX_TAG_BYTES ? MAX_TAG_BYTES : tlvData.length - offset); int index = 0; final byte[] tagBytes; byte tagByte = tlvBytesHex[index]; int tagLength = 1; if ((tagByte & 0x1F) == 0x1F) { tagLength++; tagByte = tlvBytesHex[index + 1]; while (/* tagLength < MAX_TAG_BYTES && */(tagByte & 0x80) == 0x80) { tagLength++; tagByte = tlvBytesHex[index + tagLength - 1]; } tagBytes = new byte[tagLength]; System.arraycopy(tlvBytesHex, index, tagBytes, 0, tagBytes.length); } else { tagBytes = new byte[]{tagByte}; } return new UnpackResult(tagBytes, tagInterpreter.getPackedLength(tagLength)); }
private byte[] packTag(final ISOTaggedField c) { final byte[] tagBytes; String tag = c.getTag(); tagBytes = ISOUtil.hex2byte(tag); byte[] packedTagBytes = new byte[tagInterpreter.getPackedLength(tagBytes.length)]; tagInterpreter.interpret(tagBytes, packedTagBytes, 0); return packedTagBytes; }
/** * Returns the prefixer's packed length and the interpreter's packed length. * @see org.jpos.iso.ISOFieldPackager#getMaxPackedLength() */ @Override public int getMaxPackedLength() { return tagPrefixer.getPackedLength() + prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException { try { int len = prefixer.decodeLength(b, offset); if (len == -1) { // The prefixer doesn't know how long the field is, so use // maxLength instead len = getLength(); } else if (getLength() > 0 && len > getLength()) throw new ISOException("Field length " + len + " too long. Max: " + getLength()); int lenLen = prefixer.getPackedLength(); byte[] unpacked = interpreter.uninterpret(b, offset + lenLen, len); c.setValue(unpacked); return lenLen + interpreter.getPackedLength(len); } catch(Exception e) { throw new ISOException(makeExceptionMessage(c, "unpacking"), e); } }
private byte[] packTLV(ISOTaggedField c) throws ISOException { byte[] b; final byte[] rawValueBytes; try { rawValueBytes = packValue(c.getTag(), c); } catch (UnknownTagNumberException e) { throw new ISOException(e); } byte[] valueBytes = new byte[valueInterpreter.getPackedLength(rawValueBytes.length)]; valueInterpreter.interpret(rawValueBytes, valueBytes, 0); byte[] tagBytes = packTag(c); byte[] lengthBytes = packLength(valueBytes); b = new byte[tagBytes.length + lengthBytes.length + valueBytes.length]; System.arraycopy(tagBytes, 0, b, 0, tagBytes.length); System.arraycopy(lengthBytes, 0, b, tagBytes.length, lengthBytes.length); System.arraycopy(valueBytes, 0, b, tagBytes.length + lengthBytes.length, valueBytes.length); return b; }
/** Unpack from an input stream */ public void unpack (ISOComponent c, InputStream in) throws IOException, ISOException { try { int lenLen = prefixer.getPackedLength (); int len; if (lenLen == 0) { len = getLength(); } else { len = prefixer.decodeLength (readBytes (in, lenLen), 0); if (getLength() > 0 && len > 0 && len > getLength()) throw new ISOException("Field length " + len + " too long. Max: " + getLength()); } int packedLen = interpreter.getPackedLength(len); byte[] unpacked = interpreter.uninterpret(readBytes (in, packedLen), 0, len); c.setValue(unpacked); } catch(ISOException e) { throw new ISOException(makeExceptionMessage(c, "unpacking"), e); } }
/** * Convert the component into a byte[]. */ public byte[] pack(ISOComponent c) throws ISOException { try { byte[] data = c.getBytes(); int packedLength = prefixer.getPackedLength(); if (packedLength == 0 && data.length != getLength()) { throw new ISOException("Binary data length not the same as the packager length (" + data.length + "/" + getLength() + ")"); } byte[] ret = new byte[interpreter.getPackedLength(data.length) + packedLength]; prefixer.encodeLength(data.length, ret); interpreter.interpret(data, ret, packedLength); return ret; } catch(Exception e) { throw new ISOException(makeExceptionMessage(c, "packing"), e); } }
/** * Unpacks the byte array into the component. * @param c The component to unpack into. * @param b The byte array to unpack. * @param offset The index in the byte array to start unpacking from. * @return The number of bytes consumed unpacking the component. */ @Override public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException { try{ int tagLen = tagPrefixer.getPackedLength(); c.setFieldNumber(tagPrefixer.decodeLength(b, offset)); int len = prefixer.decodeLength(b, offset + tagLen); if (len == -1) { // The prefixer doesn't know how long the field is, so use // maxLength instead len = getLength(); } else if (getLength() > 0 && len > getLength()) throw new ISOException("Field length " + len + " too long. Max: " + getLength()); int lenLen = prefixer.getPackedLength(); byte[] unpacked = interpreter.uninterpret(b, offset + tagLen + lenLen, len); c.setValue(unpacked); return tagLen + lenLen + interpreter.getPackedLength(len); } catch(Exception e){ throw new ISOException(makeExceptionMessage(c, "unpacking"), e); } }
/** * Convert the component into a byte[]. */ @Override public byte[] pack(ISOComponent c) throws ISOException { try{ byte[] data = (byte[])c.getValue(); if (data.length > getLength()) throw new ISOException("Field length " + data.length + " too long. Max: " + getLength()); int tag = (Integer)c.getKey(); byte[] paddedData = data; if (!(padder instanceof NullPadder)) //for save few cycles paddedData = ISOUtil.hex2byte(padder.pad(ISOUtil.hexString(data), getLength())); byte[] rawData = new byte[tagPrefixer.getPackedLength()+prefixer.getPackedLength() + interpreter.getPackedLength(paddedData.length)]; tagPrefixer.encodeLength(tag, rawData); byte[] rawLen = new byte[prefixer.getPackedLength()]; prefixer.encodeLength(paddedData.length, rawLen); System.arraycopy(rawLen, 0, rawData, tagPrefixer.getPackedLength(), rawLen.length); interpreter.interpret(paddedData, rawData, tagPrefixer.getPackedLength()+rawLen.length); return rawData; } catch(Exception e) { throw new ISOException(makeExceptionMessage(c, "packing"), e); } }
/** * Unpack the input stream into the component. * @param c The Component to unpack into. * @param in Input stream where the packed bytes come from. * @exception IOException Thrown if there's a problem reading the input stream. */ @Override public void unpack (ISOComponent c, InputStream in) throws IOException, ISOException { try{ int tagLen = tagPrefixer.getPackedLength(); c.setFieldNumber(tagPrefixer.decodeLength(readBytes (in, tagLen), 0)); int lenLen = prefixer.getPackedLength (); int len; if (lenLen == 0) len = getLength(); else { len = prefixer.decodeLength (readBytes (in, lenLen), 0); if (getLength() > 0 && len > 0 && len > getLength()) throw new ISOException("Field length " + len + " too long. Max: " + getLength()); } int packedLen = interpreter.getPackedLength(len); byte[] unpacked = interpreter.uninterpret(readBytes (in, packedLen), 0, len); c.setValue(unpacked); } catch(ISOException e){ throw new ISOException(makeExceptionMessage(c, "unpacking"), e); } }