com.mooapi.common.message
Class StringMessage

java.lang.Object
  extended by com.mooapi.common.message.AbstractMessage<T>
      extended by com.mooapi.common.message.AbstractTypedMessage<java.lang.String>
          extended by com.mooapi.common.message.StringMessage

public class StringMessage
extends AbstractTypedMessage<java.lang.String>

This class represents a string message that can be sent as part of a message command between MooAPI clients and servers. It contains constructors and methods for encoding and decoding string messages. Instances of this class created from a byte array preserve that array so that the original array is used to decode the message. The consequence is that string messages are character-set neutral, so that messages relayed to connections via a server are unaffected by the server's character encoding; In this case, the server's character encoding only affects the way it interprets and displays messages.

Author:
Steve Taylor

Constructor Summary
StringMessage(byte[] bytes)
          Create a string message from a byte array, using the default character set to decode it.
StringMessage(byte[] bytes, java.nio.charset.Charset charset)
          Create a string message from a byte array, using the specified character set to decode it.
StringMessage(java.lang.String message)
          Create a string message from a string, using the default character set to encode it.
StringMessage(java.lang.String message, java.nio.charset.Charset charset)
          Create a string message from a string, using the specified character set to encode it.
 
Method Summary
 RawMessage asRawMessage()
          Convert this message into a raw message.
static StringMessage decode(org.apache.mina.common.ByteBuffer message)
          Decode the string message, including the leading type byte, using the default character set.
static StringMessage decode(org.apache.mina.common.ByteBuffer message, java.nio.charset.Charset charset)
          Decode the string message, including the leading type byte, using the specified character set.
protected  org.apache.mina.common.ByteBuffer encodeBody()
          Encode the string message, excluding the leading type byte.
 int getEncodedLength()
          Get the length, in bytes, of this message when encoded.
 MessageType getType()
          Get this message's type.
 
Methods inherited from class com.mooapi.common.message.AbstractTypedMessage
encode, equals, hashCode, toString
 
Methods inherited from class com.mooapi.common.message.AbstractMessage
getMessage
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringMessage

public StringMessage(java.lang.String message,
                     java.nio.charset.Charset charset)
Create a string message from a string, using the specified character set to encode it.

Parameters:
message - A string.
charset - A character set.

StringMessage

public StringMessage(java.lang.String message)

Create a string message from a string, using the default character set to encode it.

This is equivalent to this(message, Charset.defaultCharset()).

Parameters:
message - A string.

StringMessage

public StringMessage(byte[] bytes,
                     java.nio.charset.Charset charset)
Create a string message from a byte array, using the specified character set to decode it.

Parameters:
bytes - An encoded string.
charset - A character set.

StringMessage

public StringMessage(byte[] bytes)

Create a string message from a byte array, using the default character set to decode it.

This is equivalent to StrintMessage(bytes, Charset.defaultCharset()).

Parameters:
bytes - The string's encoded bytes.
Method Detail

getType

public MessageType getType()
Description copied from class: AbstractTypedMessage
Get this message's type.

Specified by:
getType in class AbstractTypedMessage<java.lang.String>
Returns:

decode

public static StringMessage decode(org.apache.mina.common.ByteBuffer message,
                                   java.nio.charset.Charset charset)
Decode the string message, including the leading type byte, using the specified character set.

Parameters:
message - The bytes to decode.
charset - The character set used to decode.
Returns:
The decoded message.
Throws:
java.lang.IllegalArgumentException - if the leading type byte is not MessageType.STRING.
java.nio.BufferUnderflowException - if message is empty.

decode

public static StringMessage decode(org.apache.mina.common.ByteBuffer message)

Decode the string message, including the leading type byte, using the default character set.

This is equivalent to decode(message, Charset.defaultCharset()).

Parameters:
message - The bytes to decode.
Returns:
The decoded message.
Throws:
java.lang.IllegalArgumentException - if the leading type byte is not MessageType.STRING.
java.nio.BufferUnderflowException - if message is empty.

getEncodedLength

public int getEncodedLength()
Description copied from class: AbstractMessage
Get the length, in bytes, of this message when encoded.

Specified by:
getEncodedLength in class AbstractMessage<java.lang.String>
Returns:

encodeBody

protected org.apache.mina.common.ByteBuffer encodeBody()
Encode the string message, excluding the leading type byte.

Specified by:
encodeBody in class AbstractTypedMessage<java.lang.String>
Returns:
The encoded string message.

asRawMessage

public RawMessage asRawMessage()
Convert this message into a raw message.

Specified by:
asRawMessage in class AbstractMessage<java.lang.String>
Returns:
A new raw message.


Copyright © 2009. All Rights Reserved.