Rakesh API

org.rakeshv.io
Class TeeWriter

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

public class TeeWriter
extends java.io.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 org.rakeshv.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,v 1.2 2005/09/20 23:30:21 rakesh Exp $
Author:
Rakesh Vidyadharan 19th September 2005
See Also:
FileUtilities.writeToFiles( InputStream, String[] ),

© Copyright 2005, Rakesh Vidyadharan


Field Summary
private  java.io.Writer[] writers
          The Array of Writers to which the content is to be written.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
TeeWriter(java.io.File[] files)
          Create a new instance that writes all the data to the specified files.
TeeWriter(java.lang.String[] names)
          Create a new instance that writes all the data to the specified files.
TeeWriter(java.io.Writer[] writers)
          Create a new instance that writes all the data to the specified writers.
 
Method Summary
 void addWriter(java.io.Writer writer)
          Add the specified Writer to the writers.
 void close()
          Close the stream, flushing it first.
private  void createWriters(java.io.File[] files)
          Initialise the writers with the files specified.
private  void createWriters(java.lang.String[] names)
          Initialise the writers with files representing the names specified.
 void flush()
          Flush the stream.
 java.io.Writer[] getWriters()
          Returns writers.
 void setWriters(java.io.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(java.lang.String string)
          Write the string to the writers.
 void write(java.lang.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
 

Field Detail

writers

private java.io.Writer[] writers
The Array of Writers to which the content is to be written.

Constructor Detail

TeeWriter

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

TeeWriter

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

addWriter

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


close

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

flush

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

write

public void write(int character)
           throws java.io.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 java.io.Writer
Parameters:
character - The character 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(char[] chars)
           throws java.io.IOException
Writes chars.length characters from the specified char array to all the output streams.

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

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

write

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

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

write

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

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

createWriters

private void createWriters(java.lang.String[] names)
                    throws java.io.IOException
Initialise the writers with files representing the names specified.

Parameters:
names - The array of files to which the output is to be written.
Throws:
java.io.IOException - If a file specified could not be created.
See Also:
FileUtilities.mkdirs(java.io.File)

createWriters

private void createWriters(java.io.File[] files)
                    throws java.io.IOException
Initialise the writers with the files specified.

Parameters:
files - The array of files to which the output is to be written.
Throws:
java.io.IOException - If a file specified could not be accessed.
See Also:
FileUtilities.mkdirs(java.io.File)

getWriters

public final java.io.Writer[] getWriters()
Returns writers.

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

setWriters

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

Parameters:
writers - The value to set.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan