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