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.packager;
020
021import org.jpos.iso.ISOBitMapPackager;
022import org.jpos.iso.ISOComponent;
023import org.jpos.iso.ISOException;
024import org.jpos.iso.ISOUtil;
025
026import java.util.BitSet;
027
028/**
029 * ISOFieldPackager Binary Bitmap
030 *
031 * @author <a href="mailto:eoin.flood@orbiscom.com">Eoin Flood</a>
032 * @version $Id$
033 * @see ISOComponent
034 * @see ISOBitMapPackager
035 */
036public class Base1_BITMAP126 extends ISOBitMapPackager 
037{
038    /** Default constructor. */
039    public Base1_BITMAP126()
040    {
041        super();
042    }
043    /**
044    * Creates a Base1_BITMAP126 with the given length and description.
045    * @param len - field len
046    * @param description symbolic description
047    */
048    public Base1_BITMAP126(int len, String description) 
049    {
050        super(len, description);
051    }
052    /**
053    * @param c - a component
054    * @return packed component
055    * @throws ISOException on packing/unpacking error
056    */
057    public byte[] pack (ISOComponent c) throws ISOException 
058    {
059        return ISOUtil.bitSet2byte ((BitSet) c.getValue());
060    }
061    /**
062    * @param c - the Component to unpack
063    * @param b - binary image
064    * @param offset - starting offset within the binary image
065    * @return consumed bytes
066    * @throws ISOException on error
067    */
068    public int unpack (ISOComponent c, byte[] b, int offset) throws ISOException
069    {
070        int len;
071        // 
072        // For this type of Bitmap bit0 does not mean
073        // that there is a secondary bitmap to follow
074        // It simply means that field 1 is present
075        // The standard IFB_BITMAP class assumes that
076        // bit0 always means extended bitmap 
077        //
078        BitSet bmap = ISOUtil.byte2BitSet (b, offset, false); // False => no extended bitmap
079
080        c.setValue(bmap);
081        len = (len=bmap.size()) > 128 ? 128 : len;
082        return len >> 3;
083    }
084    public int getMaxPackedLength() 
085    {
086        return getLength() >> 3;
087    }
088}