/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2019 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.iso;
import java.io.IOException;
import java.io.InputStream;

public class IFEP_LLLBINARY extends ISOBinaryFieldPackager {
    public IFEP_LLLBINARY() {
        super();
    }
    /**
     * @param c - a component
     * @return packed component
     * @exception ISOException
     */
    public byte[] pack (ISOComponent c) throws ISOException {
        int len = ((byte[]) c.getValue()).length;
    
        if (len > getLength())  
            throw new ISOException (
                "invalid len "+len +" packing IFEP_LLLBINARY field "
                +c.getKey().toString()
            );

        byte[] b = new byte[len + 5];
        byte[] llltt = 
            ISOUtil.asciiToEbcdic (
              ISOUtil.zeropad(Integer.toString(len+2), 3)
              +ISOUtil.zeropad(c.getKey().toString(), 2));

        System.arraycopy(llltt, 0, b, 0, 5);
        System.arraycopy(c.getValue(), 0, b, 5, len);
        return b;
    }

    /**
     * @param c - the Component to unpack
     * @param b - binary image
     * @param offset - starting offset within the binary image
     * @return consumed bytes
     * @exception ISOException
     */
    public int unpack (ISOComponent c, byte[] b, int offset)
        throws ISOException
    {
        int len = Integer.parseInt(ISOUtil.ebcdicToAscii(b, offset, 3)) - 2;
        if (!(c instanceof ISOBinaryField))
            throw new ISOException 
                (c.getClass().getName() + " is not an ISOBinaryField");

        c.setFieldNumber (
            Integer.parseInt(ISOUtil.ebcdicToAscii (b, offset+3, 2))
        );
        byte[] value = new byte[len];
        System.arraycopy(b, offset+5, value, 0, len);
        c.setValue (value);
        return len + 5;
    }
    public void unpack (ISOComponent c, InputStream in) 
        throws IOException, ISOException
    {

        if (!(c instanceof ISOField))
            throw new ISOException 
                (c.getClass().getName() + " is not an ISOField");

        int len   = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes (in, 3))) - 2;
        int fldno = Integer.parseInt(ISOUtil.ebcdicToAscii(readBytes (in, 2)));
        c.setFieldNumber (fldno);
        c.setValue (readBytes(in, len));
    }
    public int getMaxPackedLength() {
        return getLength() + 5;
    }
}

@@ -894,60 +894,735 @@
 r %7B%0A
-    public IFEP_LLLBINARY() %7B%0A        super();%0A    %7D
+%0A    private static final int TAG_HEADER_LENGTH = 2;%0A    private final int prefixerPackedLength;%0A%0A    public IFEP_LLLBINARY() %7B%0A        super();%0A        prefixerPackedLength = 3;%0A    %7D%0A%0A    public IFEP_LLLBINARY(int length, String description) %7B%0A        super(length, description, LiteralBinaryInterpreter.INSTANCE, EbcdicPrefixer.LLL);%0A        checkLength(length, 999);%0A        prefixerPackedLength = EbcdicPrefixer.LLL.getPackedLength();%0A    %7D%0A%0A    public IFEP_LLLBINARY(int length, String description, BinaryInterpreter binaryInterpreter, Prefixer prefixer) %7B%0A        super(length, description, binaryInterpreter, prefixer);%0A        checkLength(length, 999);%0A        prefixerPackedLength = prefixer.getPackedLength();%0A    %7D%0A
 %0A   
@@ -2072,17 +2072,56 @@
 e%5Blen + 
-5
+prefixerPackedLength + TAG_HEADER_LENGTH
 %5D;%0A     
@@ -2230,13 +2230,49 @@
 len+
-2), 3
+ TAG_HEADER_LENGTH), prefixerPackedLength
 )%0A  
@@ -2323,17 +2323,33 @@
 ring(), 
-2
+TAG_HEADER_LENGTH
 ));%0A%0A   
@@ -2386,17 +2386,56 @@
 , b, 0, 
-5
+TAG_HEADER_LENGTH + prefixerPackedLength
 );%0A     
@@ -2474,17 +2474,56 @@
 , 0, b, 
-5
+TAG_HEADER_LENGTH + prefixerPackedLength
 , len);%0A
@@ -2927,15 +2927,50 @@
 et, 
-3)) - 2
+prefixerPackedLength)) - TAG_HEADER_LENGTH
 ;%0A  
@@ -3213,12 +3213,47 @@
 set+
-3, 2
+prefixerPackedLength, TAG_HEADER_LENGTH
 ))%0A 
@@ -3338,10 +3338,52 @@
 fset
-+5
+ + prefixerPackedLength + TAG_HEADER_LENGTH 
 , va
@@ -3445,17 +3445,56 @@
 n len + 
-5
+prefixerPackedLength + TAG_HEADER_LENGTH
 ;%0A    %7D%0A
@@ -3817,16 +3817,51 @@
 in, 
-3))) - 2
+prefixerPackedLength))) - TAG_HEADER_LENGTH
 ;%0A  
@@ -3932,17 +3932,33 @@
 es (in, 
-2
+TAG_HEADER_LENGTH
 )));%0A   
@@ -4106,9 +4106,48 @@
 ) + 
-5
+prefixerPackedLength + TAG_HEADER_LENGTH
 ;%0A  

Add field max length initialization for IFEP_LLLBINARY