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
021/**
022 * ISOFieldPackager Binary Amount
023 *
024 * @author apr@cs.com.uy
025 * @version $Id$
026 * @see ISOComponent
027 */
028public class IFB_AMOUNT extends ISOFieldPackager {
029    private BCDInterpreter interpreter;
030    
031    public IFB_AMOUNT() {
032        super();
033        interpreter = BCDInterpreter.LEFT_PADDED;
034    }
035    /**
036     * @param len - field len
037     * @param description symbolic descrption
038     */
039    public IFB_AMOUNT(int len, String description, boolean pad) {
040        super(len, description);
041        this.pad = pad;
042        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
043    }
044    
045    public void setPad(boolean pad)
046    {
047        this.pad = pad;
048        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
049    }
050
051    /**
052     * @param c - a component
053     * @return packed component
054     * @exception ISOException
055     */
056    public byte[] pack (ISOComponent c) throws ISOException {
057        String s = (String) c.getValue();
058        String amount = ISOUtil.zeropad(s.substring(1), getLength()-1);
059        byte[] b   = new byte[1 + (getLength() >> 1)];
060        b[0] = (byte) s.charAt(0);
061        interpreter.interpret(amount, b, 1);
062        return b;
063    }
064    /**
065     * @param c - the Component to unpack
066     * @param b - binary image
067     * @param offset - starting offset within the binary image
068     * @return consumed bytes
069     * @exception ISOException
070     */
071    public int unpack (ISOComponent c, byte[] b, int offset)
072        throws ISOException
073    {
074        String d = new String(b, offset, 1)
075                    + interpreter.uninterpret(b, offset + 1, getLength() - 1);
076        c.setValue(d);
077        return 1 + (getLength() >> 1);
078    }
079    public int getMaxPackedLength() {
080        return 1 + (getLength() >> 1);
081    }
082}