/** * Returns the prefixer's packed length and the interpreter's packed length. */ public int getMaxPackedLength() { return tagPrefixer.getPackedLength() + prefixer.getPackedLength() + interpreter.getPackedLength(getLength()); }
/** * 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); } }
/** * 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); } }
/** * 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); String unpacked = interpreter.uninterpret(readBytes (in, packedLen), 0, len); c.setValue(unpacked); } catch(ISOException e){ throw new ISOException(makeExceptionMessage(c, "unpacking"), e); } }