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    public static final BcdPrefixer L = new BcdPrefixer(1);
034    /**
035         * A length prefixer for up to 99 chars. The length is encoded with 2 BCD digits.
036         */
037    public static final BcdPrefixer LL = new BcdPrefixer(2);
038    /**
039         * A length prefixer for up to 999 chars. The length is encoded with 3 BCD digits.
040         */
041    public static final BcdPrefixer LLL = new BcdPrefixer(3);
042    /**
043         * A length prefixer for up to 9999 chars. The length is encoded with 4 BCD digits.
044         */
045    public static final BcdPrefixer LLLL = new BcdPrefixer(4);
046    /**
047     * A length prefixer for up to 99999 chars. The length is encoded with 5 BCD digits.
048     */
049    public static final BcdPrefixer LLLLL = new BcdPrefixer(5);
050    /**
051    * A length prefixer for up to 999999 chars. The length is encoded with 6 BCD digits.
052    */
053    public static final BcdPrefixer LLLLLL = new BcdPrefixer(6);
054
055    /** The number of digits allowed to express the length */
056    private int nDigits;
057
058    public BcdPrefixer(int nDigits)
059    {
060        this.nDigits = nDigits;
061    }
062
063    @Override
064    public void encodeLength(int length, byte[] b)
065    {
066        for (int i = getPackedLength() - 1; i >= 0; i--) {
067            int twoDigits = length % 100;
068            length /= 100;
069            b[i] = (byte)((twoDigits / 10 << 4) + twoDigits % 10);
070        }
071    }
072
073    @Override
074    public int decodeLength(byte[] b, int offset)
075    {
076        int len = 0;
077        for (int i = 0; i < (nDigits + 1) / 2; i++)
078        {
079            len = 100 * len + ((b[offset + i] & 0xF0) >> 4) * 10 + (b[offset + i] & 0x0F);
080        }
081        return len;
082    }
083
084    @Override
085    public int getPackedLength()
086    {
087        return nDigits + 1 >> 1;
088    }
089}