SPT Core API

com.sptci.io
Class TeeWriter

java.lang.Object
  extended by java.io.Writer
      extended by com.sptci.io.TeeWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class TeeWriter
extends Writer

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

The following code shows sample usage of this class.

 import com.sptci.io.TeeWriter;
 import java.io.BufferedReader;
 import java.io.FileReader;

 public class test
 {
   public static void main( String[] args )
   {
     try
     {
       String[] files = {"/tmp/one", "/tmp/two", "/tmp/three"};;
       TeeWriter writer = new TeeWriter( files );
       BufferedReader reader = new BufferedReader(
           new FileReader( "/tmp/test.java" ) );
       String line = "";
       while ( ( line = reader.readLine() ) != null )
       {
         writer.write( line );
         writer.write( System.getProperty( "line.separator" ) );
       }
       reader.close();
       writer.flush();
       writer.close();
     }
     catch ( Throwable t )
     {
       t.printStackTrace();
     }
   }
 }
 

Version:
$Id: TeeWriter.java 4553 2008-12-24 10:34:16Z rakesh $
Author:
Rakesh Vidyadharan 2005-09-19
See Also:
FileUtilities.writeToFiles(java.io.InputStream, String[]),

© Copyright 2005, Sans Pareil Technologies, Inc.


Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
TeeWriter(File[] files)
          Create a new instance that writes all the data to the specified files.
TeeWriter(String[] names)
          Create a new instance that writes all the data to the specified files.
TeeWriter(Writer[] writers)
          Create a new instance that writes all the data to the specified writers.
 
Method Summary
 void addWriter(Writer writer)
          Add the specified Writer to the writers.
 void close()
          Close the stream, flushing it first.
 void flush()
          Flush the stream.
 Writer[] getWriters()
          Returns writers.
 void setWriters(Writer[] writers)
          Set writers.
 void write(char[] chars)
          Writes chars.length characters from the specified char array to all the output streams.
 void write(char[] chars, int offset, int length)
          Writes length characters from the specified char array starting at offset offset to the writers.
 void write(int character)
          Write a single character.
 void write(String string)
          Write the string to the writers.
 void write(String string, int offset, int length)
          Write the portion of the string to the writers.
 
Methods inherited from class java.io.Writer
append, append, append
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TeeWriter

public TeeWriter(Writer[] writers)
Create a new instance that writes all the data to the specified writers.

Parameters:
writers - The array of Writers to which the contents are to be written.

TeeWriter

public TeeWriter(String[] names)
          throws IOException
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:
IOException - If errors are encountered while creating the specified files.
See Also:
createWriters( String[] )

TeeWriter

public TeeWriter(File[] files)
          throws IOException
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:
IOException - If errors are encountered while creating the specified files.
See Also:
createWriters( File[] )
Method Detail

addWriter

public final void addWriter(Writer writer)
Add the specified Writer to the writers.

Parameters:
writer - The writer to add.

close

public void close()
           throws IOException
Close the stream, flushing it first. Once a stream has been closed, further write(int) or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect. Iterates through the writers and closes each of them.

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

flush

public void flush()
           throws IOException
Flush the stream. If the stream has saved any characters from the various write(int) methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. Iteratores through the writers and flushes each sequentially.

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

write

public void write(int character)
           throws IOException
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.

Overrides:
write in class Writer
Parameters:
character - The character to be written.
Throws:
IOException - If errors are encountered while writing the byte to any of the configured output streams.

write

public void write(char[] chars)
           throws IOException
Writes chars.length characters from the specified char array to all the output streams.

Overrides:
write in class Writer
Parameters:
chars - 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(char[] chars,
                  int offset,
                  int length)
           throws IOException,
                  NullPointerException,
                  IndexOutOfBoundsException
Writes length characters from the specified char array starting at offset offset to the writers.

Specified by:
write in class Writer
Parameters:
chars - The data to be written.
offset - The start offset in the array.
length - The number of characters to write.
Throws:
IOException - If an I/O error occurs. In particular, an IOException is thrown if the writer is closed.
NullPointerException
IndexOutOfBoundsException

write

public void write(String string)
           throws IOException
Write the string to the writers.

Overrides:
write in class Writer
Parameters:
string - The string to be written.
Throws:
IOException - If an I/O error occurs. In particular, an IOException is thrown if the writer is closed.

write

public void write(String string,
                  int offset,
                  int length)
           throws IOException
Write the portion of the string to the writers.

Overrides:
write in class Writer
Parameters:
string - The string to be written.
offset - The start offset in the string.
length - The number of characters to write.
Throws:
IOException - If an I/O error occurs. In particular, an IOException is thrown if the writer is closed.

getWriters

public final Writer[] getWriters()
Returns writers.

Returns:
Writer[] The value/reference of/to writers.

setWriters

public final void setWriters(Writer[] writers)
Set writers.

Parameters:
writers - The value to set.

SPT Core API