Package adams.core.io
Class Tee
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.PrintStream
-
- adams.core.io.Tee
-
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class Tee extends PrintStream
This class pipelines print/println's to several PrintStreams. Useful for redirecting System.out and System.err to files etc.
E.g., for redirecting stderr/stdout to files with timestamps and:
import java.io.*; import adams.core.io.Tee; ... // stdout Tee teeOut = new Tee(System.out); teeOut.add(new PrintStream(new FileOutputStream("out.txt")), true); System.setOut(teeOut); // stderr Tee teeErr = new Tee(System.err); teeErr.add(new PrintStream(new FileOutputStream("err.txt")), true); System.setOut(teeErr); ...- Version:
- $Revision$
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
-
-
Field Summary
Fields Modifier and Type Field Description protected PrintStreamm_Defaultthe default printstream.protected List<String>m_Prefixeswhether to add a prefix or not.protected List<PrintStream>m_Streamsthe different PrintStreams.protected List<Boolean>m_Timestampswhether to add timestamps or not.-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description Tee()initializes the object, with a default printstream.Tee(PrintStream def)initializes the object with the given default printstream, e.g., System.out.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(PrintStream p)adds the given PrintStream to the list of streams, with NO timestamp and NO prefix.voidadd(PrintStream p, boolean timestamp)adds the given PrintStream to the list of streams, with NO prefix.voidadd(PrintStream p, boolean timestamp, String prefix)adds the given PrintStream to the list of streams.voidclear()removes all streams and places the default printstream, if any, again in the list.booleancontains(PrintStream p)checks whether the given PrintStream is already in the list.voidflush()flushes all the printstreams.PrintStreamget(int index)returns the specified PrintStream from the list.PrintStreamgetDefault()returns the default printstrean, can be NULL.voidprint(boolean x)prints the given boolean to the streams.voidprint(int x)prints the given int to the streams.voidprint(Object x)prints the given object to the streams.voidprint(String x)prints the given string to the streams.voidprintln()prints a new line to the streams.voidprintln(boolean x)prints the given boolean to the streams.voidprintln(int x)prints the given int to the streams.voidprintln(Object x)prints the given object to the streams (for Throwables we print the stack trace).voidprintln(String x)prints the given string to the streams.PrintStreamremove(int index)removes the given PrintStream from the list.PrintStreamremove(PrintStream p)removes the given PrintStream from the list.intsize()returns the number of streams currently in the list.StringtoString()returns only the classname and the number of streams.-
Methods inherited from class java.io.PrintStream
append, append, append, checkError, clearError, close, format, format, print, print, print, print, print, printf, printf, println, println, println, println, println, setError, write, write
-
Methods inherited from class java.io.FilterOutputStream
write
-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Field Detail
-
m_Streams
protected List<PrintStream> m_Streams
the different PrintStreams.
-
m_Default
protected PrintStream m_Default
the default printstream.
-
-
Constructor Detail
-
Tee
public Tee()
initializes the object, with a default printstream.
-
Tee
public Tee(PrintStream def)
initializes the object with the given default printstream, e.g., System.out.- Parameters:
def- the default printstream, remains also after calling clear()
-
-
Method Detail
-
clear
public void clear()
removes all streams and places the default printstream, if any, again in the list.- See Also:
getDefault()
-
getDefault
public PrintStream getDefault()
returns the default printstrean, can be NULL.- Returns:
- the default printstream
- See Also:
m_Default
-
add
public void add(PrintStream p)
adds the given PrintStream to the list of streams, with NO timestamp and NO prefix.- Parameters:
p- the printstream to add
-
add
public void add(PrintStream p, boolean timestamp)
adds the given PrintStream to the list of streams, with NO prefix.- Parameters:
p- the printstream to addtimestamp- whether to use timestamps or not
-
add
public void add(PrintStream p, boolean timestamp, String prefix)
adds the given PrintStream to the list of streams.- Parameters:
p- the printstream to addtimestamp- whether to use timestamps or notprefix- the prefix to use
-
get
public PrintStream get(int index)
returns the specified PrintStream from the list.- Parameters:
index- the index of the PrintStream to return- Returns:
- the specified PrintStream, or null if invalid index
-
remove
public PrintStream remove(PrintStream p)
removes the given PrintStream from the list.- Parameters:
p- the PrintStream to remove- Returns:
- returns the removed PrintStream if it could be removed, null otherwise
-
remove
public PrintStream remove(int index)
removes the given PrintStream from the list.- Parameters:
index- the index of the PrintStream to remove- Returns:
- returns the removed PrintStream if it could be removed, null otherwise
-
contains
public boolean contains(PrintStream p)
checks whether the given PrintStream is already in the list.- Parameters:
p- the PrintStream to look for- Returns:
- true if the PrintStream is in the list
-
size
public int size()
returns the number of streams currently in the list.- Returns:
- the number of streams in the list
-
flush
public void flush()
flushes all the printstreams.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classPrintStream
-
print
public void print(int x)
prints the given int to the streams.- Overrides:
printin classPrintStream- Parameters:
x- the object to print
-
print
public void print(boolean x)
prints the given boolean to the streams.- Overrides:
printin classPrintStream- Parameters:
x- the object to print
-
print
public void print(String x)
prints the given string to the streams.- Overrides:
printin classPrintStream- Parameters:
x- the object to print
-
print
public void print(Object x)
prints the given object to the streams.- Overrides:
printin classPrintStream- Parameters:
x- the object to print
-
println
public void println()
prints a new line to the streams.- Overrides:
printlnin classPrintStream
-
println
public void println(int x)
prints the given int to the streams.- Overrides:
printlnin classPrintStream- Parameters:
x- the object to print
-
println
public void println(boolean x)
prints the given boolean to the streams.- Overrides:
printlnin classPrintStream- Parameters:
x- the object to print
-
println
public void println(String x)
prints the given string to the streams.- Overrides:
printlnin classPrintStream- Parameters:
x- the object to print
-
println
public void println(Object x)
prints the given object to the streams (for Throwables we print the stack trace).- Overrides:
printlnin classPrintStream- Parameters:
x- the object to print
-
-