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    public IFEP_LLCHAR() {
043        super();
044    }
045    /**
046     * @param len - field len
047     * @param description symbolic descrption
048     */
049    public IFEP_LLCHAR (int len, String description) {
050        super(len, description);
051    }
052    /**
053     * @param c - a component
054     * @return packed component
055     * @exception ISOException
056     */
057    public byte[] pack (ISOComponent c) throws ISOException {
058        int len;
059        String s = (String) c.getValue();
060    
061        if ((len=s.length()) > getLength() || len>97)   // paranoia settings
062            throw new ISOException (
063                "invalid len "+len +" packing IFEP_LLCHAR field "
064                + c.getKey()
065            );
066
067        return (
068            ISOUtil.zeropad(Integer.toString(len+2), 2) 
069           +ISOUtil.zeropad(c.getKey().toString(), 2)
070           +s
071        ).getBytes();
072    }
073
074    /**
075     * @param c - the Component to unpack
076     * @param b - binary image
077     * @param offset - starting offset within the binary image
078     * @return consumed bytes
079     * @exception ISOException
080     */
081    public int unpack (ISOComponent c, byte[] b, int offset)
082        throws ISOException
083    {
084        int len = Integer.parseInt(new String(b, offset, 2));
085        if (!(c instanceof ISOField))
086            throw new ISOException 
087                (c.getClass().getName() + " is not an ISOField");
088
089        c.setFieldNumber(
090                Integer.parseInt(new String(b, offset + 2, 2))
091        );
092        c.setValue (new String (b, offset+4, len-2));
093        return len + 2;
094    }
095    public void unpack (ISOComponent c, InputStream in) 
096        throws IOException, ISOException
097    {
098
099        if (!(c instanceof ISOField))
100            throw new ISOException 
101                (c.getClass().getName() + " is not an ISOField");
102
103        int len   = Integer.parseInt(new String(readBytes (in, 2)));
104        int fldno = Integer.parseInt(new String(readBytes (in, 2)));
105        c.setFieldNumber(fldno);
106        c.setValue (new String (readBytes (in, len-2)));
107    }
108
109    public int getMaxPackedLength() {
110        return getLength() + 2;
111    }
112}