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.util.BitSet;
024
025/**
026 * ASCII packaged Bitmap
027 *
028 * @author apr@cs.com.uy
029 * @version $Id$
030 * @see ISOComponent
031 * @see ISOBitMapPackager
032 */
033public class IFA_BITMAP extends ISOBitMapPackager {
034    public IFA_BITMAP() {
035        super();
036    }
037    /**
038     * @param len - field len
039     * @param description symbolic descrption
040     */
041    public IFA_BITMAP(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        BitSet b = (BitSet) c.getValue();
051        int len =
052            getLength() >= 8 ?
053                    b.length()+62 >>6 <<3 : getLength();
054        return ISOUtil.hexString(ISOUtil.bitSet2byte (b, len)).getBytes();
055    }
056
057    public int getMaxPackedLength() {
058        return getLength() >> 2;
059    }
060    /**
061     * @param c - the Component to unpack
062     * @param b - binary image
063     * @param offset - starting offset within the binary image
064     * @return consumed bytes
065     * @exception ISOException
066     */
067    public int unpack (ISOComponent c, byte[] b, int offset)
068        throws ISOException
069    {
070        int len;
071        BitSet bmap = ISOUtil.hex2BitSet (b, offset, getLength() << 3);
072        c.setValue(bmap);
073        len = bmap.get(1) ? 128 : 64; /* changed by Hani */
074        if (getLength() > 16 && bmap.get(65)) {
075            len = 192;
076            bmap.clear(65);
077        }
078        return Math.min (getLength() << 1, len >> 2);
079    }
080    public void unpack (ISOComponent c, InputStream in) 
081        throws IOException, ISOException
082    {
083        BitSet bmap = ISOUtil.hex2BitSet (new BitSet (64), readBytes (in, 16), 0);
084        if (getLength() > 8 && bmap.get (1)) {
085            ISOUtil.hex2BitSet (bmap, readBytes (in, 16), 64);
086        }
087        c.setValue(bmap);
088    }
089}