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 IFEP_LLLCHAR extends ISOFieldPackager { 034 public IFEP_LLLCHAR() { 035 super(); 036 } 037 /** 038 * @param len - field len 039 * @param description symbolic descrption 040 */ 041 public IFEP_LLLCHAR (int len, String description) { 042 super(len, description); 043 } 044 /** 045 * @param c - a component 046 * @return packed component 047 * @exception ISOException 048 */ 049 public byte[] pack (ISOComponent c) throws ISOException { 050 int len; 051 String s = (String) c.getValue(); 052 053 if ((len=s.length()) > getLength() || len>997) // paranoia settings 054 throw new ISOException ( 055 "invalid len "+len +" packing LLEPCHAR field " 056 +c.getKey() 057 ); 058 059 byte[] b = new byte[len + 5]; 060 byte[] llltt = 061 ISOUtil.asciiToEbcdic ( 062 ISOUtil.zeropad(Integer.toString(len+2), 3) 063 +ISOUtil.zeropad(c.getKey().toString(), 2)); 064 System.arraycopy(llltt, 0, b, 0, 5); 065 System.arraycopy(s.getBytes(), 0, b, 5, len); 066 return b; 067 } 068 069 /** 070 * @param c - the Component to unpack 071 * @param b - binary image 072 * @param offset - starting offset within the binary image 073 * @return consumed bytes 074 * @exception ISOException 075 */ 076 public int unpack (ISOComponent c, byte[] b, int offset) 077 throws ISOException 078 { 079 int len = Integer.parseInt(ISOUtil.ebcdicToAscii(b, offset, 3)) - 2; 080 if (!(c instanceof ISOField)) 081 throw new ISOException 082 (c.getClass().getName() + " is not an ISOField"); 083 084 c.setFieldNumber ( 085 Integer.parseInt(ISOUtil.ebcdicToAscii (b, offset+3, 2)) 086 ); 087 088 c.setValue (new String (b, offset+5, len)); 089 return len + 5; 090 } 091 public void unpack (ISOComponent c, InputStream in) 092 throws IOException, ISOException 093 { 094 095 if (!(c instanceof ISOField)) 096 throw new ISOException 097 (c.getClass().getName() + " is not an ISOField"); 098 099 int len = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes (in, 3))) - 2; 100 int fldno = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes (in, 2))); 101 c.setFieldNumber (fldno); 102 c.setValue (new String (readBytes (in, len))); 103 } 104 105 public int getMaxPackedLength() { 106 return getLength() + 2; 107 } 108}