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.tlv.packager;
020
021import org.jpos.iso.IFA_LLCHAR;
022import org.jpos.iso.IF_CHAR;
023import org.jpos.iso.ISOComponent;
024import org.jpos.iso.ISOException;
025import org.jpos.iso.ISOField;
026import org.jpos.iso.ISOFieldPackager;
027import org.jpos.iso.LeftPadder;
028import org.jpos.iso.TaggedFieldPackager;
029import org.jpos.tlv.ISOTaggedField;
030
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.ObjectOutput;
034
035/**
036 * @author Vishnu Pillai
037 */
038public class IFTA_LLCHAR extends IFA_LLCHAR implements TaggedFieldPackager {
039    private String token;
040
041    private IF_CHAR tagPackager;
042
043    public IFTA_LLCHAR() {
044        super();
045    }
046
047    public IFTA_LLCHAR(int len, String description) {
048        super(len, description);
049    }
050
051    @Override
052    public ISOComponent createComponent(int fieldNumber) {
053        return new ISOTaggedField(getToken(), super.createComponent(fieldNumber));
054    }
055
056    @Override
057    public void setToken(String token) {
058        this.token = token;
059        tagPackager = new IF_CHAR(token.length(), "Tag");
060        tagPackager.setPadder(LeftPadder.ZERO_PADDER);
061    }
062
063    @Override
064    public String getToken() {
065        return token;
066    }
067
068    protected ISOFieldPackager getTagPackager() {
069        return tagPackager;
070    }
071
072    protected byte[] packTag(ISOComponent c) throws ISOException {
073        return getTagPackager().pack(new ISOField((Integer) c.getKey(), ((ISOTaggedField) c).getTag()));
074    }
075
076    protected int unpackTag(ISOComponent c, byte[] tagBytes, int offset) throws ISOException {
077        ISOField tagField = new ISOField((Integer) c.getKey());
078        int consumed = getTagPackager().unpack(tagField, tagBytes, offset);
079        ((ISOTaggedField) c).setTag(tagField.getValue().toString());
080        return consumed;
081    }
082
083    protected void unpackTag(ISOComponent c, InputStream in) throws ISOException, IOException {
084        ISOField tagField = new ISOField((Integer) c.getKey());
085        getTagPackager().unpack(tagField, in);
086        ((ISOTaggedField) c).setTag(tagField.getValue().toString());
087    }
088
089    @Override
090    public byte[] pack(ISOComponent c) throws ISOException {
091        byte[] tagBytes = packTag(c);
092        byte[] message = super.pack(c);
093        byte[] b = new byte[tagBytes.length + message.length];
094        System.arraycopy(tagBytes, 0, b, 0, tagBytes.length);
095        System.arraycopy(message, 0, b, tagBytes.length, message.length);
096        return b;
097    }
098
099    @Override
100    public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
101        int consumed = unpackTag(c, b, offset);
102        return consumed + super.unpack(c, b, offset + consumed);
103    }
104
105    @Override
106    public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException {
107        unpackTag(c, in);
108        super.unpack(c, in);
109    }
110
111    @Override
112    public void pack(ISOComponent c, ObjectOutput out) throws IOException, ISOException {
113        byte[] tagBytes = packTag(c);
114        out.write(tagBytes);
115        super.pack(c, out);
116    }
117
118}