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