gumbo.net.msg.ascii
Class AsciiMessageReader

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

public class AsciiMessageReader
extends AsciiMessageIO
implements MessageReader

Reads ASCII message elements (units, tokens, delimiters) from a reader.

Note that the StreamTokenizer returns the quote character, not TT_WORD, when a quoted word token is read. This is a crucial point that is sorely missing in the javadoc.

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
AsciiMessageReader(java.io.Reader reader)
           
 
Method Summary
 void atUnitEnd(int ttype)
          Used to verify that a special character read in a unit was the unit end.
 void close()
          Closes this message reader.
 double getTokenNumber()
          Gets the number in the token last read by readToken().
 java.lang.String getTokenWord()
          Gets the word in the token last read by readToken().
 Message readMessage()
          Reads a message, including its end delimiter, in a thread-safe manner.
 void readMessageEnd()
          Reads the message end delimiter, skipping any white space.
 int readToken()
          Reads the next token in the current message unit.
 java.lang.String readUnit()
          Reads the next message unit (head, parameters, or body) and returns its data as a string.
 void readUnitEnd()
          Reads the message unit end delimiter, skipping any white space.
 void syncMessageEnd()
          Reads the current message to its end.
 
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

AsciiMessageReader

public AsciiMessageReader(java.io.Reader reader)
Method Detail

readToken

public int readToken()
              throws java.io.IOException,
                     MessageIOException
Reads the next token in the current message unit. Blocks until a new token or the unit end is received.

Returns:
StreamTokenizer.TT_EOF if the unit (or message) ended; otherwise, TT_NUMBER or TT_WORD, indicating the type of token read.
Throws:
java.io.IOException - Reader is closed.
MessageIOException - Message ended before unit or message is done.

getTokenWord

public java.lang.String getTokenWord()
                              throws MessageIOException
Gets the word in the token last read by readToken().

Returns:
New String containing the word.
Throws:
MessageIOException - readToken() not yet called or did not read a word.

getTokenNumber

public double getTokenNumber()
                      throws MessageIOException
Gets the number in the token last read by readToken().

Returns:
The number.
Throws:
MessageIOException - readToken() not yet called or did not read a number.

readUnit

public java.lang.String readUnit()
                          throws java.io.IOException,
                                 MessageIOException
Reads the next message unit (head, parameters, or body) and returns its data as a string. Blocks until a new unit is received.

Returns:
A new String containing the unit data (i.e. including whitespace but not the unit delimiter). Empty if no data, but never null.
Throws:
java.io.IOException - Reader is closed.
MessageIOException - Message ended before unit or too many units for message.

readUnitEnd

public void readUnitEnd()
                 throws java.io.IOException,
                        MessageIOException
Reads the message unit end delimiter, skipping any white space. Blocks until the unit end is received.

Throws:
java.io.IOException - Reader is closed.
MessageIOException - Unit is not at the end (i.e. other data found).

atUnitEnd

public void atUnitEnd(int ttype)
               throws java.io.IOException,
                      MessageIOException
Used to verify that a special character read in a unit was the unit end.

Parameters:
ttype - Ttype from a parser.
Throws:
java.io.IOException - Reader is closed.
MessageIOException - Unit is not at the end (i.e. other data found).

readMessageEnd

public void readMessageEnd()
                    throws java.io.IOException,
                           MessageIOException
Reads the message end delimiter, skipping any white space. Blocks until the message end is received.

Throws:
java.io.IOException - Reader is closed.
MessageIOException - Message is not at the end (i.e. other data found) or too few units for message.

close

public void close()
           throws java.io.IOException
Description copied from interface: MessageReader
Closes this message reader.

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

readMessage

public Message readMessage()
                    throws java.io.IOException,
                           MessageIOException,
                           MessageDataException
Description copied from interface: MessageReader
Reads a message, including its end delimiter, in a thread-safe manner. Blocks until a message is completely received. Typically, this method is synchronized on this message reader, which is used by all threads in the application to receive messages.

Specified by:
readMessage in interface MessageReader
Returns:
The message. Never null.
Throws:
java.io.IOException - Reader is closed.
MessageIOException - Message end not found.
MessageDataException - Message data corrupted.

syncMessageEnd

public void syncMessageEnd()
                    throws java.io.IOException,
                           MessageIOException
Description copied from interface: MessageReader
Reads the current message to its end. Blocks until the end is received. Ignores everything but the message end delimiter. Useful for syncing the message stream following a message exception.

Specified by:
syncMessageEnd in interface MessageReader
Throws:
java.io.IOException - Reader is closed.
MessageIOException - Message end not found (which should never happen).