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, DocumentStreamIO

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.

InputStreamMessageElement is not as efficient as other message element types and should only be used when an input stream is the only available source for the element data.


Field Summary
protected  int cachedHashCode
          Cached Hash Code
protected  List<byte[]> 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, 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<byte[]> CopyToDataBytes(InputStream in, long limit)
          Copy data from a stream with best possible efficiency.
 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.
 
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 final List<byte[]> databytes
The bytes of this element.


length

protected final 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.

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 element 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 efficiency a lot. The stream must contain at least len bytes.
sig - optional message digest/digital signature element 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 - For errors while writing the Document.

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 DocumentStreamIO
Overrides:
sendToStream in class MessageElement
Parameters:
sendTo - The OutputStream to which the Document will be written.
Throws:
IOException - For errors while writing the Document.

CopyToDataBytes

protected List<byte[]> CopyToDataBytes(InputStream in,
                                       long limit)
                                throws IOException
Copy data from a stream with best possible efficiency. 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:
A list of buffers.
Throws:
IOException - if there is a problem reading from the stream.

JXSE