edu.rice.cs.plt.io
Class DirectOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by edu.rice.cs.plt.io.DirectOutputStream
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
ChecksumOutputStream, MessageDigestOutputStream, OutputStreamSplitter, VoidOutputStream, WrappedDirectOutputStream

public abstract class DirectOutputStream
extends OutputStream

An OutputStream that supports writing directly from an InputStream. This class provides default implementations defined in terms of InputStream and OutputStream methods. Subclasses can override (at least) writeAll(InputStream, byte[]) and write(InputStream, int, byte[]) to provide better implementations (by, for example, not invoking OutputStream.write(byte[])).

Also guarantees that, consistent with the Writer class, all write operations are by default defined in terms of the (declared abstract) write(byte[], int, int) method.

See Also:
DirectWriter, DirectInputStream, DirectReader

Field Summary
protected static int DEFAULT_BUFFER_SIZE
           
 
Constructor Summary
DirectOutputStream()
           
 
Method Summary
 void write(byte[] bbuf)
          Delegate to the more general write(byte[], int, int) method
abstract  void write(byte[] bbuf, int offset, int bytes)
          Subclasses are, at a minimum, required to implement this method.
 int write(InputStream in, int bytes)
          Write some number of bytes, using the provided InputStream as input.
 int write(InputStream in, int bytes, byte[] buffer)
          Write some number of bytes, using the provided InputStream as input.
 int write(InputStream in, int bytes, int bufferSize)
          Write some number of bytes, using the provided InputStream as input.
 void write(int b)
          Delegate to the more general write(byte[], int, int) method
 int writeAll(InputStream in)
          Write the full contents of the given InputStream to this stream.
 int writeAll(InputStream in, byte[] buffer)
          Write the full contents of the given InputStream to this stream.
 int writeAll(InputStream in, int bufferSize)
          Write the full contents of the given InputStream to this stream.
 
Methods inherited from class java.io.OutputStream
close, flush
 
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

DirectOutputStream

public DirectOutputStream()
Method Detail

write

public void write(int b)
           throws IOException
Delegate to the more general write(byte[], int, int) method

Specified by:
write in class OutputStream
Throws:
IOException

write

public void write(byte[] bbuf)
           throws IOException
Delegate to the more general write(byte[], int, int) method

Overrides:
write in class OutputStream
Throws:
IOException

write

public abstract void write(byte[] bbuf,
                           int offset,
                           int bytes)
                    throws IOException
Subclasses are, at a minimum, required to implement this method.

Overrides:
write in class OutputStream
Throws:
IOException

write

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

Parameters:
in - A stream to be read from
bytes - The number of bytes to write
Returns:
-1 if the input stream is at the end of file; otherwise, the number of characters written
Throws:
IOException - If an error occurs during reading or writing

write

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

Parameters:
in - A stream to be read from
bytes - The number of bytes 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 input stream is at the end of file; otherwise, the number of characters written
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If bufferSize <= 0

write

public int write(InputStream in,
                 int bytes,
                 byte[] buffer)
          throws IOException
Write some number of bytes, using the provided InputStream as input. Fewer bytes may be written if the input stream 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 InputStream.read(byte[], int, int) operation, then writes it via OutputStream.write(byte[], int, int). Subclasses that do not require an external buffer should override this method.

Parameters:
in - A stream to be read from
bytes - The number of bytes to write
buffer - A buffer used to copy bytes from the input stream to this stream. 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 input stream is at the end of file; otherwise, the number of characters written
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If buffer has size 0

writeAll

public int writeAll(InputStream in)
             throws IOException
Write the full contents of the given InputStream to this stream. The method will block until an end-of-file is reached. The default implementation invokes writeAll(InputStream, int) with DEFAULT_BUFFER_SIZE. Subclasses that know the size of this stream's remaining contents, or that do not rely on a buffer in writeAll(InputStream, byte[]), should override this method.

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

writeAll

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

Parameters:
in - A stream 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 input stream is at the end of file; otherwise, the number of characters written (or, if the number is too large, Integer.MAX_VALUE)
Throws:
IOException - If an error occurs during reading or writing
IllegalArgumentException - If bufferSize <= 0

writeAll

public int writeAll(InputStream in,
                    byte[] buffer)
             throws IOException
Write the full contents of the given InputStream to this stream. 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 InputStream.read(byte[]) operation, then writes it via OutputStream.write(byte[]). Subclasses that do not require an external buffer should override this method.

Parameters:
in - A stream to be read from
buffer - A buffer used to copy bytes from the input stream to this stream. 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 input stream is at the end of file; otherwise, the number of characters written (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