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_LLLCHAR; 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 * ISOFieldPackager for a 3-digit ASCII-length-prefixed character tagged field. 037 * @author Vishnu Pillai 038 */ 039public class IFTA_LLLCHAR extends IFA_LLLCHAR implements TaggedFieldPackager { 040 private String token; 041 042 private IF_CHAR tagPackager; 043 044 /** Default constructor. */ 045 public IFTA_LLLCHAR() { 046 super(); 047 } 048 049 /** 050 * Constructs a packager with the given length and description. 051 * @param len field length 052 * @param description field description 053 */ 054 public IFTA_LLLCHAR(int len, String description) { 055 super(len, description); 056 } 057 058 @Override 059 public ISOComponent createComponent(int fieldNumber) { 060 return new ISOTaggedField(getToken(), super.createComponent(fieldNumber)); 061 } 062 063 @Override 064 public void setToken(String token) { 065 this.token = token; 066 tagPackager = new IF_CHAR(token.length(), "Tag"); 067 tagPackager.setPadder(LeftPadder.ZERO_PADDER); 068 } 069 070 @Override 071 public String getToken() { 072 return token; 073 } 074 075 /** 076 * Returns the packager used to pack/unpack the tag field. 077 * @return the tag packager 078 */ 079 protected ISOFieldPackager getTagPackager() { 080 return tagPackager; 081 } 082 083 /** 084 * Packs the tag portion of the tagged field. 085 * @param c the ISO component 086 * @return packed tag bytes 087 * @throws ISOException on pack error 088 */ 089 protected byte[] packTag(ISOComponent c) throws ISOException { 090 return getTagPackager().pack(new ISOField((Integer) c.getKey(), ((ISOTaggedField) c).getTag())); 091 } 092 093 /** 094 * Unpacks the tag from a byte array. 095 * @param c the ISO component 096 * @param tagBytes byte array containing tag data 097 * @param offset starting offset 098 * @return number of bytes consumed 099 * @throws ISOException on unpack error 100 */ 101 protected int unpackTag(ISOComponent c, byte[] tagBytes, int offset) throws ISOException { 102 ISOField tagField = new ISOField((Integer) c.getKey()); 103 int consumed = getTagPackager().unpack(tagField, tagBytes, offset); 104 ((ISOTaggedField) c).setTag(tagField.getValue().toString()); 105 return consumed; 106 } 107 108 /** 109 * Unpacks the tag from an InputStream. 110 * @param c the ISO component 111 * @param in the input stream 112 * @throws ISOException on unpack error 113 * @throws IOException on I/O error 114 */ 115 protected void unpackTag(ISOComponent c, InputStream in) throws ISOException, IOException { 116 ISOField tagField = new ISOField((Integer) c.getKey()); 117 getTagPackager().unpack(tagField, in); 118 ((ISOTaggedField) c).setTag(tagField.getValue().toString()); 119 } 120 121 @Override 122 public byte[] pack(ISOComponent c) throws ISOException { 123 byte[] tagBytes = packTag(c); 124 byte[] message = super.pack(c); 125 byte[] b = new byte[tagBytes.length + message.length]; 126 System.arraycopy(tagBytes, 0, b, 0, tagBytes.length); 127 System.arraycopy(message, 0, b, tagBytes.length, message.length); 128 return b; 129 } 130 131 @Override 132 public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException { 133 int consumed = unpackTag(c, b, offset); 134 return consumed + super.unpack(c, b, offset + consumed); 135 } 136 137 @Override 138 public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException { 139 unpackTag(c, in); 140 super.unpack(c, in); 141 } 142 143 @Override 144 public void pack(ISOComponent c, ObjectOutput out) throws IOException, ISOException { 145 byte[] tagBytes = packTag(c); 146 out.write(tagBytes); 147 super.pack(c, out); 148 } 149 150}