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    public IF_TCHAR() {
030        super();
031    }
032    /**
033     * @param len - field len
034     * @param description symbolic descrption
035     */
036    public IF_TCHAR(int len, String description) {
037        super(len, description);
038    }
039    /**
040     * @param len - field len
041     * @param description symbolic descrption
042     * @param token token descrption
043     */
044    public IF_TCHAR(int len, String description, String token) {
045        super(len, description, token);
046    }
047
048    /**
049     * @param c - a component
050     * @return packed component
051     * @exception ISOException
052     */
053    @Override
054    public byte[] pack (ISOComponent c) throws ISOException {
055        String s = c.getValue() + getToken() ;
056        return s.getBytes(ISOUtil.CHARSET);
057    }
058    /**
059     * @param c - the Component to unpack
060     * @param b - binary image
061     * @param offset - starting offset within the binary image
062     * @return consumed bytes
063     * @exception ISOException
064     */
065    @Override
066    public int unpack (ISOComponent c, byte[] b, int offset)
067        throws ISOException
068    {
069        String s = new String(b, ISOUtil.CHARSET);
070        int newoffset = s.indexOf( getToken() , offset );
071        c.setValue( s.substring(offset, newoffset ));
072        int len = newoffset - offset;
073        setLength( len );
074        return len + getToken().length();
075    }
076
077    @Override
078    public int getMaxPackedLength() {
079        return getLength() + getToken().length();
080    }
081}