📄 isobasepackager.java
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2008 Alejandro P. Revilla * * 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.EOFException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.BitSet;import java.util.Hashtable;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 2594 2008-01-22 16:41:31Z apr $ * @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; protected int headerLength = 0; 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; ArrayList v = new ArrayList(128); Hashtable fields = m.getChildren(); int len = 0; int first = getFirstField(); c = (ISOComponent) fields.get (new Integer (0)); byte[] b; if (m instanceof ISOMsg && headerLength>0) { byte[] h = ((ISOMsg) m).getHeader(); if (h != null) len += h.length; } if (first > 0 && c != null) { b = fld[0].pack(c); len += b.length; v.add (b); } if (emitBitMap()) { // BITMAP (-1 in HashTable) c = (ISOComponent) fields.get (new Integer (-1)); b = getBitMapfieldPackager().pack(c); len += b.length; v.add (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 "+i+" packager"); b = fp.pack(c); len += b.length; v.add (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.add (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]; // if ISOMsg insert header if (m instanceof ISOMsg && headerLength>0) { byte[] h = ((ISOMsg) m).getHeader(); if (h != null) for (int j=0; j<h.length; j++) d[k++] = h[j]; } for (int i=0; i<v.size(); i++) { b = (byte[]) v.get(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 ISOMsg and headerLength defined if (m instanceof ISOMsg /*&& ((ISOMsg) m).getHeader()==null*/ && headerLength>0) { byte[] h = new byte[headerLength]; for (int i=0; i<headerLength; i++) h[i] = b[i]; ((ISOMsg) m).setHeader(h); consumed += headerLength; } if (!(fld[0] instanceof ISOBitMapPackager)) { ISOComponent mti = fld[0].createComponent(0); consumed += fld[0].unpack(mti, b, consumed); m.set (mti); } BitSet bmap = null; int maxField = fld.length; if (emitBitMap()) { ISOBitMap bitmap = new ISOBitMap (-1); consumed += getBitMapfieldPackager().unpack(bitmap,b,consumed); bmap = (BitSet) bitmap.getValue(); if (logger != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -