⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isomsg.java

📁 POS is a Java&#174 platform-based, mission-critical, ISO-8583 based financial transaction library/fr
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param fldno the Field Number     * @return field's String value     */    public String getString (int fldno) {        String s = null;        if (hasField (fldno)) {            try {                Object obj = getValue(fldno);                if (obj instanceof String)                    s = (String) obj;                else if (obj instanceof byte[])                    s = ISOUtil.hexString ((byte[]) obj);            } catch (ISOException e) {                // ignore ISOException - return null            }        }        return s;    }    /**     * Return the String value associated with the given field path      * @param fpath field path     * @return field's String value (may be null)     */    public String getString (String fpath) {        String s = null;        try {            Object obj = getValue(fpath);            if (obj instanceof String)                s = (String) obj;            else if (obj instanceof byte[])                s = ISOUtil.hexString ((byte[]) obj);        } catch (ISOException e) {            // ignore ISOException - return null        }        return s;    }    /**     * Return the byte[] value associated with the given ISOField number     * @param fldno the Field Number     * @return field's byte[] value     */    public byte[] getBytes (int fldno) {        byte[] b = null;        if (hasField (fldno)) {            try {                Object obj = getValue(fldno);                if (obj instanceof String)                    b = ((String) obj).getBytes();                else if (obj instanceof byte[])                    b = ((byte[]) obj);            } catch (ISOException e) {                // ignore ISOException - return null            }        }        return b;    }    /**     * Return the String value associated with the given field path      * @param fpath field path     * @return field's byte[] value (may be null)     */    public byte[] getBytes (String fpath) {        byte[] b = null;        try {            Object obj = getValue(fpath);            if (obj instanceof String)                b = ((String) obj).getBytes();            else if (obj instanceof byte[])                b = ((byte[]) obj);        } catch (ISOException e) {            // ignore ISOException - return null        }        return b;    }    /**     * Check if a given field is present     * @param fldno the Field Number     * @return boolean indicating the existence of the field     */    public boolean hasField(int fldno) {        return fields.get(new Integer(fldno)) != null;    }    /**     * Check if all fields are present     * @param fields an array of fields to check for presence     * @return true if all fields are present     */    public boolean hasFields (int[] fields) {        for (int i=0; i<fields.length; i++)             if (!hasField (fields[i]))                return false;        return true;    }    /**     * Don't call setValue on an ISOMsg. You'll sure get     * an ISOException. It's intended to be used on Leafs     * @see ISOField     * @see ISOException     */    public void setValue(Object obj) throws ISOException {        throw new ISOException ("setValue N/A in ISOMsg");    }        public Object clone() {        try {            ISOMsg m = (ISOMsg) super.clone();            m.fields = (Hashtable) fields.clone();            if (header != null)                m.header = (ISOHeader) header.clone();            for (Enumeration e = fields.keys(); e.hasMoreElements(); ) {                Integer k = (Integer) e.nextElement();                ISOComponent c = (ISOComponent) m.fields.get (k);                if (c instanceof ISOMsg)                     m.fields.put (k, ((ISOMsg)c).clone());            }            return m;        } catch (CloneNotSupportedException e) {            throw new InternalError();        }    }    /**     * Partially clone an ISOMsg     * @param fields int array of fields to go     * @return new ISOMsg instance     */    public Object clone(int[] fields) {        try {            ISOMsg m = (ISOMsg) super.clone();            m.fields = new Hashtable();            for (int i=0; i<fields.length; i++) {                if (hasField(fields[i])) {                    try {                        m.set (getComponent(fields[i]));                    } catch (ISOException e) {                         // it should never happen                    }                }            }            return m;        } catch (CloneNotSupportedException e) {            throw new InternalError();        }    }    /**     * add all fields present on received parameter to this ISOMsg<br>     * please note that received fields take precedence over      * existing ones (simplifying card agent message creation      * and template handling)     * @param m ISOMsg to merge     */    public void merge (ISOMsg m) {        for (int i=0; i<=m.getMaxField(); i++)             try {                if (m.hasField(i))                    set (m.getComponent(i));            } catch (ISOException e) {                // should never happen             }    }    /**     * @return a string suitable for a log     */    public String toString() {        StringBuffer s = new StringBuffer();        if (isIncoming())            s.append("<-- ");        else if (isOutgoing())            s.append("--> ");        else            s.append("    ");        s.append(getString(0));        if (hasField(11)) {            s.append(' ');            s.append(getString(11));        }        if (hasField(41)) {            s.append(' ');            s.append(getString(41));        }        return s.toString();    }    public Object getKey() throws ISOException {        if (fieldNumber != -1)            return new Integer(fieldNumber);        throw new ISOException ("This is not a subField");    }    public Object getValue() {        return this;    }    /**     * @return true on inner messages     */    public boolean isInner() {        return fieldNumber > -1;    }    /**     * @param mti new MTI     * @exception ISOException if message is inner message     */    public void setMTI (String mti) throws ISOException {        if (isInner())            throw new ISOException ("can't setMTI on inner message");        set (new ISOField (0, mti));    }    /**     * moves a field (renumber)     * @param oldFieldNumber old field number     * @param newFieldNumber new field number     * @throws ISOException     */    public void move (int oldFieldNumber, int newFieldNumber)         throws ISOException    {        ISOComponent c = getComponent (oldFieldNumber);        unset (oldFieldNumber);        if (c != null) {            c.setFieldNumber (newFieldNumber);            set (c);        } else            unset (newFieldNumber);    }    /**     * @return current MTI     * @exception ISOException on inner message or MTI not set     */    public String getMTI() throws ISOException {        if (isInner())            throw new ISOException ("can't getMTI on inner message");        else if (!hasField(0))            throw new ISOException ("MTI not available");        return (String) getValue(0);    }    /**     * @return true if message "seems to be" a request     * @exception ISOException on MTI not set     */    public boolean isRequest() throws ISOException {        return Character.getNumericValue(getMTI().charAt (2))%2 == 0;    }    /**     * @return true if message "seems not to be" a request     * @exception ISOException on MTI not set     */    public boolean isResponse() throws ISOException {        return !isRequest();    }    /**     * @return true if message is Retransmission     * @exception ISOException on MTI not set     */    public boolean isRetransmission() throws ISOException {        return getMTI().charAt(3) == '1';    }    /**     * sets an appropiate response MTI.     *     * i.e. 0100 becomes 0110<br>     * i.e. 0201 becomes 0210<br>     * i.e. 1201 becomes 1210<br>     * @exception ISOException on MTI not set or it is not a request     */    public void setResponseMTI() throws ISOException {        if (!isRequest())            throw new ISOException ("not a request - can't set response MTI");        String mti = getMTI();        char c1 = mti.charAt(3);        char c2 = '0';        switch (c1)        {            case '0' :            case '1' : c2='0';break;            case '2' :            case '3' : c2='2';break;            case '4' :            case '5' : c2='4';break;        }        set (new ISOField (0,            mti.substring(0,2)            +(Character.getNumericValue(getMTI().charAt (2))+1) + c2            )        );    }    /**     * sets an appropiate retransmission MTI<br>     * @exception ISOException on MTI not set or it is not a request     */    public void setRetransmissionMTI() throws ISOException {        if (!isRequest())            throw new ISOException ("not a request");        set (new ISOField (0, getMTI().substring(0,3) + "1"));    }    protected void writeHeader (ObjectOutput out) throws IOException {        int len = header.getLength();        if (len > 0) {            out.writeByte ('H');            out.writeShort (len);            out.write (header.pack());        }    }        protected void readHeader (ObjectInput in)         throws IOException, ClassNotFoundException    {        byte[] b = new byte[in.readShort()];        in.readFully (b);        setHeader (b);    }    protected void writePackager(ObjectOutput out) throws IOException {        out.writeByte('P');        String pclass = ((Class) packager.getClass()).getName();        byte[] b = pclass.getBytes();        out.writeShort(b.length);        out.write(b);    }    protected void readPackager(ObjectInput in) throws IOException,    ClassNotFoundException {        byte[] b = new byte[in.readShort()];        in.readFully(b);        try {            Class mypClass = Class.forName(new String(b));            ISOPackager myp = (ISOPackager) mypClass.newInstance();            setPackager(myp);        } catch (Exception e) {            setPackager(null);        }}    protected void writeDirection (ObjectOutput out) throws IOException {        out.writeByte ('D');        out.writeByte (direction);    }    protected void readDirection (ObjectInput in)         throws IOException, ClassNotFoundException    {        direction = in.readByte();    }     public void writeExternal (ObjectOutput out) throws IOException {        out.writeByte (0);  // reserved for future expansion (version id)        out.writeShort (fieldNumber);        if (header != null)             writeHeader (out);        if (packager != null)            writePackager(out);        if (direction > 0)            writeDirection (out);        for (Enumeration e = fields.elements(); e.hasMoreElements(); ) {            ISOComponent c = (ISOComponent) e.nextElement();            if (c instanceof ISOMsg) {                out.writeByte ('M');                ((Externalizable) c).writeExternal (out);            }            else if (c instanceof ISOBinaryField) {                out.writeByte ('B');                ((Externalizable) c).writeExternal (out);            }            else if (c instanceof ISOField) {                out.writeByte ('F');                ((Externalizable) c).writeExternal (out);            }        }        out.writeByte ('E');    }    public void readExternal  (ObjectInput in)         throws IOException, ClassNotFoundException    {        in.readByte();  // ignore version for now        fieldNumber = in.readShort();        byte fieldType;        ISOComponent c;        try {            while ((fieldType = in.readByte()) != 'E') {                c = null;                switch (fieldType) {                    case 'F':                        c = new ISOField ();                        break;                    case 'B':                        c = new ISOBinaryField ();                        break;                    case 'M':                        c = new ISOMsg ();                        break;                    case 'H':                        readHeader (in);                        break;                    case 'P':                        readPackager(in);                        break;                    case 'D':                        readDirection (in);                        break;                    default:                        throw new IOException ("malformed ISOMsg");                }                if (c != null) {                    ((Externalizable)c).readExternal (in);                    set (c);                }            }        }        catch (ISOException e) {            throw new IOException (e.getMessage());        }    }    /**     * Let this ISOMsg object hold a weak reference to an ISOSource     * (usually used to carry a reference to the incoming ISOChannel)     * @param source an ISOSource     */    public void setSource (ISOSource source) {        this.sourceRef = new WeakReference (source);    }    /**     * @return an ISOSource or null     */    public ISOSource getSource () {        return (sourceRef != null) ? (ISOSource) sourceRef.get () : null;    }}

⌨️ 快捷键说明

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