Class CapturedPrintStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public final class CapturedPrintStream
    extends java.io.PrintStream
    Extends PrintStream to capture the output to an in-memory buffer. The captured stream is available by calling getReader(). UTF-8 is used for encoding and decoding the stream content.
    See Also:
    getInstance()
    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CapturedPrintStream()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static CapturedPrintStream getInstance()
      Instantiates a new CapturedPrintStream.
      java.io.BufferedReader getReader()
      Gets a BufferedReader over the bytes written to this PrintStream.
      void reset()
      Discards the captured output.
      byte[] toByteArray()
      Gets a copy of the content of this stream as a byte array.
      • 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.io.OutputStream

        nullOutputStream
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CapturedPrintStream

        private CapturedPrintStream()
                             throws java.io.UnsupportedEncodingException
        Throws:
        java.io.UnsupportedEncodingException
    • Method Detail

      • getInstance

        public static CapturedPrintStream getInstance()
        Instantiates a new CapturedPrintStream.
        Returns:
        a new CapturedPrintStream
      • getReader

        public java.io.BufferedReader getReader()
        Gets a BufferedReader over the bytes written to this PrintStream. The content of this stream is not altered by this method.
        Returns:
        a new BufferedReader over the bytes written to this stream
        See Also:
        reset()
      • reset

        public void reset()
        Discards the captured output.
      • toByteArray

        public byte[] toByteArray()
        Gets a copy of the content of this stream as a byte array. The content of this stream is not altered by this method.
        Returns:
        a byte array containing a copy of the captured data
        See Also:
        reset()