dLife Home Page

dlife.sys
Class CopyOfNonblockingBufferedInputStream

java.lang.Object
  extended by dlife.sys.CopyOfNonblockingBufferedInputStream

public class CopyOfNonblockingBufferedInputStream
extends Object

This class wraps a BufferedInputStream and provides several non-blocking calls for reading data from the stream. The wrapped BufferedInputStream must be an object that accurately supports the available() method.

Version:
May 15, 2010
Author:
Grant Braught, Dickinson College

Constructor Summary
CopyOfNonblockingBufferedInputStream(BufferedInputStream bis)
          Construct a new NonblockingBufferedInputStream around the provided BufferedInputStream.
 
Method Summary
 void close()
          Close the BufferedInputStream that is being used by this NonblockingBufferedInputStream.
 void flush()
          Discard all of the currently available data in this NonblockingBufferedInputStream.
 String readAvailable()
          Read all of the characters that are currently available from the BufferedInputStream and return them as a String.
 String readBytes(int count, int timeout)
          Read input from the BufferedInputStream up to the provided number of bytes if they become available before the specified timeout.
 String readToken(String delim, int timeout)
          Read input from the BufferedInputStream up to the provided delimiter if it becomes available before the specified timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CopyOfNonblockingBufferedInputStream

public CopyOfNonblockingBufferedInputStream(BufferedInputStream bis)
Construct a new NonblockingBufferedInputStream around the provided BufferedInputStream.

Parameters:
bis - the BufferedInputStream.
Method Detail

flush

public void flush()
Discard all of the currently available data in this NonblockingBufferedInputStream. NOTE: This method checks if the stream is empty after it attempts to flush. Thus, if the BufferedInputStream is constantly getting new data at a high enough rate this method could hang.


readAvailable

public String readAvailable()
Read all of the characters that are currently available from the BufferedInputStream and return them as a String. If the BufferedInputStream supports the available method then this method will not block. If there are no characters available an empty string is returned.

Returns:
a String containing the characters available from the BufferedInputStream.

readToken

public String readToken(String delim,
                        int timeout)
                 throws TimeoutException
Read input from the BufferedInputStream up to the provided delimiter if it becomes available before the specified timeout. If the delimiter is not read before the timeout, the read characters are discarded and a TimeoutException is thrown. If a delimiter is found the characters preceding it are returned as a String without the delimiter.

Parameters:
delim - the delimiter that appears between the tokens.
timeout - the maximum number of milliseconds to wait for the delimiter to be read.
Returns:
A String containing the characters that were read. The delimiter is not included.
Throws:
TimeoutException - if the delimiter is not read before timeout ms have passed.

readBytes

public String readBytes(int count,
                        int timeout)
                 throws TimeoutException
Read input from the BufferedInputStream up to the provided number of bytes if they become available before the specified timeout. If the specified number of bytes is not read before the timeout, the read characters are discarded and a TimeoutException is thrown.

Parameters:
count - the number of bytes to try to read.
timeout - the maximum number of ms to wait for the bytes to be read.
Returns:
A String containing the bytes.
Throws:
TimeoutException - if the requested number of bytes are not read before the timeout.

close

public void close()
           throws IOException
Close the BufferedInputStream that is being used by this NonblockingBufferedInputStream.

Throws:
IOException - if the stream cannot be closed.

dLife Home Page