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 * BcdPrefixer constructs a prefix storing the length in BCD. 023 * 024 * @author joconnor 025 * @version $Revision$ $Date$ 026 */ 027@SuppressWarnings("unused") 028public class BcdPrefixer implements Prefixer 029{ 030 /** 031 * A length prefixer for up to 9 chars. The length is encoded with 1 BCD digit. 032 */ 033 /** 034 * Pre-built {@link BcdPrefixer} for common BCD length prefix sizes. 035 */ 036 public static final BcdPrefixer L = new BcdPrefixer(1); 037 /** 038 * A length prefixer for up to 99 chars. The length is encoded with 2 BCD digits. 039 */ 040 public static final BcdPrefixer LL = new BcdPrefixer(2); 041 /** 042 * A length prefixer for up to 999 chars. The length is encoded with 3 BCD digits. 043 */ 044 public static final BcdPrefixer LLL = new BcdPrefixer(3); 045 /** 046 * A length prefixer for up to 9999 chars. The length is encoded with 4 BCD digits. 047 */ 048 public static final BcdPrefixer LLLL = new BcdPrefixer(4); 049 /** 050 * A length prefixer for up to 99999 chars. The length is encoded with 5 BCD digits. 051 */ 052 public static final BcdPrefixer LLLLL = new BcdPrefixer(5); 053 /** 054 * A length prefixer for up to 999999 chars. The length is encoded with 6 BCD digits. 055 */ 056 public static final BcdPrefixer LLLLLL = new BcdPrefixer(6); 057 058 /** The number of digits allowed to express the length */ 059 private int nDigits; 060 061 /** 062 * Creates a BcdPrefixer with the given number of digits. 063 * @param nDigits the number of BCD digits used for the length prefix 064 */ 065 public BcdPrefixer(int nDigits) 066 { 067 this.nDigits = nDigits; 068 } 069 070 @Override 071 public void encodeLength(int length, byte[] b) 072 { 073 for (int i = getPackedLength() - 1; i >= 0; i--) { 074 int twoDigits = length % 100; 075 length /= 100; 076 b[i] = (byte)((twoDigits / 10 << 4) + twoDigits % 10); 077 } 078 } 079 080 @Override 081 public int decodeLength(byte[] b, int offset) 082 { 083 int len = 0; 084 for (int i = 0; i < (nDigits + 1) / 2; i++) 085 { 086 len = 100 * len + ((b[offset + i] & 0xF0) >> 4) * 10 + (b[offset + i] & 0x0F); 087 } 088 return len; 089 } 090 091 @Override 092 public int getPackedLength() 093 { 094 return nDigits + 1 >> 1; 095 } 096}