SPT Core API

com.sptci.io
Class TeeOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.sptci.io.TeeOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class TeeOutputStream
extends OutputStream

A implementation of the UNIX tee command. This class enables writing the contents of an InputStream to multiple OutputStreams.

The following code shows sample usage of this class.

 import com.sptci.io.TeeOutputStream;
 import java.io.BufferedInputStream;
 import java.io.FileInputStream;

 public class test
 {
   public static void main( String[] args )
   {
     String[] names = {"/tmp/one.m4a", "/tmp/two.m4a"};

     try
     {
       InputStream inputStream = new BufferedInputStream(
           new FileInputStream( "/tmp/song.m4a" ) );
       TeeOutputStream tee = new TeeOutputStream( names );

       // Read data from your stream
       byte[] buffer = new byte[ 2048 ];
       int length = 0;
       while ( ( length = inputStream.read( buffer, 0, buffer.length ) ) != -1 )
       {
         tee.write( buffer, 0, length );
       }
     }
     catch ( Throwable t )
     {
       t.printStackTrace();
     }
   }
 }
 

Version:
$Id: TeeOutputStream.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2005-09-19
See Also:
TeeOutputStream,

© Copyright 2005, Sans Pareil Technologies, Inc.


Constructor Summary
TeeOutputStream(File[] files)
          Create a new instance that writes all the data to the specified files.
TeeOutputStream(OutputStream[] outputStreams)
          Create a new instance that writes all the data to the specified output streams.
TeeOutputStream(String[] names)
          Create a new instance that writes all the data to the specified files.
 
Method Summary
 void addOutputStream(OutputStream outputStream)
          Add the specified OutputStream to the outputStreams.
 void close()
          Closes this output stream and releases any system resources associated with this stream.
 void flush()
          Flushes all the configured output streams and forces any buffered output bytes to be written out.
 OutputStream[] getOutputStreams()
          Returns outputStreams.
 void setOutputStreams(OutputStream[] outputStreams)
          Set outputStreams.
 void write(byte[] bytes)
          Writes b.length bytes from the specified byte array to all the output streams.
 void write(byte[] bytes, int offset, int length)
          Writes length bytes from the specified byte array starting at offset offset to the output streams.
 void write(int b)
          Writes the specified byte to all the output streams.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TeeOutputStream

public TeeOutputStream(OutputStream[] outputStreams)
Create a new instance that writes all the data to the specified output streams.

Parameters:
outputStreams - The array of OutputStreams to which the contents are to be written.

TeeOutputStream

public TeeOutputStream(String[] names)
                throws FileNotFoundException
Create a new instance that writes all the data to the specified files. Calling classes must call the flush() method if this constructor is used.

Note: If the file(s) specified point to paths that do not yet exist, the required directory structure will be created.

Parameters:
names - The array of fully qualified filenames to which the data is to be written.
Throws:
FileNotFoundException - If a file corresponding to a name specified could not be created.
See Also:
createOutputStreams( String[] )

TeeOutputStream

public TeeOutputStream(File[] files)
                throws FileNotFoundException
Create a new instance that writes all the data to the specified files. Calling classes must call the flush() method if this constructor is used.

Note: If the file(s) specified point to paths that do not yet exist, the required directory structure will be created.

Parameters:
files - The array of files to which the data is to be written.
Throws:
FileNotFoundException - If a file that was specified could could not be accessed.
See Also:
createOutputStreams( File[] )
Method Detail

addOutputStream

public final void addOutputStream(OutputStream outputStream)
Add the specified OutputStream to the outputStreams.

Parameters:
outputStream - The output stream to be added.

close

public void close()
           throws IOException
Closes this output stream and releases any system resources associated with this stream. Iterates through the outputStreams and closes each of them.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
Throws:
IOException - If errors are encountered while closing any of the configured output streams.

flush

public void flush()
           throws IOException
Flushes all the configured output streams and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

Specified by:
flush in interface Flushable
Overrides:
flush in class OutputStream
Throws:
IOException - If errors are encountered while flushing any of the configured output streams.

write

public void write(int b)
           throws IOException
Writes the specified byte to all the output streams. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Specified by:
write in class OutputStream
Parameters:
b - The byte to be written.
Throws:
IOException - If errors are encountered while writing the byte to any of the configured output streams.

write

public void write(byte[] bytes)
           throws IOException
Writes b.length bytes from the specified byte array to all the output streams. The general contract for write(b) is that it should have exactly the same effect as the call write(byte[], int, int).

Overrides:
write in class OutputStream
Parameters:
bytes - The data that is to be written.
Throws:
IOException - If errors are encountered while writing the data to any of the configured output streams.

write

public void write(byte[] bytes,
                  int offset,
                  int length)
           throws IOException,
                  NullPointerException,
                  IndexOutOfBoundsException
Writes length bytes from the specified byte array starting at offset offset to the output streams. The general contract for write( bytes, offset, length) is that some of the bytes in the array bytes are written to the output stream in order; element bytes[offset] is the first byte written and b[offset+length-1] is the last byte written by this operation.

The write method of OutputStream calls the write(int) method on each of the bytes to be written out.

If bytes is null, a NullPointerException is thrown.

If offset is negative, or length is negative, or offset+length is greater than the length of the array bytes, then an IndexOutOfBoundsException is thrown.

Overrides:
write in class OutputStream
Parameters:
bytes - The data to be written.
offset - The start offset in the data.
length - The number of bytes to write.
Throws:
IOException - If an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
NullPointerException
IndexOutOfBoundsException

getOutputStreams

public final OutputStream[] getOutputStreams()
Returns outputStreams.

Returns:
OutputStream[] The value/reference of/to outputStreams.

setOutputStreams

public final void setOutputStreams(OutputStream[] outputStreams)
Set outputStreams.

Parameters:
outputStreams - The value to set.

SPT Core API