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 * Length is represented in ASCII (as in IFA_LL*) 025 * Value is represented in BCD 026 * ISOFieldPackager Binary LLNUM 027 * 028 * @author Mladen Mrkic <mmrkic@arius.co.yu> 029 * 030 * @see ISOComponent 031 */ 032public class IFA_LLBNUM extends ISOFieldPackager { 033 private Interpreter interpreter; 034 private Prefixer prefixer; 035 036 public IFA_LLBNUM () { 037 super(); 038 interpreter = BCDInterpreter.LEFT_PADDED; 039 prefixer = AsciiPrefixer.LL; 040 } 041 /** 042 * @param len - field len 043 * @param description symbolic descrption 044 */ 045 public IFA_LLBNUM (int len, String description, boolean pad) { 046 super(len, description); 047 this.pad = pad; 048 interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED; 049 prefixer = AsciiPrefixer.LL; 050 } 051 052 public void setPad(boolean pad) 053 { 054 this.pad = pad; 055 interpreter = pad ? BCDInterpreter.LEFT_PADDED : BCDInterpreter.RIGHT_PADDED; 056 } 057 058 /** 059 * @param c - a component 060 * @return packed component 061 * @exception ISOException 062 */ 063 public byte[] pack (ISOComponent c) throws ISOException { 064 String s = (String) c.getValue(); 065 int len = s.length(); 066 if (len > getLength() || len>99) // paranoia settings 067 throw new ISOException ( 068 "invalid len "+len +" packing IFA_LLBNUM field " + c.getKey() 069 ); 070 071 byte[] b = new byte[2 + ((len+1) >> 1)]; 072 prefixer.encodeLength(len + 1 >> 1 << 1, b); 073 interpreter.interpret(s, b, 2); 074 return b; 075 } 076 077 /** 078 * @param c - the Component to unpack 079 * @param b - binary image 080 * @param offset - starting offset within the binary image 081 * @return consumed bytes 082 * @exception ISOException 083 */ 084 public int unpack (ISOComponent c, byte[] b, int offset) 085 throws ISOException 086 { 087 int len = prefixer.decodeLength(b, offset); 088 c.setValue (interpreter.uninterpret(b, offset + 2, len)); 089 return 2 + (++len >> 1); 090 } 091 public void unpack (ISOComponent c, InputStream in) 092 throws IOException, ISOException 093 { 094 int len = prefixer.decodeLength(readBytes (in, 2), 2); 095 c.setValue (interpreter.uninterpret(readBytes (in, len+2 >> 1), 0, len)); 096 } 097 public int getMaxPackedLength() { 098 return 1 + (getLength()+1 >> 1); 099 } 100}