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    /** Default constructor. */
035    public IFA_LLABINARY() {
036        super();
037    }
038    /**
039     * Constructs a packager with the given length and description.
040     * @param len - field len
041     * @param description symbolic descrption
042     */
043    public IFA_LLABINARY (int len, String description) {
044        super(len, description);
045    }
046    /**
047     * @param c - a component
048     * @return packed component
049     * @exception ISOException on ISO processing error
050     */
051    public byte[] pack (ISOComponent c) throws ISOException {
052        int len;
053        byte[] b = (byte[]) c.getValue();
054    
055        if ( (len=b.length) > getLength() || len>99)
056            throw new ISOException (
057                "invalid len "+len 
058                +" packing field "+ c.getKey()
059            );
060        //CJH incorrect IFA_LLBINARY pack 08/07/04
061        byte[] data = ISOUtil.hexString( (byte[]) c.getValue() ).getBytes();
062        byte[] nb=new byte[ 2 +  data.length];
063        
064        byte[] length = new DecimalFormat("00").format(len).getBytes();
065        System.arraycopy(length, 0, nb, 0, 2);
066        System.arraycopy(data, 0, nb, 2, data.length);
067        return nb;
068        //CJH END.
069    }
070    /**
071     * @param c - the Component to unpack
072     * @param b - binary image
073     * @param offset - starting offset within the binary image
074     * @return consumed bytes
075     * @exception ISOException on ISO processing error
076     */
077    public int unpack (ISOComponent c, byte[] b, int offset)
078        throws ISOException
079    {
080      //CJH incorrect IFA_LLBINARY unpack 08/07/04
081         
082      int len = Integer.parseInt(new String(b, offset, 2));       
083      c.setValue (ISOUtil.hex2byte(b, offset + 2, len));
084      return len * 2 + 2;
085
086      //CJH END.
087        
088        
089    }
090    public ISOComponent createComponent(int fieldNumber) {
091        return new ISOBinaryField (fieldNumber);
092    }
093    public int getMaxPackedLength() {
094        return (getLength() << 1) + 2;
095    }
096    public void unpack (ISOComponent c, InputStream in) 
097        throws IOException, ISOException
098    {
099        int len = Integer.parseInt(new String(readBytes (in, 2)));
100        c.setValue (readBytes (in, len));
101    }
102}
103