edu.rice.cs.plt.io
Class DirectReader

java.lang.Object
  extended by java.io.Reader
      extended by edu.rice.cs.plt.io.DirectReader
All Implemented Interfaces:
Closeable, Readable
Direct Known Subclasses:
WrappedDirectReader

public abstract class DirectReader
extends Reader

A Reader that supports reading directly into a Writer. This class provides default implementations defined in terms of Reader and Writer methods. Subclasses can override (at least) readAll(Writer, char[]) and read(Writer, int, char[]) to provide better implementations (by, for example, not invoking Reader.read(char[])).

See Also:
DirectInputStream, DirectWriter, DirectOutputStream

Field Summary
protected static int DEFAULT_BUFFER_SIZE
           
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
DirectReader()
           
 
Method Summary
 int read(Writer w, int chars)
          Read some number of characters from this reader, sending them to the provided Writer.
 int read(Writer w, int chars, char[] buffer)
          Read some number of characters from this reader, sending them to the provided Writer.
 int read(Writer w, int chars, int bufferSize)
          Read some number of characters from this reader, sending them to the provided Writer.
 int readAll(Writer w)
          Read the full contents of this reader, sending the characters to the provided Writer.
 int readAll(Writer w, char[] buffer)
          Read the full contents of this reader, sending the characters to the provided Writer.
 int readAll(Writer w, int bufferSize)
          Read the full contents of this reader, sending the characters to the provided Writer.
 
Methods inherited from class java.io.Reader
close, mark, markSupported, read, read, read, read, ready, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

protected static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

DirectReader

public DirectReader()
Method Detail

read

public int read(Writer w,
                int chars)
         throws IOException
Read some number of characters from this reader, sending them to the provided Writer. The default implementation invokes read(Writer, int, int) with the minimum of chars and DEFAULT_BUFFER_SIZE. Subclasses that know the size of this reader's remaining contents, or that do not rely on a buffer in read(Writer, int, char[]), should override this method.

Parameters:
w - A writer to be written to
chars - The number of characters to read
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read
Throws:
IOException - If an error occurs during reading or writing

read

public int read(Writer w,
                int chars,
                int bufferSize)
         throws IOException
Read some number of characters from this reader, sending them to the provided Writer. The default implementation invokes read(Writer, int, char[]) with a newly-allocated array of the given size. Subclasses that do not rely on a buffer in read(Writer, int, char[]) should override this method.

Parameters:
w - A writer to be written to
chars - The number of characters to read
bufferSize - The size of buffer to use (if necessary). Smaller values may reduce the amount of memory required; larger values may increase performance of large readers
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If bufferSize <= 0

read

public int read(Writer w,
                int chars,
                char[] buffer)
         throws IOException
Read some number of characters from this reader, sending them to the provided Writer. The given buffer is useful in repeated read invocations to avoid unnecessary memory allocation. The default implementation repeatedly fills the given buffer via a Reader.read(char[], int, int) operation, then writes it via Writer.write(char[], int, int). Subclasses that do not require an external buffer should override this method.

Parameters:
w - A writer to be written to
chars - The number of characters to read
buffer - A buffer used to copy characters from this reader to the writer. Note that this is only used to avoid unnecessary memory allocation. No assumptions are made about the buffer's contents (which may be overwritten), and no assumptions should be made about the contents of the buffer after the method invocation.
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0

readAll

public int readAll(Writer w)
            throws IOException
Read the full contents of this reader, sending the characters to the provided Writer. The method will block until an end-of-file is reached. The default implementation invokes readAll(Writer, int) with DEFAULT_BUFFER_SIZE. Subclasses that know the size of this reader's remaining contents, or that do not rely on a buffer in readAll(Writer, char[]), should override this method.

Parameters:
w - A writer to be written to
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read (or, if the number is too large, Integer.MAX_VALUE)
Throws:
IOException - If an error occurs during reading or writing

readAll

public int readAll(Writer w,
                   int bufferSize)
            throws IOException
Read the full contents of this reader, sending the characters to the provided Writer. The method will block until an end-of-file is reached. The default implementation invokes readAll(Writer, char[]) with a newly-allocated array of the given size. Subclasses that do not rely on a buffer in readAll(Writer, char[]) should override this method.

Parameters:
w - A writer to be written to
bufferSize - The size of buffer to use (if necessary). Smaller values may reduce the amount of memory required; larger values may increase performance of large readers
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read (or, if the number is too large, Integer.MAX_VALUE)
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If bufferSize <= 0

readAll

public int readAll(Writer w,
                   char[] buffer)
            throws IOException
Read the full contents of this reader, sending the characters to the provided Writer. The given buffer is useful in repeated readAll invocations to avoid unnecessary memory allocation. The method will block until an end-of-file is reached. The default implementation repeatedly fills the given buffer via a Reader.read(char[]) operation, then writes it via Writer.write(char[]). Subclasses that do not require an external buffer should override this method.

Parameters:
w - A writer to be written to
buffer - A buffer used to copy characters from this reader to the writer. Note that this is only used to avoid unnecessary memory allocation. No assumptions are made about the buffer's contents (which may be overwritten), and no assumptions should be made about the contents of the buffer after the method invocation.
Returns:
-1 if this reader is at the end of file; otherwise, the number of characters read (or, if the number is too large, Integer.MAX_VALUE)
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0