edu.rice.cs.util
Class InputStreamRedirector

java.lang.Object
  extended by java.io.InputStream
      extended by edu.rice.cs.util.InputStreamRedirector
All Implemented Interfaces:
Closeable

public abstract class InputStreamRedirector
extends InputStream

Redirects requests for input through the abstract method _getInput().

Version:
$Id: InputStreamRedirector.java 4314 2008-01-30 00:08:33Z mgricken $

Field Summary
protected  ArrayList<Character> _buffer
          Buffer that stores the current set of bytes.
 
Constructor Summary
InputStreamRedirector()
          Constructs a new InputStreamRedirector.
 
Method Summary
protected abstract  String _getInput()
          This method gets called whenever input is requested from the stream and nothing is currently available.
 int available()
           
 int read()
          Overrides the read() in PipedInputStream so that if the stream is empty, it asks for more input from _getInput().
 int read(byte[] b)
          Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.
 int read(byte[] b, int off, int len)
          Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.
 
Methods inherited from class java.io.InputStream
close, mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_buffer

protected ArrayList<Character> _buffer
Buffer that stores the current set of bytes. TODO: perhaps this should use an array for efficiency This is only used as a char queue.

Constructor Detail

InputStreamRedirector

public InputStreamRedirector()
Constructs a new InputStreamRedirector.

Method Detail

_getInput

protected abstract String _getInput()
                             throws IOException
This method gets called whenever input is requested from the stream and nothing is currently available. Subclasses should return the appropriate input to feed to the input stream. When using a readLine() method, be sure to append a newline to the end of the input.

Returns:
the input to the stream, not the empty string
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.

Overrides:
read in class InputStream
Parameters:
b - the byte array to fill
Returns:
the number of bytes successfully read
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.

Overrides:
read in class InputStream
Parameters:
b - the byte array to fill
off - the offset in the byte array
len - the number of characters to try to read
Returns:
the number of bytes successfully read
Throws:
IOException

read

public int read()
         throws IOException
Overrides the read() in PipedInputStream so that if the stream is empty, it asks for more input from _getInput().

Specified by:
read in class InputStream
Returns:
the next character in the stream
Throws:
IOException - if an I/O exception

available

public int available()
Overrides:
available in class InputStream
Returns:
the number of characters available in this stream.