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