gumbo.net.msg.ascii
Class AsciiMessageWriter

java.lang.Object
  |
  +--gumbo.net.msg.AbstractMessageIO
        |
        +--gumbo.net.msg.ascii.AsciiMessageIO
              |
              +--gumbo.net.msg.ascii.AsciiMessageWriter
All Implemented Interfaces:
MessageIO, MessageWriter

public class AsciiMessageWriter
extends AsciiMessageIO
implements MessageWriter

Writes ASCII message elements (units, tokens, delimiters) to a writer or socket.

Version:
$Revision: 1.1 $
Author:
Jon Barrilleaux (jonb@jmbaai.com) of JMB and Associates Inc.

Field Summary
 
Fields inherited from class gumbo.net.msg.ascii.AsciiMessageIO
CHAR_MSG_END, CHAR_UNIT_END, UNIT_BODY, UNIT_DATA, UNIT_DONE, UNIT_FIRST, UNIT_HEAD, UNIT_LAST, UNIT_NONE, UNIT_PARM
 
Constructor Summary
AsciiMessageWriter(java.io.Writer writer)
           
 
Method Summary
 void close()
          Closes this writer.
 void flush()
          Flushes this writer.
 void writeMessage(Message msg)
          Writes a message, including its end delimiter, in a thread-safe manner.
 void writeMessageEnd()
          Writes the message end delimiter.
 void writeRawToken(java.lang.String string)
          Writes a raw string to the current message unit.
 void writeToken(double number)
          Writes a number token into the current message unit.
 void writeToken(long number)
          Writes a number token into the current message unit.
 void writeToken(java.lang.String word)
          Writes a word token into the current message unit.
 void writeUnit(java.lang.String data)
          Writes a message unit into the current message, with data provided as a string.
 void writeUnitEnd()
          Writes the message unit end delimiter.
 
Methods inherited from class gumbo.net.msg.ascii.AsciiMessageIO
currentUnit, nextUnit, resetUnit
 
Methods inherited from class gumbo.net.msg.AbstractMessageIO
addMessageIOListener, fireMessageRead, fireMessageWrite, getMessageIOListeners, removeMessageIOListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface gumbo.net.msg.MessageIO
addMessageIOListener, getMessageIOListeners, removeMessageIOListener
 

Constructor Detail

AsciiMessageWriter

public AsciiMessageWriter(java.io.Writer writer)
Method Detail

writeToken

public void writeToken(java.lang.String word)
                throws java.io.IOException,
                       MessageIOException
Writes a word token into the current message unit. Blocks until the token is sent. Guaranteed to write one and only one token, regardless of the word content. Note that in the message stream the token will be surrounded by double quotes and delimited with a newline character (white space). Any internal quotes and backslashes will be escaped with an added backslash.

Parameters:
word - The word string. Can safely contain any characters including whitespace (i.e. the string should not be quoted nor should characters be escaped for message transport). If null an empty quoted token is written.
Throws:
java.io.IOException - Writer is closed.
MessageIOException - Message is done. Call writeMessageEnd() to start a new message.

writeToken

public void writeToken(double number)
                throws java.io.IOException,
                       MessageIOException
Writes a number token into the current message unit. Blocks until the token is sent. Note that in the message stream the token will be delimited with a newline character (white space).

Parameters:
number - The number value.
Throws:
java.io.IOException - Writer is closed.
MessageIOException - Message is done. Call writeMessageEnd() to start a new message.

writeToken

public void writeToken(long number)
                throws java.io.IOException,
                       MessageIOException
Writes a number token into the current message unit. Blocks until the token is sent. Note that in the message stream the token will be delimited with a newline character (white space).

Parameters:
number - The number value.
Throws:
java.io.IOException - Writer is closed.
MessageIOException - Message is done. Call writeMessageEnd() to start a new message.

writeRawToken

public void writeRawToken(java.lang.String string)
                   throws java.io.IOException,
                          MessageIOException
Writes a raw string to the current message unit. Blocks until the string is sent. The string contents must conform to the message protocol for word and number token formatting (no error checking is performed). This is meant as a means of last resort for writing a token.

Parameters:
string - The raw string.
Throws:
java.io.IOException - Writer is closed.
MessageIOException - Message is done. Call writeMessageEnd() to start a new message.

writeUnit

public void writeUnit(java.lang.String data)
               throws java.io.IOException,
                      MessageIOException
Writes a message unit into the current message, with data provided as a string. Blocks until the unit is sent.

Parameters:
data - The unit data (i.e. without a unit delimiter).
Throws:
java.io.IOException - Writer is closed.
MessageIOException - Too many units in message.

writeUnitEnd

public void writeUnitEnd()
                  throws java.io.IOException,
                         MessageIOException
Writes the message unit end delimiter. Blocks until the unit end is sent.

Throws:
java.io.IOException - Writer is closed.
MessageIOException - Message is done. Call writeMessageEnd() to start a new message.

writeMessageEnd

public void writeMessageEnd()
                     throws java.io.IOException,
                            MessageIOException
Writes the message end delimiter. Blocks until the message end is sent. This method must be called after all units are written and before a new message can be started.

Throws:
java.io.IOException - Writer is closed.
MessageIOException - Too few units in message.

flush

public void flush()
           throws java.io.IOException
Description copied from interface: MessageWriter
Flushes this writer.

Specified by:
flush in interface MessageWriter
java.io.IOException

close

public void close()
           throws java.io.IOException
Description copied from interface: MessageWriter
Closes this writer.

Specified by:
close in interface MessageWriter
java.io.IOException

writeMessage

public void writeMessage(Message msg)
                  throws java.io.IOException,
                         MessageIOException,
                         MessageDataException
Description copied from interface: MessageWriter
Writes a message, including its end delimiter, in a thread-safe manner. Blocks until the message is completely sent. Typically, this method is synchronized on this message writer, which is used by all threads in the application to send messages.

Specified by:
writeMessage in interface MessageWriter
Throws:
MessageDataException - Message is ill-formed for writing.
java.io.IOException - Writer is closed.
MessageIOException - Message stream is out of sync.