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    public IFB_LLHFBINARY() {
033        super();
034    }
035    /**
036    * @param len - field len
037    * @param description symbolic descrption
038    */
039    public IFB_LLHFBINARY (int len, String description) {
040    super(len, description);
041    }
042   /**
043    * @param c - a component
044    * @return packed component
045    * @exception ISOException
046    */
047    public byte[] pack (ISOComponent c) throws ISOException {
048        int len = ((byte[]) c.getValue()).length;
049        if (len > getLength() || len>255) {
050            throw new ISOException (
051                "invalid len "+len +" packing field "+ c.getKey()
052            );
053        }
054        byte[] b = new byte[getLength() + 1];
055        b[0] = (byte) len;
056        System.arraycopy(c.getValue(), 0, b, 1, len);
057        return b;
058    }
059   /**
060    * @param c - the Component to unpack
061    * @param b - binary image
062    * @param offset - starting offset within the binary image
063    * @return consumed bytes
064    * @exception ISOException
065    */
066    public int unpack (ISOComponent c, byte[] b, int offset) throws ISOException
067    {
068        int len = b[offset] & 0xFF;
069        byte[] value = new byte[len];
070        System.arraycopy(b, ++offset, value, 0, len);
071        c.setValue (value);
072        return getLength()+1;
073    }
074    public void unpack (ISOComponent c, InputStream in) 
075        throws IOException, ISOException
076    {
077        byte[] b = readBytes (in, 1);
078        int len = b[0] & 0xFF;
079        c.setValue (readBytes (in, len));
080        in.skip (getLength () - len);
081    }
082    public ISOComponent createComponent(int fieldNumber) {
083        return new ISOBinaryField (fieldNumber);
084    }
085    public int getMaxPackedLength() {
086        return getLength() + 1;
087    }
088}
089