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

📄 messagewireformatbinary.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
字号:
/************************************************************************
 *
 * $Id: MessageWireFormatBinary.java,v 1.11 2002/03/08 18:11:09 bondolo Exp $
 *
 * Copyright (c) 2001 Sun Microsystems, Inc.  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
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
 *    must not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * 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 SUN MICROSYSTEMS 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 Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 *********************************************************************************/

package net.jxta.impl.endpoint;

import java.io.IOException;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.document.MimeMediaType;
import java.io.InputStream;
import java.io.DataInputStream;
import java.lang.reflect.InvocationTargetException;

import org.apache.log4j.Category;
import org.apache.log4j.Priority;

/**
 * Provides static utility methods to convert Message
 * to and from the serial format used on the wire.
 *
 * @author  chgenly
 * @version
 */
public class MessageWireFormatBinary extends MessageWireFormat {
    
    private static final Category LOG = Category.getInstance(MessageWireFormatBinary.class.getName());

    // Flag bits
    protected static final int HAS_TYPE  = 0x01;
    
    protected static final int MESSAGE_VERSION = 0;
    
    protected static final MimeMediaType outputMimeType = new MimeMediaType( "application/x-jxta-msg" );
    
    MimeMediaType type;
    
    public static class Instantiator implements MessageWireFormatFactory.Instantiator {
        
        public MimeMediaType getOutputMimeType() {
            return MessageWireFormatBinary.getOutputMimeType();
        }
        
        /** Creates new MessageWireFormat */
        public MessageWireFormat newInstance( MimeMediaType type ) {
            return new MessageWireFormatBinary( type );
        }
    };
    
    public static MimeMediaType getOutputMimeType() {
        return (MimeMediaType) outputMimeType.clone();
    }
    
    /** Creates new MessageWireFormat */
    public MessageWireFormatBinary( MimeMediaType type ) {
        this.type = type;
    }
    
    public void writeMessage(OutputStream os, Message m)
    throws IOException {
        Enumeration els;
        DataOutputStream dos = new DataOutputStream(os);
        //// Message signature to placate the bad man..
        dos.writeBytes("jxmg");
        
        dos.writeByte(MESSAGE_VERSION);
        
        //// Assign namespace ids.
        
        // maps a namespace to the assigned id.
        Hashtable namespace2id = new Hashtable();
        
        // The list of namespace order by how they
        // will appear on the wire.
        Vector namespaceTable = new Vector();
        assignNamespaceIds(m, namespace2id, namespaceTable);
        writeStringVector(dos, namespaceTable);
        
        // Write element count
        short elementCount = 0;
        els = m.getElements();
        while(els.hasMoreElements()) {
            els.nextElement();
            elementCount++;
        }
        dos.writeShort(elementCount);
        
        //// Write out the elements.
        els = ((MessageImpl) m).getElementsInFifoOrder();
        while(els.hasMoreElements()) {
            MessageElement el = (MessageElement)els.nextElement();
            writeMessageElement(dos, namespace2id, m, el);
        }
    }
    
    /**
     * Read one message from the input stream and return it.
     * The input stream remains open.
     */
    public void readMessage(InputStream is, Message msg)
    throws IOException {
        DataInputStream dis = new DataInputStream(is);
        
        // Read message signature
        if (dis.readByte() != 'j'
        ||  dis.readByte() != 'x'
        ||  dis.readByte() != 'm'
        ||  dis.readByte() != 'g')
            throw new IOException("Not a message");
        
        // Message version
        if (dis.readByte() != 0)
            throw new IOException("Message not version 0");
        
        // Namespaces
        int namespaceCnt = dis.readShort();
        Hashtable id2namespace = new Hashtable();
        id2namespace.put(new Integer(0), "");
        id2namespace.put(new Integer(1), "jxta");
        int id=2;
        for(int i=0; i<namespaceCnt; ++i) {
            try {
            String namespace = readString(dis);
            id2namespace.put(new Integer(id++), namespace);
            } catch ( IOException caught ) {
              if (LOG.isEnabledFor(Priority.WARN)) 
                LOG.warn( "Error Processing namespace", caught );
              throw caught;
            }
            
        }
        
        // Elements
        int elementCnt = dis.readShort();
        if (elementCnt < 0) throw new IOException("Negative element count");
        for(int i=0; i<elementCnt; ++i)
            try {
            readMessageElement(dis, id2namespace, msg);
            } catch ( IOException caught ) {
              if (LOG.isEnabledFor(Priority.WARN)) 
                LOG.warn( "Error Processing message element #" + i + " of " + elementCnt + ".", caught );
              throw caught;
            }
    }
    
