bb.io
Class PrintStreamStoring

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by java.io.PrintStream
              extended by bb.io.PrintStreamStoring
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class PrintStreamStoring
extends PrintStream

PrintStream subclass that writes all bytes to an internal byte buffer. This output data is stored indefinately until it is retrieved and cleared by calling either getString or getBytes.

Motivation: this class is typically used for debugging/test purposes when output from a PrintStream needs to be captured.

This class is obsolete in the same sense that PrintStream is obsolete: just as PrintStream should be replaced by PrintWriter whenever possible, so should this class be replaced by PrintWriterStoring. Unfortunately, certain APIs (e.g. System.out) have hardwired the use of PrintStream, and so this class must be retained.

This class is multithread safe: every method is synchronized. (Altho unmentioned in the javadocs, if you look at the source code, PrintStream synchronizes on the this reference, so this class continues that pattern.)

Author:
Brent Boyer

Nested Class Summary
static class PrintStreamStoring.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  ByteArrayOutputStream baos
           
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
PrintStreamStoring()
           
 
Method Summary
 byte[] getBytes()
          Returns all the bytes that have been written so far to this instance as a new byte[].
 String getString()
          Returns new String( getBytes() ).
 
Methods inherited from class java.io.PrintStream
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

baos

private final ByteArrayOutputStream baos
Constructor Detail

PrintStreamStoring

public PrintStreamStoring()
Method Detail

getString

public String getString()
Returns new String( getBytes() ).


getBytes

public byte[] getBytes()
Returns all the bytes that have been written so far to this instance as a new byte[]. Side effect: before return, clears the internal byte buffer.