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 salaman@teknos.com
029 * @author Christopher.Harris@retail-logic.com
030 * @version Id: IFA_LLLBINARY.java,v 1.0 1999/05/15 01:05 salaman Exp 
031 * @see ISOComponent
032 */
033public class IFA_LLLABINARY extends ISOFieldPackager {
034    public IFA_LLLABINARY() {
035        super();
036    }
037    /**
038     * @param len - field len
039     * @param description symbolic descrption
040     */
041    public IFA_LLLABINARY (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       //CJH incorrect IFA_LLLBINARY pack 08/07/04
051   
052        int len;
053        byte[] b = (byte[]) c.getValue();
054    
055        if ( (len=b.length) > getLength() || len>999)
056            throw new ISOException (
057                "invalid len "+len 
058                +" packing IFA_LLLABINARY field "+ c.getKey()
059            );
060        
061        byte[] data = ISOUtil.hexString( (byte[]) c.getValue() ).getBytes();
062        byte[] nb=new byte[ 3 +  data.length];
063        byte[] length = new DecimalFormat("000").format(len).getBytes();
064        System.arraycopy(length, 0, nb, 0, 3);
065        System.arraycopy(data, 0, nb, 3, data.length);
066
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
076     */
077    public int unpack (ISOComponent c, byte[] b, int offset)
078        throws ISOException
079    {
080      //CJH incorrect IFA_LLLBINARY unpack 08/07/04
081        
082      int len = Integer.parseInt(new String(b, offset, 3));       
083      c.setValue (ISOUtil.hex2byte(b, offset + 3, len));
084      return len * 2 + 3;
085        
086      //CJH END
087    }
088    public ISOComponent createComponent(int fieldNumber) {
089        return new ISOBinaryField (fieldNumber);
090    }
091    public int getMaxPackedLength() {
092        return (getLength() << 1) + 3;
093    }
094    public void unpack (ISOComponent c, InputStream in) 
095        throws IOException, ISOException
096    {
097        int len = Integer.parseInt(new String(readBytes (in, 3)));
098        c.setValue (readBytes (in, len));
099    }
100}
101