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.EOFException;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.ObjectOutput;
025
026/**
027 * base class for the various IF*.java Field Packagers
028 * Implements "FlyWeight" pattern
029 *
030 * @author apr@cs.com.uy
031 * @version $Id$
032 *
033 * @see IFA_AMOUNT
034 * @see IFA_BINARY
035 * @see IFA_BITMAP
036 * @see IFA_FLLCHAR
037 * @see IFA_FLLNUM
038 * @see IFA_LLCHAR
039 * @see IFA_LLLBINARY
040 * @see IFA_LLLCHAR
041 * @see IFA_LLLNUM
042 * @see IFA_LLNUM
043 * @see IFA_NUMERIC
044 * @see IFB_AMOUNT
045 * @see IFB_BINARY
046 * @see IFB_BITMAP
047 * @see IFB_LLBINARY
048 * @see IFB_LLCHAR
049 * @see IFB_LLHBINARY
050 * @see IFB_LLHCHAR
051 * @see IFB_LLHECHAR
052 * @see IFB_LLHNUM
053 * @see IFB_LLLBINARY
054 * @see IFB_LLLCHAR
055 * @see IFB_LLLNUM
056 * @see IFB_LLNUM
057 * @see IFB_NUMERIC
058 * @see IF_CHAR
059 */
060public abstract class ISOFieldPackager {
061    private int len;
062    private String description;
063    protected boolean pad;
064    protected boolean trim;
065
066    /**
067     * Default Constructor
068     */
069    public ISOFieldPackager()
070    {
071        this.len = -1;
072        this.description = null;
073    }
074
075    /**
076     * @param len - field Len
077     * @param description - details
078     */
079    public ISOFieldPackager(int len, String description) {
080        this.len = len;
081        this.description = description;
082    }
083    public String getDescription() {
084        return description;
085    }
086    public void setDescription(String description) {
087        this.description = description;
088    }
089    public int getLength() {
090        return len;
091    }
092    public void setLength(int len) {
093        this.len = len;
094    }
095
096    public void setPad(boolean pad) {
097        this.pad = pad;
098    }
099
100    public void setTrim(boolean trim) {
101        this.trim = trim;
102    }
103
104    public abstract int getMaxPackedLength();
105
106    public ISOComponent createComponent(int fieldNumber) {
107        return new ISOField (fieldNumber);
108    }
109    /**
110     * @param c - a component
111     * @return packed component
112     * @exception ISOException
113     */
114    public abstract byte[] pack (ISOComponent c) throws ISOException;
115
116    /**
117     * @param c - the Component to unpack
118     * @param b - binary image
119     * @param offset - starting offset within the binary image
120     * @return consumed bytes
121     * @exception ISOException
122     */
123    public abstract int unpack (ISOComponent c, byte[] b, int offset)
124        throws ISOException;
125
126    /**
127     * @param c  - the Component to unpack
128     * @param in - input stream
129     * @exception ISOException
130     */
131    public void unpack (ISOComponent c, InputStream in) 
132        throws IOException, ISOException
133    {
134        unpack (c, readBytes (in, getMaxPackedLength ()), 0);
135    }
136    /**
137     * @param c   - the Component to unpack
138     * @param out - output stream
139     * @exception ISOException
140     * @exception IOException
141     */
142    public void pack (ISOComponent c, ObjectOutput out) 
143        throws IOException, ISOException
144    {
145        out.write (pack (c));
146    }
147
148    protected byte[] readBytes (InputStream in, int l) throws IOException {
149        byte[] b = new byte [l];
150        int n = 0;
151        while (n < l) {
152            int count = in.read(b, n, l - n);
153            if (count < 0)
154                throw new EOFException();
155            n += count;
156        }
157        return b;
158    }
159}
160