bb.io
Class PrintStreamStoring
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
java.io.PrintStream
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. |
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.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
baos
private final ByteArrayOutputStream baos
PrintStreamStoring
public PrintStreamStoring()
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.