Project JXTA

net.jxta.endpoint
Class ByteArrayMessageElement

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

public class ByteArrayMessageElement
extends MessageElement

A Message Element using byte arrays for the element data.

This implementation does not copy the byte array provided and assumes that the contents of the byte array will not change through out the lifetime of the MessageElement.

some synchronization is due to optimization in getBytes(boolean) which replaces value of internal member b.


Field Summary
protected  byte[] b
          The bytes of this element.
protected  int len
          length of the element data. sometimes the same as b.length, but may be lesser.
protected  int offset
          This is the offset of our data within the array
 
Fields inherited from class net.jxta.endpoint.MessageElement
cachedGetByteLength, cachedGetBytes, cachedToString, name, properties, sig, type
 
Constructor Summary
ByteArrayMessageElement(String name, MimeMediaType type, byte[] b, int offset, int len, MessageElement sig)
          Create a new Element, but dont add it to the message.
ByteArrayMessageElement(String name, MimeMediaType type, byte[] b, int offset, MessageElement sig)
          Create a new MessageElement, The contents of the provided byte array are not copied during construction.
ByteArrayMessageElement(String name, MimeMediaType type, byte[] b, MessageElement sig)
          Create a new Message Element.
 
Method Summary
 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.
 byte[] getBytes()
          Returns the contents of this element as a byte array.
 byte[] getBytes(boolean copy)
          Returns a byte array which contains the element data.

synchronized so that we can replace our internal buffer with the buffer we are returning if we were using a shared buffer.

 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.

 String toString()
          

Returns a String representation of the element data. Returns the string representation of this element.

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

Field Detail

b

protected byte[] b
The bytes of this element.


offset

protected int offset
This is the offset of our data within the array


len

protected int len
length of the element data. sometimes the same as b.length, but may be lesser.

Constructor Detail

ByteArrayMessageElement

public ByteArrayMessageElement(String name,
                               MimeMediaType type,
                               byte[] b,
                               MessageElement sig)
Create a new Message Element. The contents of the provided byte array are not copied during construction.

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".
b - A byte array containing the contents of this element.
sig - optional message digest/digital signature elemnent or null if no signature is desired.

ByteArrayMessageElement

public ByteArrayMessageElement(String name,
                               MimeMediaType type,
                               byte[] b,
                               int offset,
                               MessageElement sig)
Create a new MessageElement, The contents of the provided byte array are not copied during construction.

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".
b - A byte array containing the contents of this element.
offset - all bytes before this location in b will be ignored.
sig - optional message digest/digital signature elemnent or null if no signature is desired.

ByteArrayMessageElement

public ByteArrayMessageElement(String name,
                               MimeMediaType type,
                               byte[] b,
                               int offset,
                               int len,
                               MessageElement sig)
Create a new Element, but dont add it to the message. The contents of the byte array are not copied during construction.

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".
b - A byte array containing the contents of this Element.
offset - all bytes before this location will be ignored.
len - number of bytes to include
sig - optional message digest/digital signature elemnent or null if no signature is desired.
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

toString

public String toString()

Returns a String representation of the element data. The 'charset' parameter of the message element's mimetype, if any, is used to determine encoding. If the charset specified is unsupported then the default enconding will be used.

synchronized for caching purposes. Returns the string representation of this element. The 'charset' parameter of the mimetype, if any, is used to determine encoding. If the charset specified is unsupported then the default enconding will be used.

Overrides:
toString in class MessageElement
Returns:
String string representation of this message element.

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.

getBytes

public byte[] getBytes(boolean copy)
Returns a byte array which contains the element data. The byte array returned may be shared amongst all copies of the element, do not modify it. The copy parameter allows you to request a private, modifiable copy of the element data.

This implementation builds the byte array from the stream.

synchronized so that we can replace our internal buffer with the buffer we are returning if we were using a shared buffer.

Overrides:
getBytes in class MessageElement
Parameters:
copy - If true then the result can be modified without damaging the state of this MessageElement. If false, then the result may be a shared copy of the data and should be considered read-only.
Returns:
byte[] Contents of message element.

getStream

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

Returns:
An InputStream containing the bytes of this 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 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.

getBytes

public byte[] getBytes()
Returns the contents of this element as a byte array. If this elements was originally constructed from a intact byte array, the array returned is a "shared" copy of the byte array used by this element. If this element was constructed with an offset of other than zero and a length different than the length of the source array then this function WILL RETURN A COPY OF THE BYTE ARRAY.

Returns:
a byte array containing the contents of this element.

JXTA J2SE