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    /** Default constructor. */
032    public IFB_AMOUNT() {
033        super();
034        interpreter = BCDInterpreter.LEFT_PADDED;
035    }
036    /**
037     * Constructs a packager with the given length and description.
038     * @param len - field len
039     * @param description symbolic descrption
040     * @param pad if true, apply padding
041     */
042    public IFB_AMOUNT(int len, String description, boolean pad) {
043        super(len, description);
044        this.pad = pad;
045        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
046    }
047    
048    public void setPad(boolean pad)
049    {
050        this.pad = pad;
051        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
052    }
053
054    /**
055     * @param c - a component
056     * @return packed component
057     * @exception ISOException on ISO processing error
058     */
059    public byte[] pack (ISOComponent c) throws ISOException {
060        String s = (String) c.getValue();
061        String amount = ISOUtil.zeropad(s.substring(1), getLength()-1);
062        byte[] b   = new byte[1 + (getLength() >> 1)];
063        b[0] = (byte) s.charAt(0);
064        interpreter.interpret(amount, b, 1);
065        return b;
066    }
067    /**
068     * @param c - the Component to unpack
069     * @param b - binary image
070     * @param offset - starting offset within the binary image
071     * @return consumed bytes
072     * @exception ISOException on ISO processing error
073     */
074    public int unpack (ISOComponent c, byte[] b, int offset)
075        throws ISOException
076    {
077        String d = new String(b, offset, 1)
078                    + interpreter.uninterpret(b, offset + 1, getLength() - 1);
079        c.setValue(d);
080        return 1 + (getLength() >> 1);
081    }
082    public int getMaxPackedLength() {
083        return 1 + (getLength() >> 1);
084    }
085}