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    public EbcdicPrefixer(int nDigits)
059    {
060        this.nDigits = nDigits;
061    }
062
063
064    @Override
065    public void encodeLength(int length, byte[] b)
066    {
067        for (int i = nDigits - 1; i >= 0; i--)
068        {
069            b[i] = EBCDIC_DIGITS[length % 10];
070            length /= 10;
071        }
072    }
073
074    @Override
075    public int decodeLength(byte[] b, int offset)
076    {
077        int len = 0;
078        for (int i = 0; i < nDigits; i++)
079        {
080            len = len * 10 + (b[offset + i] & 0x0F);
081        }
082        return len;
083    }
084
085    @Override
086    public int getPackedLength()
087    {
088        return nDigits;
089    }
090}