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 * vim:set ts=8 sw=4:
025 */
026
027/**
028 * ISOFieldPackager ASCII variable len CHAR 
029 * suitable for EuroPay subfield 48<br>
030 * <code>
031 * Format LLTT....
032 * Where LL is the 2 digit field length
033 *       TT is the 2 digit field number (Tag)
034 *       is the field content   
035 * </code>
036 *
037 * @author <a href="mailto:eoin.flood@orbiscom.com">Eoin Flood</a>
038 * @version $Id$
039 * @see ISOComponent
040 */
041public class IFEP_LLCHAR extends ISOFieldPackager {
042    /** Default constructor. */
043    public IFEP_LLCHAR() {
044        super();
045    }
046    /**
047     * Constructs a packager with the given length and description.
048     * @param len - field len
049     * @param description symbolic descrption
050     */
051    public IFEP_LLCHAR (int len, String description) {
052        super(len, description);
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        int len;
061        String s = (String) c.getValue();
062    
063        if ((len=s.length()) > getLength() || len>97)   // paranoia settings
064            throw new ISOException (
065                "invalid len "+len +" packing IFEP_LLCHAR field "
066                + c.getKey()
067            );
068
069        return (
070            ISOUtil.zeropad(Integer.toString(len+2), 2) 
071           +ISOUtil.zeropad(c.getKey().toString(), 2)
072           +s
073        ).getBytes();
074    }
075
076    /**
077     * @param c - the Component to unpack
078     * @param b - binary image
079     * @param offset - starting offset within the binary image
080     * @return consumed bytes
081     * @exception ISOException on ISO processing error
082     */
083    public int unpack (ISOComponent c, byte[] b, int offset)
084        throws ISOException
085    {
086        int len = Integer.parseInt(new String(b, offset, 2));
087        if (!(c instanceof ISOField))
088            throw new ISOException 
089                (c.getClass().getName() + " is not an ISOField");
090
091        c.setFieldNumber(
092                Integer.parseInt(new String(b, offset + 2, 2))
093        );
094        c.setValue (new String (b, offset+4, len-2));
095        return len + 2;
096    }
097    public void unpack (ISOComponent c, InputStream in) 
098        throws IOException, ISOException
099    {
100
101        if (!(c instanceof ISOField))
102            throw new ISOException 
103                (c.getClass().getName() + " is not an ISOField");
104
105        int len   = Integer.parseInt(new String(readBytes (in, 2)));
106        int fldno = Integer.parseInt(new String(readBytes (in, 2)));
107        c.setFieldNumber(fldno);
108        c.setValue (new String (readBytes (in, len-2)));
109    }
110
111    public int getMaxPackedLength() {
112        return getLength() + 2;
113    }
114}