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; 020 021/** 022 * AsciiPrefixer constructs a prefix for ASCII messages. 023 * 024 * @author joconnor 025 * @version $Revision$ $Date$ 026 */ 027public class AsciiPrefixer implements Prefixer 028{ 029 /** 030 * A length prefixer for up to 9 chars. The length is encoded with 1 ASCII 031 * char representing 1 decimal digit. 032 */ 033 public static final AsciiPrefixer L = new AsciiPrefixer(1); 034 /** 035 * A length prefixer for up to 99 chars. The length is encoded with 2 ASCII 036 * chars representing 2 decimal digits. 037 */ 038 public static final AsciiPrefixer LL = new AsciiPrefixer(2); 039 /** 040 * A length prefixer for up to 999 chars. The length is encoded with 3 ASCII 041 * chars representing 3 decimal digits. 042 */ 043 public static final AsciiPrefixer LLL = new AsciiPrefixer(3); 044 /** 045 * A length prefixer for up to 9999 chars. The length is encoded with 4 046 * ASCII chars representing 4 decimal digits. 047 */ 048 public static final AsciiPrefixer LLLL = new AsciiPrefixer(4); 049 /** 050 * A length prefixer for up to 99999 chars. The length is encoded with 5 051 * ASCII chars representing 5 decimal digits. 052 */ 053 public static final AsciiPrefixer LLLLL = new AsciiPrefixer(5); 054 055 /** 056 * A length prefixer for up to 999999 chars. The length is encoded with 6 057 * ASCII chars representing 6 decimal digits. 058 */ 059 public static final AsciiPrefixer LLLLLL = new AsciiPrefixer(6); 060 061 //private static final LeftPadder PADDER = LeftPadder.ZERO_PADDER; 062 //private static final AsciiInterpreter INTERPRETER = AsciiInterpreter.INSTANCE; 063 064 /** The number of digits allowed to express the length */ 065 private int nDigits; 066 067 public AsciiPrefixer(int nDigits) 068 { 069 this.nDigits = nDigits; 070 } 071 072 @Override 073 public void encodeLength(int length, byte[] b) throws ISOException 074 { 075 int n = length; 076 // Write the string backwards - I don't know why I didn't see this at first. 077 for (int i = nDigits - 1; i >= 0; i--) 078 { 079 b[i] = (byte)(n % 10 + '0'); 080 n /= 10; 081 } 082 if (n != 0) 083 { 084 throw new ISOException("invalid len "+ length + ". Prefixing digits = " + nDigits); 085 } 086 } 087 088 @Override 089 public int decodeLength(byte[] b, int offset) throws ISOException { 090 int len = 0; 091 for (int i = 0; i < nDigits; i++) 092 { 093 byte d = b[offset + i]; 094 if(d < '0' || d > '9') 095 { 096 throw new ISOException("Invalid character found. Expected digit."); 097 } 098 len = len * 10 + d - (byte)'0'; 099 } 100 return len; 101 } 102 103 @Override 104 public int getPackedLength() 105 { 106 return nDigits; 107 } 108}