private int getUninterpretLength(int length, Interpreter interpreter) { int lengthAdjusted = length + length % 2; return length * (lengthAdjusted / interpreter.getPackedLength(lengthAdjusted)); }
@Override public byte[] pack(Map<String, String> fields) throws ISOException { if (value == null || value.equals("")) { // if field is not set, make sure to send the delimiter to indicate // its presence. return new byte[] { delimiter.byteValue() }; } if (value.length() <= maxSize) { byte[] b = new byte[interpreter.getPackedLength(value.length() + 1)]; interpreter.interpret(value, b, 0); b[b.length - 1] = delimiter.byteValue(); return b; } throw new ISOException(String.format("Size [%d] is greater than maxSize[%d] ", value.length(), maxSize)); }
/** * Returns the prefixer's packed length and the interpreter's packed length. */ public int getMaxPackedLength() { return prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
/** * Returns the prefixer's packed length and the interpreter's packed length. */ public int getMaxPackedLength() { return prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
/** * Returns the prefixer's packed length and the interpreter's packed length. */ public int getMaxPackedLength() { return prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
@Override public byte[] hexDump(String prefix, Map<String, String> setfields) { int numberOfPackedBytes = interpreter.getPackedLength(getValue().length()); String compliant = compliance.makeCompliant(getValue()); byte[] temp = new byte[numberOfPackedBytes+1];// +1 for the delimiter try { interpreter.interpret(compliant, temp, 0); temp[temp.length-1]= delimiter.byteValue(); } catch (ISOException e) { return null; } return temp; }
/** * Returns the prefixer's packed length and the interpreter's packed length. */ public int getMaxPackedLength() { return tagPrefixer.getPackedLength() + prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
public void testGetPackedLength() { assertEquals(3, inter.getPackedLength(3)); }
public void testGetPackedLength() { assertEquals(3, inter.getPackedLength(3)); }
public void testGetPackedLength() throws Exception { assertEquals(3, inter.getPackedLength(3)); }
@Override public byte[] hexDump(String prefix,Map<String, String> setfields) { int numberOfPackedBytes = interpretter.getPackedLength(getValue().length()); String compliant = compliance.makeCompliant(getValue()); byte[] temp = new byte[numberOfPackedBytes]; try { interpretter.interpret(compliant, temp, 0); } catch (ISOException e) { // TODO Auto-generated catch block return null; } return temp; } @Override
@Override public int unpack(byte[] inStream, int offset, Map<String, String> fields) throws ISOException { int packedSize = interpretter.getPackedLength(size); if (inStream.length - offset < packedSize) { throw new ISOException(String.format("Field [%s] at offset [%d]:Expecting %d bytes found %d", getName(),offset,packedSize, inStream.length - offset)); } String interprettedvalue = interpretter.uninterpret(inStream, offset, size); if (getValue() != null) { if (!getValue().equals(interprettedvalue)) { throw new ISOException(String.format("Field [%s] at offset [%d]:Expected %s but found %s", getName(),offset,getValue(), interprettedvalue)); } } fields.put(getName(),interprettedvalue); value = interprettedvalue; return offset + packedSize; }
@Override public byte[] pack(Map<String, String> fields) throws ISOException { if (value== null) {// if the hardcoded value is in the constructor , use it. value = fields.get(getName()); } else { fields.put(getName(), value); } if (value==null){ throw new ISOException(String.format("Field [%s]: Unable to pack as field is not set",getName())); } if (value.length() <= size) { if (padLeft){ ISOUtil.padleft(getValue(), size,(char) padCharacter.byteValue() ); } else { ISOUtil.padright(getValue(), size,(char) padCharacter.byteValue() ); } } else { throw new ISOException(String.format("Field [%s]:Cannot pack as data has size %d and size needs to be %d",getName(), value.length(),size)); } byte[] packedbyte = new byte[interpretter.getPackedLength(size)]; interpretter.interpret(getValue(), packedbyte, 0); return packedbyte; }
/** * Convert the component into a byte[]. */ public byte[] pack(ISOComponent c) throws ISOException { try { String data = (String)c.getValue(); if (data.length() > getLength()) { throw new ISOException("Field length " + data.length() + " too long. Max: " + getLength()); } String paddedData = padder.pad(data, getLength()); byte[] rawData = new byte[prefixer.getPackedLength() + interpreter.getPackedLength(paddedData.length())]; prefixer.encodeLength(data.length(), rawData); interpreter.interpret(paddedData, rawData, prefixer.getPackedLength()); return rawData; } catch(Exception e) { throw new ISOException(makeExceptionMessage(c, "packing"), e); } }
/** * Convert the component into a byte[]. * @return byte array representation of component * @throws org.jpos.iso.ISOException */ @Override public byte[] pack(ISOComponent c) throws ISOException { try { String data; if(c.getValue() instanceof byte[]) data = new String(c.getBytes(), ISOUtil.CHARSET); // transparent handling of complex fields else data = (String)c.getValue(); if (data.length() > getLength()) { throw new ISOException("Field length " + data.length() + " too long. Max: " + getLength()); } String paddedData = padder.pad(data, getLength()); byte[] rawData = new byte[prefixer.getPackedLength() + interpreter.getPackedLength(paddedData.length())]; prefixer.encodeLength(paddedData.length(), rawData); interpreter.interpret(paddedData, rawData, prefixer.getPackedLength()); return rawData; } catch(Exception e) { throw new ISOException(makeExceptionMessage(c, "packing"), e); } }
/** * Convert the component into a byte[]. */ public byte[] pack(ISOComponent c) throws ISOException { try{ String data = (String)c.getValue(); if (data.length() > getLength()) throw new ISOException("Field length " + data.length() + " too long. Max: " + getLength()); int tag = (Integer)c.getKey(); String paddedData = padder.pad(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); } }
/** * 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. */ 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(); String 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); } }
/** * 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. */ 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); String unpacked = interpreter.uninterpret(readBytes (in, packedLen), 0, len); c.setValue(unpacked); } catch(ISOException e) { throw new ISOException(makeExceptionMessage(c, "unpacking"), 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. */ 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); String unpacked = interpreter.uninterpret(readBytes (in, packedLen), 0, len); c.setValue(unpacked); } catch(ISOException e) { throw new ISOException(makeExceptionMessage(c, "unpacking"), 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. */ 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(); String 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); } }