FileDocCategorySizeDatePackage
TeePrintStream.javaAPI DocExample2800Fri Apr 06 22:35:52 BST 2001None

TeePrintStream

public class TeePrintStream extends PrintStream
TeePrintStream tees all PrintStream operations into a file, rather like the UNIX tee(1) command. It is a PrintStream subclass. The expected usage would be something like the following:
...
TeePrintStream ts = new TeePrintStream(System.err, "err.log");
System.setErr(ts);
// ...lots of code that occasionally writes to System.err...
ts.close();
...

I only override Constructors, the write(), check() and close() methods, since any of the print() or println() methods must go through these. Thanks to Svante Karlsson for help formulating this.

author
Ian F. Darwin, ian@darwinsys.com
version
$Id: TeePrintStream.java,v 1.3 2001/04/07 01:35:53 ian Exp $

Fields Summary
protected PrintStream
parent
protected String
fileName
Constructors Summary
public TeePrintStream(PrintStream orig, OutputStream os, boolean flush)
Construct a TeePrintStream given an existing PrintStream, an opened OutputStream, and a boolean to control auto-flush. This is the main constructor, to which others delegate via "this".

		super(os, true);
		fileName = "(opened Stream)";
		parent = orig;
	
public TeePrintStream(PrintStream orig, OutputStream os)
Construct a TeePrintStream given an existing PrintStream and an opened OutputStream.

		this(orig, os, true);
	
public TeePrintStream(PrintStream os, String fn)

		this(os, fn, true);
	
public TeePrintStream(PrintStream orig, String fn, boolean flush)

		this(new FileOutputStream(fn), flush);
	
Methods Summary
public booleancheckError()
Return true if either stream has an error.

		return parent.checkError() || super.checkError();
	
public voidclose()
Close both streams.

		parent.close();
		super.close();
	
public voidflush()
Flush both streams.

		parent.flush();
		super.flush();
	
public static voidmain(java.lang.String[] args)
A simple test case.

		TeePrintStream ts = new TeePrintStream(System.err, "err.log");
		System.setErr(ts);
		System.err.println("An imitation error message");
		ts.close();
	
public voidwrite(int x)
override write(). This is the actual "tee" operation.

		parent.write(x);	// "write once;
		super.write(x);		// write somewhere else."
	
public voidwrite(byte[] x, int o, int l)
override write(). This is the actual "tee" operation.

		parent.write(x, o, l);	// "write once;
		super.write(x, o, l);	// write somewhere else."