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 * ISOFieldPackager ASCII variable len CHAR 
025 * suitable for GICC subfield 60<br>
026 * <code>
027 * Format LLLTT....
028 * Where LLL is the 3 digit field length
029 *       TT is the 2 digit field number (Tag)
030 *       is the field content   
031 * </code>
032 */
033public class IFELPE_LLLCHAR extends ISOFieldPackager {
034    private static final int TAG_BYTE_LENGTH = 2;
035    private static final int LENGTH_BYTE_LENGTH = 3;
036    private static final int TAG_HEADER_LENGTH = TAG_BYTE_LENGTH + LENGTH_BYTE_LENGTH;
037
038    public IFELPE_LLLCHAR() {
039        super();
040    }
041    /**
042     * @param len - field len
043     * @param description symbolic descrption
044     */
045    public IFELPE_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    @Override
054    public byte[] pack(final ISOComponent c) throws ISOException {
055        final String s = (String) c.getValue();
056        final int len = s.length();
057        final byte[] payload = new byte[len + TAG_HEADER_LENGTH];
058        final String tagHeader = ISOUtil.zeropad(Integer.toString(len + TAG_BYTE_LENGTH), LENGTH_BYTE_LENGTH)
059                + ISOUtil.zeropad(c.getKey().toString(), TAG_BYTE_LENGTH);
060        System.arraycopy(ISOUtil.asciiToEbcdic(tagHeader), 0, payload, 0, TAG_HEADER_LENGTH);
061        System.arraycopy(ISOUtil.asciiToEbcdic(s), 0, payload, TAG_HEADER_LENGTH, len);
062        return payload;
063    }
064
065    @Override
066    public int unpack(final ISOComponent c, final byte[] b, final int offset) throws ISOException {
067        final String asciiResult = ISOUtil.ebcdicToAscii(b, offset, LENGTH_BYTE_LENGTH);
068        final int len = Integer.parseInt(asciiResult) - TAG_BYTE_LENGTH;
069        if (!(c instanceof ISOField)) throw new ISOException(c.getClass()
070                .getName()
071                + " is not an ISOField");
072        c.setFieldNumber(Integer.parseInt(ISOUtil.ebcdicToAscii(b, offset + LENGTH_BYTE_LENGTH, TAG_BYTE_LENGTH)));
073        c.setValue(ISOUtil.ebcdicToAscii(b, offset + TAG_HEADER_LENGTH, len));
074        return len + 5;
075    }
076
077    @Override
078    public void unpack(final ISOComponent c, final InputStream in) throws IOException,
079            ISOException {
080        if (!(c instanceof ISOField)) throw new ISOException(c.getClass()
081                .getName()
082                + " is not an ISOField");
083
084        final int len = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes(in, LENGTH_BYTE_LENGTH))) - TAG_BYTE_LENGTH;
085        final int fieldNumber = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes(in, TAG_BYTE_LENGTH)));
086        c.setFieldNumber(fieldNumber);
087        c.setValue(new String(readBytes(in, len)));
088    }
089
090    @Override
091    public int getMaxPackedLength() {
092        return getLength() + TAG_BYTE_LENGTH;
093    }
094}