/** constructor which installs a ByteArrayOutputStream to this stream */ public OutputCapturingContext(ThreadLocalPrintStream stream) { this.stream = stream; this.out = new ByteArrayOutputStream(); this.streamToRestore = stream.setThreadLocalPrintStream(out); } /** constructor for a capturing context which is already installed */
public String end() { if (streamToRestore!=null) stream.setThreadLocalPrintStream(streamToRestore); else stream.clearThreadLocalPrintStream(); finished = true; return out.toString(); } public boolean isActive() {
/** creates a capturing context which sees the output to this stream, without interrupting the original target */ public OutputCapturingContext captureTee() { ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream toRestore = setThreadLocalPrintStream(new TeeOutputStream(getDelegate(), out)); return new OutputCapturingContext(this, out, toRestore); }