JXTA

net.jxta.endpoint
Class StringMessageElement

java.lang.Object
  extended by net.jxta.endpoint.MessageElement
      extended by net.jxta.endpoint.TextMessageElement
          extended by net.jxta.endpoint.StringMessageElement
All Implemented Interfaces:
Document, DocumentStreamIO, TextDocument, TextDocumentReaderIO

public class StringMessageElement
extends TextMessageElement

A Message Element using character strings for the element data.


Field Summary
protected  String data
          The data for this Message Element.
 
Fields inherited from class net.jxta.endpoint.TextMessageElement
cachedGetCharLength, cachedGetChars
 
Fields inherited from class net.jxta.endpoint.MessageElement
cachedGetByteLength, cachedGetBytes, cachedToString, name, sig, type
 
Constructor Summary
StringMessageElement(String name, String value, MessageElement sig)
          Create a new Message Element from the provided String.
StringMessageElement(String name, String value, String encoding, MessageElement sig)
          Create a new Message Element from the provided String.
 
Method Summary
 boolean equals(Object target)
          

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

 byte[] getBytes(boolean copy)
          Returns a byte array which contains the element data.
 long getCharLength()
          Returns the size of the element data in characters

synchronized for caching purposes.

 char[] getChars(boolean copy)
          Returns a char array which contains the element data.
 Reader getReader()
          Returns the sequence of characters which represents the content of the TextDocument.
 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.
 void sendToWriter(Writer sendTo)
          Send the contents of this TextDocument to the specified Writer.
 String toString()
          

Returns a String representation of the element data.

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

Field Detail

data

protected String data
The data for this Message Element.

Constructor Detail

StringMessageElement

public StringMessageElement(String name,
                            String value,
                            MessageElement sig)
Create a new Message Element from the provided String. The String will be encoded for transmission using UTF-8.

Parameters:
name - Name of the Element. May be the empty string ("") or null if the Element is not named.
value - A String containing the contents of this element.
sig - Message digest/digital signature element. If no signature is to be specified, pass null.
Throws:
IllegalArgumentException - if value is null.

StringMessageElement

public StringMessageElement(String name,
                            String value,
                            String encoding,
                            MessageElement sig)
                     throws UnsupportedEncodingException
Create a new Message Element from the provided String. The string will be encoded for transmission using specified character encoding.

Parameters:
name - Name of the MessageElement. May be the empty string ("") or null if the MessageElement is not named.
value - A String containing the contents of this element.
encoding - Name of the character encoding to use. If null then the system default character encoding will be used. (Using the system default character encoding should be used with extreme caution).
sig - Message digest/digital signature element. If no signature is to be specified, pass null.
Throws:
IllegalArgumentException - if value is null.
UnsupportedEncodingException - if the requested encoding is not supported.
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 encoding will be used.

synchronized for caching purposes.

synchronized for caching purposes.

Overrides:
toString in class TextMessageElement

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.

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.

getCharLength

public long getCharLength()
Returns the size of the element data in characters

synchronized for caching purposes.

Overrides:
getCharLength in class TextMessageElement
Returns:
long containing the size of the element data.

getChars

public char[] getChars(boolean copy)
Returns a char array which contains the element data. The char 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.

synchronized for caching purposes.

Overrides:
getChars in class TextMessageElement
Parameters:
copy - return a copy if true
Returns:
char[] Contents of message element.

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.

getReader

public Reader getReader()
                 throws IOException
Returns the sequence of characters which represents the content of the TextDocument.

Returns:
InputStream of the stream containing element data.
Throws:
IOException - when there is a problem getting a reader.

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.

sendToWriter

public void sendToWriter(Writer sendTo)
                  throws IOException
Send the contents of this TextDocument to the specified Writer.

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

Specified by:
sendToWriter in interface TextDocumentReaderIO
Overrides:
sendToWriter in class TextMessageElement
Parameters:
sendTo - The Writer to which the characters of the TextDocument will be written.
Throws:
IOException - If an I/O error occurs.

JXSE