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    /** Default constructor. */
035    public IFA_BITMAP() {
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_BITMAP(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        BitSet b = (BitSet) c.getValue();
053        int len =
054            getLength() >= 8 ?
055                    b.length()+62 >>6 <<3 : getLength();
056        return ISOUtil.hexString(ISOUtil.bitSet2byte (b, len)).getBytes();
057    }
058
059    public int getMaxPackedLength() {
060        return getLength() >> 2;
061    }
062    /**
063     * @param c - the Component to unpack
064     * @param b - binary image
065     * @param offset - starting offset within the binary image
066     * @return consumed bytes
067     * @exception ISOException on ISO processing error
068     */
069    public int unpack (ISOComponent c, byte[] b, int offset)
070        throws ISOException
071    {
072        int len;
073        BitSet bmap = ISOUtil.hex2BitSet (b, offset, getLength() << 3);
074        c.setValue(bmap);
075        len = bmap.get(1) ? 128 : 64; /* changed by Hani */
076        if (getLength() > 16 && bmap.get(65)) {
077            len = 192;
078            bmap.clear(65);
079        }
080        return Math.min (getLength() << 1, len >> 2);
081    }
082    public void unpack (ISOComponent c, InputStream in) 
083        throws IOException, ISOException
084    {
085        BitSet bmap = ISOUtil.hex2BitSet (new BitSet (64), readBytes (in, 16), 0);
086        if (getLength() > 8 && bmap.get (1)) {
087            ISOUtil.hex2BitSet (bmap, readBytes (in, 16), 64);
088        }
089        c.setValue(bmap);
090    }
091}