Project JXTA

net.jxta.endpoint
Class InputStreamMessageElement

java.lang.Object
  extended by net.jxta.endpoint.MessageElement
      extended by net.jxta.endpoint.InputStreamMessageElement
All Implemented Interfaces:
Document

public class InputStreamMessageElement
extends MessageElement

A Message Element using InputStream as the source for the element data. This implementation copies all of the data from the stream at the time of creation.


Field Summary
protected  int cachedHashCode
          Cached Hash Code
protected  List databytes
          The bytes of this element.
protected  long length
          The length of the data.
 
Fields inherited from class net.jxta.endpoint.MessageElement
cachedGetByteLength, cachedGetBytes, cachedToString, name, properties, sig, type
 
Constructor Summary
InputStreamMessageElement(String name, MimeMediaType type, InputStream in, long len, MessageElement sig)
          Create a new Message Element.
InputStreamMessageElement(String name, MimeMediaType type, InputStream in, MessageElement sig)
          Create a new MessageElement.
 
Method Summary
protected  List CopyToDataBytes(InputStream in, long limit)
          Copy data from a stream with best possible effciency.
 boolean equals(Object target)
          

Elements are considered equal if they have the same name, type and signatures.

 long getByteLength()
          Returns the size of the element data in bytes.
 InputStream getStream()
          Returns the stream of bytes which represents the content of this Document.
 int hashCode()
          
 void sendToStream(OutputStream sendTo)
          Send the contents of this Document to the specified stream.

This version probably has sub-optimal performance.

 
Methods inherited from class net.jxta.endpoint.MessageElement
clone, copyInputStreamToOutputStream, getBytes, getElementName, getElementProperty, getFileExtension, getMimeType, getSequentialName, getSignature, getUniqueName, setElementProperty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

databytes

protected List databytes
The bytes of this element.


length

protected long length
The length of the data.


cachedHashCode

protected transient int cachedHashCode
Cached Hash Code

Constructor Detail

InputStreamMessageElement

public InputStreamMessageElement(String name,
                                 MimeMediaType type,
                                 InputStream in,
                                 MessageElement sig)
                          throws IOException
Create a new MessageElement. This constructor copies the data as needed and closes the stream upon completion. If the stream does not support the "mark" operation then the data is copied immediately and the stream will be closed.

Parameters:
name - Name of the MessageElement. May be the empty string ("") if the MessageElement is not named.
type - Type of the MessageElement. null is the same as specifying the type "Application/Octet-stream".
in - the stream containing the body of the MessageElement. The stream will be closed by the MessageElement.
sig - optional message digest/digital signature elemnent or null if no signature is desired.
Throws:
IOException - If there is a problem reading from the source stream.

InputStreamMessageElement

public InputStreamMessageElement(String name,
                                 MimeMediaType type,
                                 InputStream in,
                                 long len,
                                 MessageElement sig)
                          throws IOException
Create a new Message Element.

Parameters:
name - Name of the MessageElement. May be the empty string ("") if the MessageElement is not named.
type - Type of the MessageElement. null is the same as specifying the type "Application/Octet-stream".
in - the stream containing the body of the MessageElement. The stream will NOT be closed unless EOF is unexpectedly reached.
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. The stream must contain at least len bytes.
sig - optional message digest/digital signature elemnent or null if no signature is desired.
Throws:
IOException - if there is a problem reading from the source stream
Method Detail

equals

public boolean equals(Object target)

Elements are considered equal if they have the same name, type and signatures. Element data is not considered by this implementation as it is mostly intended for subclass use.

Overrides:
equals in class MessageElement

hashCode

public int hashCode()

Overrides:
hashCode in class MessageElement

getByteLength

public long getByteLength()
Returns the size of the element data in bytes.

Overrides:
getByteLength in class MessageElement
Returns:
long containing the size of the element data.

getStream

public InputStream getStream()
                      throws IOException
Returns the stream of bytes which represents the content of this Document.

Returns:
An InputStream containing the bytes of this Document.
Throws:
IOException - if an I/O error occurs.

sendToStream

public void sendToStream(OutputStream sendTo)
                  throws IOException
Send the contents of this Document to the specified stream.

This version probably has sub-optimal performance. Sub-classes should override this implementation.

Specified by:
sendToStream in interface Document
Overrides:
sendToStream in class MessageElement
Parameters:
sendTo - The OutputStream to which the Document will be written.
Throws:
IOException - if an I/O error occurs.

CopyToDataBytes

protected List CopyToDataBytes(InputStream in,
                               long limit)
                        throws IOException
Copy data from a stream with best possible effciency. Unfortunately, this still results in a lot of copying since we have often have no fore-knowledge of the length of the stream.

Parameters:
in - the stream to copy from
limit - the maximum number of bytes to copy from the stream. Long.LONG_MAX will read until EOF.
Returns:
an array of byte arrays containing the data.
Throws:
IOException - if there is a problem reading from the stream.

JXTA J2SE