📄 messageelementimpl.java
字号:
/*
* 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.
*
* $Id: MessageElementImpl.java,v 1.5 2002/05/02 05:19:15 bondolo Exp $
*/
package net.jxta.impl.endpoint;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
/**
*
* @author chgenly
* @version
*/
public class MessageElementImpl extends MessageElement {
private String name;
private MimeMediaType type;
private byte[] bytes;
private int offset;
private int len;
/**
* Create a new Element, but dont add it to the message. The contents
* of the byte array are not copied.
*
* @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.
* @param len the number of bytes of the array starting from offset to
* use as the content of this element.
*
* @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 MessageElementImpl(String name, MimeMediaType type, byte[] b,
int offset, int len) {
this.name = name;
if( null != type )
this.type = type;
else
this.type = new MimeMediaType( "application/octet-stream" );
bytes = b;
this.offset = offset;
this.len = len;
}
/**
* Create a new Element, but don't 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. If the len is -1, then the input
* stream will be read until end of file, and then the stream will
* be closed.
*
* @throws NullPointerException if name or InputStream are null.
*
* @since JXTA 1.0
*/
public MessageElementImpl(String name, MimeMediaType type,
InputStream in, int len) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(len<0 ? 1024 : len);
byte[] buff = new byte[1024];
this.name = name;
if( null != type )
this.type = type;
else
this.type = new MimeMediaType( "application/octet-stream" );
int tcnt = 0;
while(len < 0 || tcnt < len) {
int needed = len - tcnt;
int thistime = len < 0 || needed > 1024 ? 1024 : needed;
int cnt = in.read(buff, 0, thistime);
if (cnt <= 0) break;
baos.write(buff, 0, cnt);
tcnt += cnt;
}
this.offset = 0;
this.len = tcnt;
bytes = baos.toByteArray();
if (len < 0)
in.close();
}
/**
* 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 MessageElementImpl(String name, MimeMediaType type,
InputStream in) throws IOException {
this(name, type, in, -1);
in.close();
}
/**
* Returns the offset into the array returned by getBytes()
* of where data use by this element starts.
*/
public int getOffset() {
return offset;
}
/**
* Returns the number of bytes used in the array
* returned by getBytes(). This may or may not be the
* entire length of the array returned by getBytes().
* <P>
* post:<br>
* 0 <= getLength() <= getBytes().length
*
*
* @return int the number of bytes of the element data.
*
* @since JXTA 1.0
**/
public int getLength() {
return len;
}
/**
* Returns the byte array which contains the element data.
* The portion of the array
* which is used as the element data starts at getOffset()
* and continues up to but not including the byte at
* getOffset()+getLength().
* <P>
* The byte array returned is not a copy of the element
* content.
*
* @return byte[] Contents of message element.
*/
public byte[] getBytesOffset() {
return bytes;
}
/**
* Returns a stream containing the element data.
*
* @return InputStream of the stream containing element data.
*
* @since JXTA 1.0
**/
public InputStream getStream() {
return new ByteArrayInputStream(bytes, offset, len);
}
/**
* Returns the name of the MessageElement.
*
* @return String containing the name of the MessageElement.
*
* @since JXTA 1.0
**/
public String getName() {
return name;
}
/**
* Returns the type of the MessageElement.
*
* @return MimeMediaType containing a copy of the type of the element.
* Will return "Application/Octet-Stream" if no type was originally
* specified.
*
* @since JXTA 1.0
**/
public MimeMediaType getType() {
return type;
}
/**
* make a clone of this element. Since element are immutable, this
* returns the same object.
*
* @return Element a clone of this element.
*
* @since JXTA 1.0
**/
public Object clone( ) {
return this;
}
/**
* Compare this MessageElement against another
*
* @param target The to compare against.
* @return boolean true if the elements are identical.
*
* @since JXTA 1.0
**/
public boolean equals( Object target ) {
if (!(target instanceof MessageElement))
return false;
MessageElement other = (MessageElement)target;
if (name == null ? other.getName() != null
: !name.equals(other.getName()))
return false;
if (type == null ? other.getType() != null
: !type.equals(other.getType()))
return false;
if (len != other.getLength())
return false;
byte[] otherBytes = other.getBytesOffset();
int otherI = other.getOffset();
for(int i=0; i<len; ++i)
if (bytes[offset+i] != otherBytes[otherI++])
return false;
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -