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/**
023 * EbcdicPrefixer constructs a prefix for EBCDIC messages.
024 * 
025 * @author joconnor
026 * @version $Revision$ $Date$
027 */
028public class EbcdicPrefixer implements Prefixer
029{
030    /**
031     * A length prefixer for up to 9 chars. The length is encoded with 1 EBCDIC
032     * chars representing 1 decimal digits.
033     */
034    public static final EbcdicPrefixer L = new EbcdicPrefixer(1);
035    /**
036     * A length prefixer for up to 99 chars. The length is encoded with 2 EBCDIC
037     * chars representing 2 decimal digits.
038     */
039    public static final EbcdicPrefixer LL = new EbcdicPrefixer(2);
040    /**
041     * A length prefixer for up to 999 chars. The length is encoded with 3 EBCDIC
042     * chars representing 3 decimal digits.
043     */
044    public static final EbcdicPrefixer LLL = new EbcdicPrefixer(3);
045    /**
046     * A length prefixer for up to 9999 chars. The length is encoded with 4
047     * EBCDIC chars representing 4 decimal digits.
048     */
049    public static final EbcdicPrefixer LLLL = new EbcdicPrefixer(4);
050
051    private static byte[] EBCDIC_DIGITS = {(byte)0xF0, (byte)0xF1, (byte)0xF2,
052        (byte)0xF3, (byte)0xF4, (byte)0xF5, (byte)0xF6, (byte)0xF7, (byte)0xF8, 
053        (byte)0xF9}; 
054
055    /** The number of digits allowed to express the length */
056    private int nDigits;
057
058    /**
059     * Creates an EbcdicPrefixer with the given number of length-indicator digits.
060     * @param nDigits number of digits used to represent the field length
061     */
062    public EbcdicPrefixer(int nDigits)
063    {
064        this.nDigits = nDigits;
065    }
066
067
068    @Override
069    public void encodeLength(int length, byte[] b)
070    {
071        for (int i = nDigits - 1; i >= 0; i--)
072        {
073            b[i] = EBCDIC_DIGITS[length % 10];
074            length /= 10;
075        }
076    }
077
078    @Override
079    public int decodeLength(byte[] b, int offset)
080    {
081        int len = 0;
082        for (int i = 0; i < nDigits; i++)
083        {
084            len = len * 10 + (b[offset + i] & 0x0F);
085        }
086        return len;
087    }
088
089    @Override
090    public int getPackedLength()
091    {
092        return nDigits;
093    }
094}