Class PrintStreamWithHistory

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

    public class PrintStreamWithHistory
    extends PrintStream
    An enhanced version of PrintStream that provides for a history recall function and some other features making I/O testing a bit easier to perform. See the documentation for PrintStream for more thorough details on what methods are provided.
    • Constructor Detail

      • PrintStreamWithHistory

        public PrintStreamWithHistory​(OutputStream out)
        Create a new print stream. This stream will not flush automatically.
        Parameters:
        out - The output stream to which values and objects will be printed
        See Also:
        PrintWriter(java.io.OutputStream)
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(OutputStream out,
                                      boolean autoFlush)
        Create a new print stream.
        Parameters:
        out - The output stream to which values and objects will be printed
        autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written
        See Also:
        PrintWriter(java.io.OutputStream, boolean)
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(OutputStream out,
                                      boolean autoFlush,
                                      String encoding)
                               throws UnsupportedEncodingException
        Create a new print stream.
        Parameters:
        out - The output stream to which values and objects will be printed
        autoFlush - A boolean; if true, the output buffer will be flushed whenever a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written
        encoding - The name of a supported character encoding
        Throws:
        UnsupportedEncodingException - If the named encoding is not supported
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(String fileName)
                               throws FileNotFoundException
        Creates a new print stream, without automatic line flushing, with the specified file name. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
        Parameters:
        fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
        Since:
        1.5
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(String fileName,
                                      String csn)
                               throws FileNotFoundException,
                                      UnsupportedEncodingException
        Creates a new print stream, without automatic line flushing, with the specified file name and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.
        Parameters:
        fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(fileName) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
        Since:
        1.5
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(File file)
                               throws FileNotFoundException
        Creates a new print stream, without automatic line flushing, with the specified file. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the default charset for this instance of the Java virtual machine.
        Parameters:
        file - The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is present and checkWrite(file.getPath()) denies write access to the file
        Since:
        1.5
      • PrintStreamWithHistory

        public PrintStreamWithHistory​(File file,
                                      String csn)
                               throws FileNotFoundException,
                                      UnsupportedEncodingException
        Creates a new print stream, without automatic line flushing, with the specified file and charset. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will encode characters using the provided charset.
        Parameters:
        file - The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.
        csn - The name of a supported charset
        Throws:
        FileNotFoundException - If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
        SecurityException - If a security manager is presentand checkWrite(file.getPath()) denies write access to the file
        UnsupportedEncodingException - If the named charset is not supported
        Since:
        1.5
    • Method Detail

      • getHistory

        public String getHistory()
        Retrieve the text history of what has been sent to this PrintStream. This will include all text printed through this object. The clearHistory() method resets the history to be empty, just as when the object was first created. Note that newline characters in the history are always represented by '\n', regardless of what value the system line.separator property has.
        Returns:
        all the text sent to this PrintStream
      • clearHistory

        public void clearHistory()
        Reset this object's history to be empty, just as when the object was first created. You can access the history using getHistory().
      • setNormalizeLineEndings

        public void setNormalizeLineEndings​(boolean value)
        Set whether this object's history will have normalized unix-style line endings, or the raw line endings generated by printing. The default for this is true, so history values will automatically be normalized unless this method is used to change that behavior.
        Parameters:
        value - If true, the retrieved history will have all line endings automatically standardized to unix-style ("\n") line endings; if fase, the retrieved history will be presented without any modifications, using whatever (platform-specific) line endings were printed to this object.
      • setMaxCapacity

        public void setMaxCapacity​(long bytes,
                                   boolean failOnOverflow)
        Set the maximum number of bytes that will be stored as history.
        Parameters:
        bytes - provides the maximum capacity of the history measured in bytes.
        failOnOverflow - If true, this instance will throw an AssertionError when the maximum capacity is exceeded.
      • getNormalizeLineEndings

        public boolean getNormalizeLineEndings()
        Returns whether this object's history has normalized unix-style line endings, or the raw line endings generated by printing. The default behavior is to normalize line endings.
        Returns:
        True if line endings will be normalized in the history.
      • write

        public void write​(int b)
        Write the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

        Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

        Overrides:
        write in class PrintStream
        Parameters:
        b - The byte to be written
        See Also:
        PrintStream.print(char), PrintStream.println(char)
      • write

        public void write​(byte[] buf,
                          int off,
                          int len)
        Write len bytes from the specified byte array starting at offset off to this stream. If automatic flushing is enabled then the flush method will be invoked.

        Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

        Overrides:
        write in class PrintStream
        Parameters:
        buf - A byte array
        off - Offset from which to start taking bytes
        len - Number of bytes to write