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;
020import java.io.IOException;
021import java.io.InputStream;
022
023/**
024 * Length is represented in ASCII (as in IFA_LL*)
025 * Value is represented in BCD
026 * ISOFieldPackager Binary LLNUM
027 *
028 * @author Mladen Mrkic (mmrkic@arius.co.yu)
029 *
030 * @see ISOComponent
031 */
032public class IFA_LLBNUM extends ISOFieldPackager {
033    private Interpreter interpreter;
034    private Prefixer prefixer;
035    
036    /** Default constructor. */
037    public IFA_LLBNUM () {
038        super();
039        interpreter = BCDInterpreter.LEFT_PADDED;
040        prefixer = AsciiPrefixer.LL;
041    }
042    /**
043     * Constructs a packager with the given length and description.
044     * @param len - field len
045     * @param description symbolic descrption
046     * @param pad if true, pad the number
047     */
048    public  IFA_LLBNUM (int len, String description, boolean pad) {
049        super(len, description);
050        this.pad = pad;
051        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
052        prefixer = AsciiPrefixer.LL;
053    }
054
055    public void setPad(boolean pad)
056    {
057        this.pad = pad;
058        interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED;
059    }
060
061    /**
062     * @param c - a component
063     * @return packed component
064     * @exception ISOException on ISO processing error
065     */
066    public byte[] pack (ISOComponent c) throws ISOException {
067        String s = (String) c.getValue();
068        int len = s.length();
069        if (len > getLength() || len>99)   // paranoia settings
070            throw new ISOException (
071                "invalid len "+len +" packing IFA_LLBNUM field " + c.getKey()
072            );
073
074        byte[] b = new byte[2 + ((len+1) >> 1)];
075        prefixer.encodeLength(len + 1 >> 1 << 1, b);
076        interpreter.interpret(s, b, 2);
077        return b;
078    }
079
080    /**
081     * @param c - the Component to unpack
082     * @param b - binary image
083     * @param offset - starting offset within the binary image
084     * @return consumed bytes
085     * @exception ISOException on ISO processing error
086     */
087    public int unpack (ISOComponent c, byte[] b, int offset)
088        throws ISOException
089    {
090        int len = prefixer.decodeLength(b, offset);
091        c.setValue (interpreter.uninterpret(b, offset + 2, len));
092        return 2 + (++len >> 1);
093    }
094    public void unpack (ISOComponent c, InputStream in) 
095        throws IOException, ISOException
096    {
097        int len = prefixer.decodeLength(readBytes (in, 2), 2);
098        c.setValue (interpreter.uninterpret(readBytes (in, len+2 >> 1), 0, len));
099    }
100    public int getMaxPackedLength() {
101        return 1 + (getLength()+1 >> 1);
102    }
103}