isobasepackager.java

来自「java pos,你可以直接编译运行,」· Java 代码 · 共 427 行 · 第 1/2 页

JAVA
427
字号
/* * Copyright (c) 2000 jPOS.org.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *    "This product includes software developed by the jPOS project  *    (http://www.jpos.org/)". Alternately, this acknowledgment may  *    appear in the software itself, if and wherever such third-party  *    acknowledgments normally appear. * * 4. The names "jPOS" and "jPOS.org" must not be used to endorse  *    or promote products derived from this software without prior  *    written permission. For written permission, please contact  *    license@jpos.org. * * 5. Products derived from this software may not be called "jPOS", *    nor may "jPOS" appear in their name, without prior written *    permission of the jPOS project. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.   * IN NO EVENT SHALL THE JPOS PROJECT OR ITS CONTRIBUTORS BE LIABLE FOR  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  * POSSIBILITY OF SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the jPOS Project.  For more * information please see <http://www.jpos.org/>. */package org.jpos.iso;import java.io.EOFException;import java.io.IOException;import java.io.InputStream;import java.util.BitSet;import java.util.Hashtable;import java.util.Vector;import org.jpos.util.LogEvent;import org.jpos.util.LogSource;import org.jpos.util.Logger;/** * provides base functionality for the actual packagers * * @author apr@cs.com.uy * @version $Id: ISOBasePackager.java,v 1.44 2004/04/12 22:12:51 apr Exp $ * @see org.jpos.iso.packager.ISO87APackager * @see org.jpos.iso.packager.ISO87BPackager */public abstract class ISOBasePackager implements ISOPackager, LogSource {    protected ISOFieldPackager[] fld;    protected Logger logger = null;    protected String realm = null;    public void setFieldPackager (ISOFieldPackager[] fld) {        this.fld = fld;    }    /**     * @return true if BitMap have to be emited     */    protected boolean emitBitMap () {        return (fld[1] instanceof ISOBitMapPackager);    }    /**     * usually 2 for normal fields, 1 for bitmap-less     * or ANSI X9.2      * @return first valid field     */    protected int getFirstField() {        if (!(fld[0] instanceof ISOMsgFieldPackager))            return (fld[1] instanceof ISOBitMapPackager) ? 2 : 1;        return 0;    }    /**     * @param   m   the Component to pack     * @return      Message image     * @exception ISOException     */    public byte[] pack (ISOComponent m) throws ISOException {        LogEvent evt = new LogEvent (this, "pack");        try {            if (m.getComposite() != m)                 throw new ISOException ("Can't call packager on non Composite");            ISOComponent c;            Vector v = new Vector();            Hashtable fields = m.getChildren();            int len = 0;            int first = getFirstField();            c = (ISOComponent) fields.get (new Integer (0));            byte[] b;            if (first > 0 && c != null) {                b = fld[0].pack(c);                len += b.length;                v.addElement (b);            }            if (emitBitMap()) {                // BITMAP (-1 in HashTable)                c = (ISOComponent) fields.get (new Integer (-1));                b = getBitMapfieldPackager().pack(c);                len += b.length;                v.addElement (b);            }            // if Field 1 is a BitMap then we are packing an            // ISO-8583 message so next field is fld#2.            // else we are packing an ANSI X9.2 message, first field is 1            int tmpMaxField=Math.min (m.getMaxField(), 128);            for (int i=first; i<=tmpMaxField; i++) {                if ((c=(ISOComponent) fields.get (new Integer (i))) != null)                {                    try {                        ISOFieldPackager fp = fld[i];                        if (fp == null)                            throw new ISOException ("null field packager");                        b = fp.pack(c);                        len += b.length;                        v.addElement (b);                    } catch (ISOException e) {                        evt.addMessage ("error packing field "+i);                        evt.addMessage (c);                        evt.addMessage (e);                        throw e;                    }                }            }                    if(m.getMaxField()>128 && fld.length > 128) {                for (int i=1; i<=64; i++) {                    if ((c = (ISOComponent)                         fields.get (new Integer (i+128))) != null)                    {                        try {                            b = fld[i+128].pack(c);                            len += b.length;                            v.addElement (b);                        } catch (ISOException e) {                            evt.addMessage ("error packing field "+(i+128));                            evt.addMessage (c);                            evt.addMessage (e);                            throw e;                        }                    }                }            }            int k = 0;            byte[] d = new byte[len];            for (int i=0; i<v.size(); i++) {                b = (byte[]) v.elementAt(i);                for (int j=0; j<b.length; j++)                    d[k++] = b[j];            }            if (logger != null)  // save a few CPU cycle if no logger available                evt.addMessage (ISOUtil.hexString (d));            return d;        } catch (ISOException e) {            evt.addMessage (e);            throw e;        } finally {            Logger.log(evt);        }    }    /**     * @param   m   the Container of this message     * @param   b   ISO message image     * @return      consumed bytes     * @exception ISOException     */    public int unpack (ISOComponent m, byte[] b) throws ISOException {        LogEvent evt = new LogEvent (this, "unpack");        try {            if (m.getComposite() != m)                 throw new ISOException ("Can't call packager on non Composite");            if (logger != null)  // save a few CPU cycle if no logger available                evt.addMessage (ISOUtil.hexString (b));            int consumed = 0;            if (!(fld[0] instanceof ISOMsgFieldPackager) &&                !(fld[0] instanceof ISOBitMapPackager))            {                ISOComponent mti = fld[0].createComponent(0);                consumed  = fld[0].unpack(mti, b, 0);                m.set (mti);            }            BitSet bmap = null;            int maxField = fld.length;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?