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

📄 messageimpl.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/************************************************************************
 *
 * $Id: MessageImpl.java,v 1.28 2002/06/19 01:17:36 jice 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 net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.document.MimeMediaType;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import net.jxta.endpoint.MessageElementEnumeration;
import net.jxta.util.StringEnumeration;

/**
 * A MessageImpl contains data and protocol specific information that
 * are exchanged between peer protocols. A MessageImpl consists of a
 * queue of data chunks that are assiciated with a MessageImplTag.
 * A MessageImpl is also associated to the TransportAddress of the
 * sender and the receiver of the message. Note that several different chunks
 * of data associated to the same tag can be added. In that case, several gets
 * will be needed to retrieve all the chunks.
 *
 */

public class MessageImpl implements Message {

    
    /**
     * Elements are stored in this message implemenation using
     * a two level structure.  
     * <ol> 
     *  <li>The first level is a <code>Hashtable</code> 
     *      which maps a namespace name to 
     *    another <code>Hashtable</code>.
     * <li>The second level hashtable maps element names 
     *     to <code>MessageElement</code>s.
     * </ol>
     */
    private Hashtable namespace2elements;
    
    /**
     * Element order is important.  We use this vector to
     * hold the order of the elements.
     */
    Vector order;

    /**
     * User to mark if a message has already been through the filters.
     * This variable is directely used by the Endpoint (private API).
     **/
    public boolean filtered = false;
    
    /**
     * Constructor for outgoing message. 
     */
    public MessageImpl()
    {
        namespace2elements = new Hashtable();
        order = new Vector();
    }
        
    /**
     *  Create a new MessageElement
     *
     *  @param name Name of the Element. May be the empty string ("") if
     *   the Element is not named.
     *  @param type Type of the Element. null is equivalent to specifying
     *   the type "Application/Octet-stream"
     *  @param in the stream containing the body of the Element. The stream
     *   will be closed by the Element.
     *
     *  @throws NullPointerException if name or InputStream are null.
     *
     */
    public MessageElement newMessageElement(String name, 
        MimeMediaType type, InputStream in) 
    throws IOException
    {
        return new MessageElementImpl(name, type, in, -1);
    }
    
    /**
     *  Create a new Element, but dont add it to the message.
     *
     *  @param name Name of the Element. May be the empty string ("") if
     *   the Element is not named.
     *  @param type Type of the Element. null is equivalent to specifying
     *   the type "Application/Octet-stream"
     *  @param in the stream containing the body of the Element. If the
     *   stream does not support the "mark" operation then a copy of the
     *   stream contents is made immediately. The stream will <b>NOT</b> be
     *   closed by the Element.
     *  @param len The size of the Element will be limited to len bytes
     *   from the stream. If you are using the stream interface and know
     *   the size of the stream, specifying it here improves performance
     *   and space effciency a lot.
     *
     *  @throws NullPointerException if name or InputStream are null.
     *
     * @since JXTA 1.0
     */
    public MessageElement newMessageElement(String name, 
        MimeMediaType type, InputStream in, int len) throws IOException
    {
        return new MessageElementImpl(name, type, in, len);
    }
    
    /**
     * Create a new Element, but dont add it to the message. The contents
     * of the byte array are not copied.
     *
     * @param namespace namespace for the MessageElement.
     * @param name Name of the Element. May be the empty string ("") if
     *   the Element is not named.
     * @param type Type of the Element. null is equivalent to specifying
     *   the type "Application/Octet-stream"
     * @param b A byte array containing the contents of this element.
     * @param offset all bytes before this location in <code>b</code>
     *   will be ignored.
     *
     * @throws NullPointerException if name or b are null.
     * @throws IndexOutOfBoundsException if offset is negative or greater
     *  than or equal to the length of <code>b</code>
     *
     * @since JXTA 1.0
     */
    public MessageElement newMessageElement(String name, MimeMediaType type, 
        byte[] b, int offset, int len) 
    {
        return new MessageElementImpl(name, type, b, offset, len);
    }
    
    /**
     * Create a new Element, but dont add it to the message. The contents
     * of the byte array are not copied.
     *
     * @param nsname Name of the Element. May be the empty string ("") if
     *   the Element is not named.
     * @param type Type of the Element. null is equivalent to specifying
     *   the type "Application/Octet-stream"
     * @param b A byte array containing the contents of this element.
     *
     * @throws NullPointerException if name or b are null.
     *
     * @since JXTA 1.0
     */
    public MessageElement newMessageElement(String nsname, MimeMediaType type,
        byte[] b) {
        return new MessageElementImpl(nsname, type, b, 0, b.length);
    }

    /**
     * Remove an element from a message.  Removal is based on the
     * namespace and the name of the element passed in.
     *
     * @param element the Element to remove from the message.
     * @return boolean returns true if the element was removed, otherwise false.
     *
     * @since JXTA 1.0
     */
    public boolean removeElement(MessageElement element) {
        String nsname = element.getName();
        if (nsname == null)
            throw new RuntimeException("Element has null name");
                
        return removeElement(nsname) != null;
    }
    
    
    public MessageElement removeElement(String nsname)
    {
        String names[] = MessageElement.parseName(nsname);
        String namespace = names[0];
        String name = names[1];

        Hashtable elements = (Hashtable)namespace2elements.get(namespace);
        if (elements == null)
            return null;
        
        MessageElement elfound = (MessageElement)elements.get(name);
        if (elfound == null)
            return null;
        elements.remove(name);
        order.remove(elfound);
        
        
        // If there are no more elements in this namespace,
        // remove the namespace.
        if (elements.size() == 0)
            namespace2elements.remove(namespace);
        
        return elfound;
    }
    
    /**
     * Get the named element from the message and return
     * the element's byte array.  offset == 0. len == bytes.length.  
     */
    public byte[] getBytes(String nsname)
    {
        byte[] bytes = null;
        MessageElement el = getElement(nsname);
        if (el != null) {
            bytes = el.getBytesOffset();
            int offset = el.getOffset();
            if (offset != 0) {
                byte[] src = bytes;
                bytes = new byte[el.getLength()];
                System.arraycopy(src, el.getOffset(), bytes, 0, el.getLength());
            }
        }
        
        return bytes;
    }
    
    /**
     * set the contents of an Element. The namespace and the
     * name of the element are as defined by the arguments of 
     * the same name.  The value of the element is set form the
     * InputStream.
     * If the element is already in the message, its content is
     * changed, otherwise a new element is created and added into
     * the message.
     *
     * @param name Name of the new element.
     * @param bytes byte array which defines the value of the element.
     */
    public void setBytes(String name, byte[] bytes)
    {
        setBytes(name, bytes, 0, bytes.length);
    }
    
    /**
     * set the contents of an Element. The namespace and the
     * name of the element are as defined by the arguments of 
     * the same name.  The value of the element is set form the
     * InputStream.
     * If the element is already in the message, its content is
     * changed, otherwise a new element is created and added into
     * the message.
     *
     * @param nsname Name of the new element.
     * @param bytes byte array which defines the value of the element.
     * @param offset Start location in byte array.
     * @param len Length of byte array segment startin at offset.
     */
    public void setBytes(String nsname,
			 byte[] bytes,

⌨️ 快捷键说明

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