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    /** Default constructor. */
035    public IFA_LLLABINARY() {
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_LLLABINARY (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       //CJH incorrect IFA_LLLBINARY pack 08/07/04
053   
054        int len;
055        byte[] b = (byte[]) c.getValue();
056    
057        if ( (len=b.length) > getLength() || len>999)
058            throw new ISOException (
059                "invalid len "+len 
060                +" packing IFA_LLLABINARY field "+ c.getKey()
061            );
062        
063        byte[] data = ISOUtil.hexString( (byte[]) c.getValue() ).getBytes();
064        byte[] nb=new byte[ 3 +  data.length];
065        byte[] length = new DecimalFormat("000").format(len).getBytes();
066        System.arraycopy(length, 0, nb, 0, 3);
067        System.arraycopy(data, 0, nb, 3, data.length);
068
069        return nb;
070//      CJH END
071     }
072    /**
073     * @param c - the Component to unpack
074     * @param b - binary image
075     * @param offset - starting offset within the binary image
076     * @return consumed bytes
077     * @exception ISOException on ISO processing error
078     */
079    public int unpack (ISOComponent c, byte[] b, int offset)
080        throws ISOException
081    {
082      //CJH incorrect IFA_LLLBINARY unpack 08/07/04
083        
084      int len = Integer.parseInt(new String(b, offset, 3));       
085      c.setValue (ISOUtil.hex2byte(b, offset + 3, len));
086      return len * 2 + 3;
087        
088      //CJH END
089    }
090    public ISOComponent createComponent(int fieldNumber) {
091        return new ISOBinaryField (fieldNumber);
092    }
093    public int getMaxPackedLength() {
094        return (getLength() << 1) + 3;
095    }
096    public void unpack (ISOComponent c, InputStream in) 
097        throws IOException, ISOException
098    {
099        int len = Integer.parseInt(new String(readBytes (in, 3)));
100        c.setValue (readBytes (in, len));
101    }
102}
103