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

📄 isomsg.java

📁 pos机交易实现源代码 含金融卡交易8583协议实现 开发环境:linux 典型应用:嵌入系统开发 仅供参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    // Uncomment to include bitmaps within logs	    // 	    // if (i == 0) {	    //  if ((c = (ISOComponent) fields.get (new Integer (-1))) != null)	    //    c.dump (p, newIndent);	    // }	    //	}        p.println (indent + "</" + XMLPackager.ISOMSG_TAG+">");    }    /**     * get the component associated with the given field number     * @param fldno the Field Number     * @return the Component     */    public ISOComponent getComponent(int fldno) {        return (ISOComponent) fields.get(new Integer(fldno));    }    /**     * Return the object value associated with the given field number     * @param fldno the Field Number     * @return the field Object     */    public Object getValue(int fldno) throws ISOException {        return getComponent(fldno).getValue();    }    /**     * Return the String value associated with the given ISOField number     * @param fldno the Field Number     * @return field's String value     */    public String getString (int fldno) {        String s = null;        try {            Object obj = getValue(fldno);            if (obj instanceof String)                s = (String) obj;        } catch (ISOException e) {            // ignore ISOException - return null        }        return s;    }    /**     * 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();            return (Object) 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 (Object) 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("    ");        try {            s.append((String) getValue(0));            if (hasField(11)) {                s.append(' ');                s.append((String) getValue(11));            }            if (hasField(41)) {                s.append(' ');                s.append((String) getValue(41));            }        } catch (ISOException e) { }        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<br>     * 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();	set (new ISOField (0,	    mti.substring(0,2)		+(Character.getNumericValue(getMTI().charAt (2))+1) + "0"	    )	);    }    /**     * 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 writeDirection (ObjectOutput out) throws IOException {        out.writeByte ('D');        out.writeByte (direction);    }    protected void readDirection (ObjectInput in)         throws IOException, ClassNotFoundException    {        direction = (int) 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 (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;        int fldno;        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 '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());        }    }}

⌨️ 快捷键说明

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