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    /** Creates an AsciiPrefixer that uses the specified number of ASCII decimal digits for the length prefix.
068     * @param nDigits number of ASCII decimal digits in the length prefix
069     */
070    public AsciiPrefixer(int nDigits)
071    {
072        this.nDigits = nDigits;
073    }
074
075    @Override
076    public void encodeLength(int length, byte[] b) throws ISOException
077    {
078        int n = length;
079        // Write the string backwards - I don't know why I didn't see this at first.
080        for (int i = nDigits - 1; i >= 0; i--)
081        {
082            b[i] = (byte)(n % 10 + '0');
083            n /= 10;
084        }
085        if (n != 0)
086        {
087            throw new ISOException("invalid len "+ length + ". Prefixing digits = " + nDigits);
088        }
089    }
090
091    @Override
092    public int decodeLength(byte[] b, int offset) throws ISOException {
093        int len = 0;
094        for (int i = 0; i < nDigits; i++)
095        {
096            byte d = b[offset + i];
097            if(d < '0' || d > '9')
098            {
099                throw new ISOException("Invalid character found. Expected digit.");
100            }
101            len = len * 10 + d - (byte)'0';
102        }
103        return len;
104    }
105
106    @Override
107    public int getPackedLength()
108    {
109        return nDigits;
110    }
111}