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 IFELPE_LLLCHAR extends ISOFieldPackager { 034 private static final int TAG_BYTE_LENGTH = 2; 035 private static final int LENGTH_BYTE_LENGTH = 3; 036 private static final int TAG_HEADER_LENGTH = TAG_BYTE_LENGTH + LENGTH_BYTE_LENGTH; 037 038 /** Default constructor. */ 039 public IFELPE_LLLCHAR() { 040 super(); 041 } 042 /** 043 * Constructs a packager with the given length and description. 044 * @param len - field len 045 * @param description symbolic descrption 046 */ 047 public IFELPE_LLLCHAR(int len, String description) { 048 super(len, description); 049 } 050 /** 051 * @param c - a component 052 * @return packed component 053 * @exception ISOException on ISO processing error 054 */ 055 @Override 056 public byte[] pack(final ISOComponent c) throws ISOException { 057 final String s = (String) c.getValue(); 058 final int len = s.length(); 059 final byte[] payload = new byte[len + TAG_HEADER_LENGTH]; 060 final String tagHeader = ISOUtil.zeropad(Integer.toString(len + TAG_BYTE_LENGTH), LENGTH_BYTE_LENGTH) 061 + ISOUtil.zeropad(c.getKey().toString(), TAG_BYTE_LENGTH); 062 System.arraycopy(ISOUtil.asciiToEbcdic(tagHeader), 0, payload, 0, TAG_HEADER_LENGTH); 063 System.arraycopy(ISOUtil.asciiToEbcdic(s), 0, payload, TAG_HEADER_LENGTH, len); 064 return payload; 065 } 066 067 @Override 068 public int unpack(final ISOComponent c, final byte[] b, final int offset) throws ISOException { 069 final String asciiResult = ISOUtil.ebcdicToAscii(b, offset, LENGTH_BYTE_LENGTH); 070 final int len = Integer.parseInt(asciiResult) - TAG_BYTE_LENGTH; 071 if (!(c instanceof ISOField)) throw new ISOException(c.getClass() 072 .getName() 073 + " is not an ISOField"); 074 c.setFieldNumber(Integer.parseInt(ISOUtil.ebcdicToAscii(b, offset + LENGTH_BYTE_LENGTH, TAG_BYTE_LENGTH))); 075 c.setValue(ISOUtil.ebcdicToAscii(b, offset + TAG_HEADER_LENGTH, len)); 076 return len + 5; 077 } 078 079 @Override 080 public void unpack(final ISOComponent c, final InputStream in) throws IOException, 081 ISOException { 082 if (!(c instanceof ISOField)) throw new ISOException(c.getClass() 083 .getName() 084 + " is not an ISOField"); 085 086 final int len = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes(in, LENGTH_BYTE_LENGTH))) - TAG_BYTE_LENGTH; 087 final int fieldNumber = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes(in, TAG_BYTE_LENGTH))); 088 c.setFieldNumber(fieldNumber); 089 c.setValue(new String(readBytes(in, len))); 090 } 091 092 @Override 093 public int getMaxPackedLength() { 094 return getLength() + TAG_BYTE_LENGTH; 095 } 096}