001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.text.DecimalFormat;
024
025/**
026 * ISOFieldPackager ASCII variable len BINARY
027 *
028 * @author apr@cs.com.uy
029 * @author Christopher.Harris@retail-logic.com
030 * @version Id: $
031 * @see ISOComponent
032 */
033public class IFA_LLABINARY extends ISOFieldPackager {
034    public IFA_LLABINARY() {
035        super();
036    }
037    /**
038     * @param len - field len
039     * @param description symbolic descrption
040     */
041    public IFA_LLABINARY (int len, String description) {
042        super(len, description);
043    }
044    /**
045     * @param c - a component
046     * @return packed component
047     * @exception ISOException
048     */
049    public byte[] pack (ISOComponent c) throws ISOException {
050        int len;
051        byte[] b = (byte[]) c.getValue();
052    
053        if ( (len=b.length) > getLength() || len>99)
054            throw new ISOException (
055                "invalid len "+len 
056                +" packing field "+ c.getKey()
057            );
058        //CJH incorrect IFA_LLBINARY pack 08/07/04
059        byte[] data = ISOUtil.hexString( (byte[]) c.getValue() ).getBytes();
060        byte[] nb=new byte[ 2 +  data.length];
061        
062        byte[] length = new DecimalFormat("00").format(len).getBytes();
063        System.arraycopy(length, 0, nb, 0, 2);
064        System.arraycopy(data, 0, nb, 2, data.length);
065        return nb;
066        //CJH END.
067    }
068    /**
069     * @param c - the Component to unpack
070     * @param b - binary image
071     * @param offset - starting offset within the binary image
072     * @return consumed bytes
073     * @exception ISOException
074     */
075    public int unpack (ISOComponent c, byte[] b, int offset)
076        throws ISOException
077    {
078      //CJH incorrect IFA_LLBINARY unpack 08/07/04
079         
080      int len = Integer.parseInt(new String(b, offset, 2));       
081      c.setValue (ISOUtil.hex2byte(b, offset + 2, len));
082      return len * 2 + 2;
083
084      //CJH END.
085        
086        
087    }
088    public ISOComponent createComponent(int fieldNumber) {
089        return new ISOBinaryField (fieldNumber);
090    }
091    public int getMaxPackedLength() {
092        return (getLength() << 1) + 2;
093    }
094    public void unpack (ISOComponent c, InputStream in) 
095        throws IOException, ISOException
096    {
097        int len = Integer.parseInt(new String(readBytes (in, 2)));
098        c.setValue (readBytes (in, len));
099    }
100}
101