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;
020
021/**
022 * ISOFieldPackager CHARACTERS (ASCII and BINARY)
023 * deal fields terminated by special token
024 * @author Zhiyu Tang
025 * @version $Id$
026 * @see ISOComponent
027 */
028public class IF_TCHAR extends IF_TBASE {
029    /** Default constructor. */
030    public IF_TCHAR() {
031        super();
032    }
033    /**
034     * Constructs a packager with the given length and description.
035     * @param len - field len
036     * @param description symbolic descrption
037     */
038    public IF_TCHAR(int len, String description) {
039        super(len, description);
040    }
041    /**
042     * Constructs a packager with the given length and description.
043     * @param len - field len
044     * @param description symbolic descrption
045     * @param token token descrption
046     */
047    public IF_TCHAR(int len, String description, String token) {
048        super(len, description, token);
049    }
050
051    /**
052     * @param c - a component
053     * @return packed component
054     * @exception ISOException on ISO processing error
055     */
056    @Override
057    public byte[] pack (ISOComponent c) throws ISOException {
058        String s = c.getValue() + getToken() ;
059        return s.getBytes(ISOUtil.CHARSET);
060    }
061    /**
062     * @param c - the Component to unpack
063     * @param b - binary image
064     * @param offset - starting offset within the binary image
065     * @return consumed bytes
066     * @exception ISOException on ISO processing error
067     */
068    @Override
069    public int unpack (ISOComponent c, byte[] b, int offset)
070        throws ISOException
071    {
072        String s = new String(b, ISOUtil.CHARSET);
073        int newoffset = s.indexOf( getToken() , offset );
074        c.setValue( s.substring(offset, newoffset ));
075        int len = newoffset - offset;
076        setLength( len );
077        return len + getToken().length();
078    }
079
080    @Override
081    public int getMaxPackedLength() {
082        return getLength() + getToken().length();
083    }
084}