edu.rice.cs.plt.io
Class DirectWriter

java.lang.Object
  extended by java.io.Writer
      extended by edu.rice.cs.plt.io.DirectWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable
Direct Known Subclasses:
VoidWriter, WrappedDirectWriter, WriterSplitter

public abstract class DirectWriter
extends java.io.Writer

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

See Also:
DirectOutputStream, DirectReader, DirectInputStream

Field Summary
protected static int DEFAULT_BUFFER_SIZE
           
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
DirectWriter()
           
 
Method Summary
 int write(java.io.Reader r, int chars)
          Write some number of characters, using the provided Reader as input.
 int write(java.io.Reader r, int chars, char[] buffer)
          Write some number of characters, using the provided Reader as input.
 int write(java.io.Reader r, int chars, int bufferSize)
          Write some number of characters, using the provided Reader as input.
 int writeAll(java.io.Reader r)
          Write the full contents of the given Reader to this writer.
 int writeAll(java.io.Reader r, char[] buffer)
          Write the full contents of the given Reader to this writer.
 int writeAll(java.io.Reader r, int bufferSize)
          Write the full contents of the given Reader to this writer.
 
Methods inherited from class java.io.Writer
append, append, append, close, flush, write, write, write, write, write
 
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

DirectWriter

public DirectWriter()
Method Detail

write

public int write(java.io.Reader r,
                 int chars)
          throws java.io.IOException
Write some number of characters, using the provided Reader as input. Fewer characters may be written if the reader has fewer available. The default implementation invokes write(Reader, int, int) with the minimum of chars and DEFAULT_BUFFER_SIZE. Subclasses that do not rely on a buffer in write(Reader, int, char[]) should override this method.

Parameters:
r - A reader to be read from
chars - The number of characters to write
Returns:
-1 if the reader is at the end of file; otherwise, the number of characters written
Throws:
java.io.IOException - If an error occurs during reading or writing

write

public int write(java.io.Reader r,
                 int chars,
                 int bufferSize)
          throws java.io.IOException
Write some number of characters, using the provided Reader as input. Fewer characters may be written if the reader has fewer available. The default implementation invokes write(Reader, int, char[]) with a newly-allocated array of the given size. Subclasses that do not rely on a buffer in write(Reader, int, char[]) should override this method.

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

write

public int write(java.io.Reader r,
                 int chars,
                 char[] buffer)
          throws java.io.IOException
Write some number of characters, using the provided Reader as input. Fewer characters may be written if the reader has fewer available. The given buffer is useful in repeated write 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:
r - A reader to be read from
chars - The number of characters to write
buffer - A buffer used to copy characters from the reader to this 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 the reader is at the end of file; otherwise, the number of characters written
Throws:
java.io.IOException - If an error occurs during reading or writing
java.lang.IllegalArgumentException - If buffer has size 0

writeAll

public int writeAll(java.io.Reader r)
             throws java.io.IOException
Write the full contents of the given Reader to this writer. The method will block until an end-of-file is reached. The default implementation invokes writeAll(Reader, 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 writeAll(Reader, char[]), should override this method.

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

writeAll

public int writeAll(java.io.Reader r,
                    int bufferSize)
             throws java.io.IOException
Write the full contents of the given Reader to this writer. The method will block until an end-of-file is reached. The default implementation invokes writeAll(Reader, char[]) with a newly-allocated array of the given size. Subclasses that do not rely on a buffer in writeAll(Reader, char[]) should override this method.

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

writeAll

public int writeAll(java.io.Reader r,
                    char[] buffer)
             throws java.io.IOException
Write the full contents of the given Reader to this writer. The given buffer is useful in repeated writeAll 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:
r - A reader to be read from
buffer - A buffer used to copy characters from the reader to this 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 the reader is at the end of file; otherwise, the number of characters written (or, if the number is too large, Integer.MAX_VALUE)
Throws:
java.io.IOException - If an error occurs during reading or writing
java.lang.IllegalArgumentException - If buffer has size 0