Class Tee
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.PrintStream
-
- weka.core.Tee
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.Appendable,java.lang.AutoCloseable,RevisionHandler
public class Tee extends java.io.PrintStream implements RevisionHandler
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 weka.core.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: 15287 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(java.io.PrintStream p)adds the given PrintStream to the list of streams, with NO timestamp and NO prefix.voidadd(java.io.PrintStream p, boolean timestamp)adds the given PrintStream to the list of streams, with NO prefix.voidadd(java.io.PrintStream p, boolean timestamp, java.lang.String prefix)adds the given PrintStream to the list of streams.java.io.PrintStreamappend(char c)Appends the specified character to this output stream.java.io.PrintStreamappend(java.lang.CharSequence csq)Appends the specified character sequence to this output stream.java.io.PrintStreamappend(java.lang.CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this output stream.voidclear()removes all streams and places the default printstream, if any, again in the list.booleancontains(java.io.PrintStream p)checks whether the given PrintStream is already in the list.voidflush()flushes all the printstreams.java.io.PrintStreamget(int index)returns the specified PrintStream from the list.java.io.PrintStreamgetDefault()returns the default printstrean, can be NULL.java.lang.StringgetRevision()Returns the revision string.voidprint(boolean x)prints the given boolean to the streams.voidprint(char x)prints the given char to the streams.voidprint(char[] x)prints the given char array to the streams.voidprint(double x)prints the given double to the streams.voidprint(float x)prints the given float to the streams.voidprint(int x)prints the given int to the streams.voidprint(long x)prints the given long to the streams.voidprint(java.lang.Object x)prints the given object to the streams.voidprint(java.lang.String x)prints the given string to the streams.java.io.PrintStreamprintf(java.lang.String format, java.lang.Object... args)A convenience method to write a formatted string to this output stream using the specified format string and arguments.java.io.PrintStreamprintf(java.util.Locale l, java.lang.String format, java.lang.Object... args)A convenience method to write a formatted string to this output stream using the specified format string and arguments.voidprintln()prints a new line to the streams.voidprintln(boolean x)prints the given boolean to the streams.voidprintln(char x)prints the given char to the streams.voidprintln(char[] x)prints the given char array to the streams.voidprintln(double x)prints the given double to the streams.voidprintln(float x)prints the given float to the streams.voidprintln(int x)prints the given int to the streams.voidprintln(long x)prints the given long to the streams.voidprintln(java.lang.Object x)prints the given object to the streams (for Throwables we print the stack trace).voidprintln(java.lang.String x)prints the given string to the streams.java.io.PrintStreamremove(int index)removes the given PrintStream from the list.java.io.PrintStreamremove(java.io.PrintStream p)removes the given PrintStream from the list.intsize()returns the number of streams currently in the list.java.lang.StringtoString()returns only the classname and the number of streams.voidwrite(byte[] b)Writesb.lengthbytes to this output stream.voidwrite(byte[] buf, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this stream.voidwrite(int b)Writes the specified byte to this stream.
-
-
-
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 java.io.PrintStream getDefault()
returns the default printstrean, can be NULL.- Returns:
- the default printstream
- See Also:
m_Default
-
add
public void add(java.io.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(java.io.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(java.io.PrintStream p, boolean timestamp, java.lang.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 java.io.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 java.io.PrintStream remove(java.io.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 java.io.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(java.io.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 interfacejava.io.Flushable- Overrides:
flushin classjava.io.PrintStream
-
print
public void print(int x)
prints the given int to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(long x)
prints the given long to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(float x)
prints the given float to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(double x)
prints the given double to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(boolean x)
prints the given boolean to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(char x)
prints the given char to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(char[] x)
prints the given char array to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(java.lang.String x)
prints the given string to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
print
public void print(java.lang.Object x)
prints the given object to the streams.- Overrides:
printin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println()
prints a new line to the streams.- Overrides:
printlnin classjava.io.PrintStream
-
println
public void println(int x)
prints the given int to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(long x)
prints the given long to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(float x)
prints the given float to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(double x)
prints the given double to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(boolean x)
prints the given boolean to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(char x)
prints the given char to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(char[] x)
prints the given char array to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(java.lang.String x)
prints the given string to the streams.- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
println
public void println(java.lang.Object x)
prints the given object to the streams (for Throwables we print the stack trace).- Overrides:
printlnin classjava.io.PrintStream- Parameters:
x- the object to print
-
write
public void write(byte[] buf, int off, int len)Writeslenbytes from the specified byte array starting at offsetoffto this stream. If automatic flushing is enabled then theflushmethod will be invoked.Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the
print(char)orprintln(char)methods.- Overrides:
writein classjava.io.PrintStream- Parameters:
buf- A byte arrayoff- Offset from which to start taking byteslen- Number of bytes to write
-
write
public void write(int b)
Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then theflushmethod will be invoked.Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the
print(char)orprintln(char)methods.- Overrides:
writein classjava.io.PrintStream- Parameters:
b- The byte to be written- See Also:
print(char),println(char)
-
write
public void write(byte[] b) throws java.io.IOExceptionWritesb.lengthbytes to this output stream.The
writemethod ofFilterOutputStreamcalls itswritemethod of three arguments with the argumentsb,0, andb.length.Note that this method does not call the one-argument
writemethod of its underlying output stream with the single argumentb.- Overrides:
writein classjava.io.FilterOutputStream- Parameters:
b- the data to be written.- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
FilterOutputStream.write(byte[], int, int)
-
printf
public java.io.PrintStream printf(java.lang.String format, java.lang.Object... args)A convenience method to write a formatted string to this output stream using the specified format string and arguments.An invocation of this method of the form
out.printf(format, args)behaves in exactly the same way as the invocationout.format(format, args)- Overrides:
printfin classjava.io.PrintStream- Parameters:
format- A format string as described in Format string syntaxargs- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on anullargument depends on the conversion.- Returns:
- This output stream
- Throws:
java.util.IllegalFormatException- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.java.lang.NullPointerException- If theformatisnull- Since:
- 1.5
-
printf
public java.io.PrintStream printf(java.util.Locale l, java.lang.String format, java.lang.Object... args)A convenience method to write a formatted string to this output stream using the specified format string and arguments.An invocation of this method of the form
out.printf(l, format, args)behaves in exactly the same way as the invocationout.format(l, format, args)- Overrides:
printfin classjava.io.PrintStream- Parameters:
l- The locale to apply during formatting. Iflisnullthen no localization is applied.format- A format string as described in Format string syntaxargs- Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behaviour on anullargument depends on the conversion.- Returns:
- This output stream
- Throws:
java.util.IllegalFormatException- If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.java.lang.NullPointerException- If theformatisnull- Since:
- 1.5
-
append
public java.io.PrintStream append(char c)
Appends the specified character to this output stream.An invocation of this method of the form
out.append(c)behaves in exactly the same way as the invocationout.print(c)- Specified by:
appendin interfacejava.lang.Appendable- Overrides:
appendin classjava.io.PrintStream- Parameters:
c- The 16-bit character to append- Returns:
- This output stream
- Since:
- 1.5
-
append
public java.io.PrintStream append(java.lang.CharSequence csq)
Appends the specified character sequence to this output stream.An invocation of this method of the form
out.append(csq)behaves in exactly the same way as the invocationout.print(csq.toString())Depending on the specification of
toStringfor the character sequencecsq, the entire sequence may not be appended. For instance, invoking thentoStringmethod of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.- Specified by:
appendin interfacejava.lang.Appendable- Overrides:
appendin classjava.io.PrintStream- Parameters:
csq- The character sequence to append. Ifcsqisnull, then the four characters"null"are appended to this output stream.- Returns:
- This output stream
- Since:
- 1.5
-
append
public java.io.PrintStream append(java.lang.CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence to this output stream.An invocation of this method of the form
out.append(csq, start, end)whencsqis notnull, behaves in exactly the same way as the invocationout.print(csq.subSequence(start, end).toString())- Specified by:
appendin interfacejava.lang.Appendable- Overrides:
appendin classjava.io.PrintStream- Parameters:
csq- The character sequence from which a subsequence will be appended. Ifcsqisnull, then characters will be appended as ifcsqcontained the four characters"null".start- The index of the first character in the subsequenceend- The index of the character following the last character in the subsequence- Returns:
- This output stream
- Throws:
java.lang.IndexOutOfBoundsException- Ifstartorendare negative,startis greater thanend, orendis greater thancsq.length()- Since:
- 1.5
-
toString
public java.lang.String toString()
returns only the classname and the number of streams.- Overrides:
toStringin classjava.lang.Object- Returns:
- only the classname and the number of streams
-
getRevision
public java.lang.String getRevision()
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-
-