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;
020import java.io.IOException;
021import java.io.InputStream;
022
023
024/**
025 * ISOFieldPackager Binary Hex Fixed LLBINARY
026 *
027 * @author apr@cs.com.uy
028 * @version $Id$
029 * @see ISOComponent
030 */
031public class IFB_LLHFBINARY extends ISOFieldPackager {
032    /** Default constructor. */
033    public IFB_LLHFBINARY() {
034        super();
035    }
036    /**
037    * Constructs a packager with the given length and description.
038    * @param len - field len
039    * @param description symbolic descrption
040    */
041    public IFB_LLHFBINARY (int len, String description) {
042    super(len, description);
043    }
044   /**
045    * @param c - a component
046    * @return packed component
047    * @exception ISOException on ISO processing error
048    */
049    public byte[] pack (ISOComponent c) throws ISOException {
050        int len = ((byte[]) c.getValue()).length;
051        if (len > getLength() || len>255) {
052            throw new ISOException (
053                "invalid len "+len +" packing field "+ c.getKey()
054            );
055        }
056        byte[] b = new byte[getLength() + 1];
057        b[0] = (byte) len;
058        System.arraycopy(c.getValue(), 0, b, 1, len);
059        return b;
060    }
061   /**
062    * @param c - the Component to unpack
063    * @param b - binary image
064    * @param offset - starting offset within the binary image
065    * @return consumed bytes
066    * @exception ISOException on ISO processing error
067    */
068    public int unpack (ISOComponent c, byte[] b, int offset) throws ISOException
069    {
070        int len = b[offset] & 0xFF;
071        byte[] value = new byte[len];
072        System.arraycopy(b, ++offset, value, 0, len);
073        c.setValue (value);
074        return getLength()+1;
075    }
076    public void unpack (ISOComponent c, InputStream in) 
077        throws IOException, ISOException
078    {
079        byte[] b = readBytes (in, 1);
080        int len = b[0] & 0xFF;
081        c.setValue (readBytes (in, len));
082        in.skip (getLength () - len);
083    }
084    public ISOComponent createComponent(int fieldNumber) {
085        return new ISOBinaryField (fieldNumber);
086    }
087    public int getMaxPackedLength() {
088        return getLength() + 1;
089    }
090}
091