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 * Similar to Europay format, but instead of LLTT it's TTTTLLL
025 * <code>
026 * Format TTLL....
027 * Where 
028 *       TTTT is the 4 digit field number (Tag)
029 *       LLL is the 3 digit field length
030 *       .. is the field content   
031 * </code>
032 *
033 * @author Alejandro Revilla
034 * @version $Id$
035 * @see IFEP_LLCHAR
036 */
037public class IFIPM_LLLCHAR extends ISOFieldPackager {
038    /** Default constructor. */
039    public IFIPM_LLLCHAR () {
040        super();
041    }
042    /**
043     * Constructs a packager with the given length and description.
044     * @param len - field len
045     * @param description symbolic descrption
046     */
047    public IFIPM_LLLCHAR (int len, String description) {
048        super(len, description);
049    }
050    /**
051     * @param c - a component
052     * @return packed component
053     * @exception ISOException on ISO processing error
054     */
055    public byte[] pack (ISOComponent c) throws ISOException {
056        int len;
057        String s = (String) c.getValue();
058    
059        if ((len=s.length()) > getLength() || len>997)   // paranoia settings
060            throw new ISOException (
061                "invalid len "+len +" packing IFIPM_LLLCHAR field "
062                + c.getKey() + " maxlen=" + getLength()
063            );
064
065        return (
066            ISOUtil.zeropad(c.getKey().toString(), 4)
067           +ISOUtil.zeropad(Integer.toString(len), 3) 
068           +s
069        ).getBytes();
070    }
071
072    /**
073     * @param c - the Component to unpack
074     * @param b - binary image
075     * @param offset - starting offset within the binary image
076     * @return consumed bytes
077     * @exception ISOException on ISO processing error
078     */
079    public int unpack (ISOComponent c, byte[] b, int offset)
080        throws ISOException
081    {
082        if (!(c instanceof ISOField))
083            throw new ISOException 
084                (c.getClass().getName() + " is not an ISOField");
085        
086        c.setFieldNumber(
087                Integer.parseInt(new String(b, offset, 4))
088        );
089        offset += 4;
090        int len = Integer.parseInt(new String(b, offset, 3));
091        offset += 3;
092        c.setValue (new String (b, offset, len));
093        return len + 7;
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 fldno = Integer.parseInt(new String(readBytes (in, 4)));
104        int len   = Integer.parseInt(new String(readBytes (in, 3)));
105        c.setFieldNumber(fldno);
106        c.setValue (new String (readBytes (in, len)));
107    }
108
109    public int getMaxPackedLength() {
110        return getLength() + 7;
111    }
112}