    private void assignNamespaceIds(
    Message m,
    Hashtable namespace2id,
    Vector namespaceTable
    ) {
        int id=0;
        Enumeration namespaces = m.getNamespaces();
        
        namespace2id.clear();
        namespaceTable.clear();
        
        // insert the predefined namespaces.
        namespace2id.put("", new Integer(id++));
        namespace2id.put("jxta", new Integer(id++));
        
        // insert items in the vector if they are not found in the table
        while(namespaces.hasMoreElements()) {
            String namespace = (String) namespaces.nextElement();
            if (namespace2id.get(namespace) == null) {
                namespace2id.put(namespace, new Integer(id++));
                namespaceTable.addElement(namespace);
            }
        }
    }
    
    private void readMessageElement(
    DataInputStream dis,
    Hashtable id2namespace,
    Message m)
    throws IOException {
        // Read message signature
        if (dis.readByte() != 'j'
        ||  dis.readByte() != 'x'
        ||  dis.readByte() != 'e'
        ||  dis.readByte() != 'l')
            throw new IOException("Not a message element");
        
        // Namespace id
        int nsid = dis.readByte();
        String namespace = (String)id2namespace.get(new Integer(nsid));
        if (namespace == null) throw new IOException("bad namespace id");
        
        // flags
        byte flags = dis.readByte();
        
        // Name
        String name = readString(dis);
        
        // Mime type
        MimeMediaType type = null;
        if ((flags & HAS_TYPE) != 0) {
            String typeString = readString(dis);
            type = new MimeMediaType(typeString);
        }
        
        // Value
        int len = dis.readInt();
        byte[] value = new byte[len];
        dis.readFully(value);
        
        String qname;
        if (namespace.equals(""))
            qname = name;
        else
            qname = namespace + ":" + name;
        
        if (LOG.isEnabledFor(Priority.DEBUG)) 
          LOG.debug( "Add Message Element ns='" + namespace + "' name='" + name + "' len="+ value.length );

        MessageElement el =
        m.newMessageElement(qname, type, value);
        m.addElement(el);
    }
    
    /**
     *   Returns a stream which contains the
     *   flattened version of the element
     *   in wire format.
     **/
    private void writeMessageElement(
    DataOutputStream dos, Hashtable namespace2id, Message m, MessageElement el)
    throws IOException {
        String[] names = MessageElement.parseName(el.getName());
        String namespace = names[0];
        String name      = names[1];
        
        // Element signature to placate the bad man..
        dos.writeByte('j');
        dos.writeByte('x');
        dos.writeByte('e');
        dos.writeByte('l');
        
        // Namespace id
        int nsid = 0;
        Integer idi = (Integer)namespace2id.get(namespace);
        if (idi != null)
            nsid = idi.intValue();
        dos.writeByte(nsid);
        
        // Message element flags
        byte flags = 0;
        if (el.getType() != null)
            flags |= HAS_TYPE;
        
        dos.writeByte(flags);
        
        // Name
        if (name != null) {
            writeString(dos, name);
        }
        
        // Type
        MimeMediaType type = el.getType();
        if (type != null) {
            writeString(dos, type.toString());
        }
        /*
        if (encodings.size() != 0) {
            os.write((byte)encodings.size());
           Enumeration e = encodings.elements();
            while(e.hasMoreElements()) {
                String encoding = (String)e.nextElement();
                byte bs[] = encoding.getBytes("UTF8");
                os.write((short)bs.length);
                os.write(bs);
            }
        }
         **/
        byte[] bytes = el.getBytesOffset();
        dos.writeInt(el.getLength());
        dos.write(bytes, el.getOffset(), el.getLength());
    }
    
    private void writeStringVector(DataOutputStream dos, Vector v)
    throws IOException {
        dos.writeShort(v.size());
        Enumeration e = v.elements();
        while(e.hasMoreElements()) {
            writeString(dos, (String)e.nextElement());
        }
    }
    
    private String readString(DataInputStream dis)
    throws IOException {
        int len = dis.readShort();
        if (len < 0) throw new IOException("Bad string length in message");
        byte[] bytes = new byte[len];
        dis.readFully(bytes);
        return new String(bytes, "UTF8");
    }
    
    private void writeString(DataOutputStream dos, String s)
    throws IOException {
        dos.writeShort(s.length());
        dos.write(s.getBytes("UTF8"));
    }
}

⌨️ 快捷键说明

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