com.claritysys.io
Class MessageInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by com.claritysys.io.MessageInputStream
All Implemented Interfaces:
java.io.Closeable

public final class MessageInputStream
extends java.io.FilterInputStream

A MessageInputStream is used to receive data from an underlying InputStream where each block of bytes contains a 2 byte length header, in network byte order, of the amount of data contained in the message. The length does not include the 2 byte header.

Version:
$Revision: 2348 $ $Date: 2004-12-01 16:12:21 -0800 (Wed, 01 Dec 2004) $

Field Summary
protected  boolean atEOF
          A flag to remember that we've already hit EOF.
protected  byte[] buffer
          The buffer in which message bytes are queued up for reading.
protected  int dataLength
          The current length of data in the buffer.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
MessageInputStream(java.io.InputStream in)
          Create a new MessageInputStream on the given InputStream.
 
Method Summary
 int read(byte[] b)
          Read an array full of bytes from this stream.
 int read(byte[] b, int off, int len)
          Read an array full of bytes from this stream.
 java.lang.String readString()
          Read a String from the underlying stream.
 int receive()
          Wait for an incoming message from the InputStream.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffer

protected byte[] buffer
The buffer in which message bytes are queued up for reading.


dataLength

protected int dataLength
The current length of data in the buffer.


atEOF

protected boolean atEOF
A flag to remember that we've already hit EOF.

Constructor Detail

MessageInputStream

public MessageInputStream(java.io.InputStream in)
Create a new MessageInputStream on the given InputStream. This may be a stream gotten via a call to socket.getInputStream ().

Parameters:
in - The underlying InputStream to read from.
Method Detail

read

public int read(byte[] b)
         throws java.io.IOException
Read an array full of bytes from this stream.

Be sure that the array is larger than any potential message.

Overrides:
read in class java.io.FilterInputStream
Parameters:
b - A byte array to read data into.
Returns:
The number of bytes actually read.
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Read an array full of bytes from this stream.

Be sure that the array is larger than any potential message.

Overrides:
read in class java.io.FilterInputStream
Parameters:
b - A byte array to read data into.
off - The starting position to read into.
len - The maximum number of bytes to read.
Returns:
The number of bytes actually read.
Throws:
java.io.IOException

readString

public java.lang.String readString()
                            throws java.io.IOException
Read a String from the underlying stream. The String may or may not be terminated by a newline, whatever that may mean on the current platform. Instead, this reads the message and constructs a String for the entire length of data using the platform's default byte to char conversion encoding.

If data has been read from this stream already this will return the remaining data. If there is no data available, this will block until a message is received.

Returns:
A String built on the bytes received.
Throws:
java.io.IOException
See Also:
String.String(byte[],int,int)

receive

public int receive()
            throws java.io.IOException
Wait for an incoming message from the InputStream.

Returns:
The size of the message received.
Throws:
java.io.IOException


Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.