Rakesh API

org.rakeshv.io
Class TeeOutputStream

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

public class TeeOutputStream
extends java.io.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 org.rakeshv.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,v 1.2 2005/09/20 23:30:18 rakesh Exp $
Author:
Rakesh Vidyadharan 19th September 2005
See Also:
TeeOutputStream,

© Copyright 2005, Rakesh Vidyadharan


Field Summary
private  java.io.OutputStream[] outputStreams
          The Array of OutputStreams to which the content is to be written.
 
Constructor Summary
TeeOutputStream(java.io.File[] files)
          Create a new instance that writes all the data to the specified files.
TeeOutputStream(java.io.OutputStream[] outputStreams)
          Create a new instance that writes all the data to the specified output streams.
TeeOutputStream(java.lang.String[] names)
          Create a new instance that writes all the data to the specified files.
 
Method Summary
 void addOutputStream(java.io.OutputStream outputStream)
          Add the specified OutputStream to the outputStreams.
 void close()
          Closes this output stream and releases any system resources associated with this stream.
private  void createOutputStreams(java.io.File[] files)
          Initialise the outputStreams with the files specified.
private  void createOutputStreams(java.lang.String[] names)
          Initialise the outputStreams with files representing the names specified.
 void flush()
          Flushes all the configured output streams and forces any buffered output bytes to be written out.
 java.io.OutputStream[] getOutputStreams()
          Returns outputStreams.
 void setOutputStreams(java.io.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
 

Field Detail

outputStreams

private java.io.OutputStream[] outputStreams
The Array of OutputStreams to which the content is to be written.

Constructor Detail

TeeOutputStream

public TeeOutputStream(java.io.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(java.lang.String[] names)
                throws java.io.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:
java.io.FileNotFoundException - If a file corresponding to a name specified could not be created.
See Also:
createOutputStreams( String[] )

TeeOutputStream

public TeeOutputStream(java.io.File[] files)
                throws java.io.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:
java.io.FileNotFoundException - If a file that was specified could could not be accessed.
See Also:
createOutputStreams( File[] )
Method Detail

addOutputStream

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


close

public void close()
           throws java.io.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 java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - If errors are encountered while closing any of the configured output streams.

flush

public void flush()
           throws java.io.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 java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - If errors are encountered while flushing any of the configured output streams.

write

public void write(int b)
           throws java.io.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 java.io.OutputStream
Parameters:
b - The byte to be written.
Throws:
java.io.IOException - If errors are encountered while writing the byte to any of the configured output streams.

write

public void write(byte[] bytes)
           throws java.io.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 java.io.OutputStream
Parameters:
bytes - The data that is to be written.
Throws:
java.io.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 java.io.IOException,
                  java.lang.NullPointerException,
                  java.lang.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 java.io.OutputStream
Parameters:
bytes - The data to be written.
offset - The start offset in the data.
length - The number of bytes to write.
Throws:
java.io.IOException - If an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
java.lang.NullPointerException
java.lang.IndexOutOfBoundsException

createOutputStreams

private void createOutputStreams(java.lang.String[] names)
                          throws java.io.FileNotFoundException,
                                 java.lang.SecurityException
Initialise the outputStreams with files representing the names specified.

Parameters:
names - The array of files to which the output is to be written.
Throws:
java.io.FileNotFoundException - If a file corresponding to a name specified could not be created.
java.lang.SecurityException - If the SecurityManager prevents creating the directory tree for the file(s).
See Also:
FileUtilities.mkdirs(java.io.File)

createOutputStreams

private void createOutputStreams(java.io.File[] files)
                          throws java.io.FileNotFoundException
Initialise the outputStreams with the files specified.

Parameters:
files - The array of files to which the output is to be written.
Throws:
java.io.FileNotFoundException - If a file specified could not be accessed.
java.lang.SecurityException - If the SecurityManager prevents creating the directory tree for the file(s).
See Also:
FileUtilities.mkdirs(java.io.File)

getOutputStreams

public final java.io.OutputStream[] getOutputStreams()
Returns outputStreams.

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

setOutputStreams

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

Parameters:
outputStreams - The value to set.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan