Class MirrorPrintStream

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

    public class MirrorPrintStream
    extends java.io.PrintStream
    MirrorPrintStream is a PrintStream that mirrors all print/write operations into a file. Only the write(), check() and close() methods need overriding given that any of the print() and println() methods must go through these.
    Since:
    1.6
    Author:
    Horacio Hoyos Rodriguez
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.lang.String fileName
      The file name.
      protected java.io.PrintStream fileStream
      The file stream.
      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      MirrorPrintStream​(java.io.OutputStream os)
      Instantiates a MirrorPrintStream with autoflush.
      MirrorPrintStream​(java.io.OutputStream os, boolean flush)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean checkError()
      Return true if either stream has an error.
      protected void clearError()
      Clears the internal error state of this stream.
      void close()  
      void disableMirror()
      Disable the mirror operation.
      void flush()  
      void mirrorToFile​(java.lang.String fileName)
      Enable mirroring to the given file.
      void mirrorToFile​(java.lang.String fileName, boolean append)
      Enable mirroring to the given file.
      protected void setError()
      Sets the error state of the stream to true.
      void write​(byte[] x, int o, int l)  
      void write​(int x)  
      • Methods inherited from class java.io.PrintStream

        append, append, append, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println
      • 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
    • Field Detail

      • fileStream

        protected java.io.PrintStream fileStream
        The file stream.
      • fileName

        protected java.lang.String fileName
        The file name.
    • Constructor Detail

      • MirrorPrintStream

        public MirrorPrintStream​(java.io.OutputStream os,
                                 boolean flush)
      • MirrorPrintStream

        public MirrorPrintStream​(java.io.OutputStream os)
        Instantiates a MirrorPrintStream with autoflush.
        Parameters:
        os - the os
    • Method Detail

      • checkError

        public boolean checkError()
        Return true if either stream has an error.
        Overrides:
        checkError in class java.io.PrintStream
      • write

        public void write​(int x)
        Overrides:
        write in class java.io.PrintStream
      • write

        public void write​(byte[] x,
                          int o,
                          int l)
        Overrides:
        write in class java.io.PrintStream
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.PrintStream
      • flush

        public void flush()
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.PrintStream
      • mirrorToFile

        public void mirrorToFile​(java.lang.String fileName)
        Enable mirroring to the given file. If there is an error the MirrorPrintStream will be flagged as having an error. By default bytes will be written to the beginning of the file
        Parameters:
        fileName - The name of the file to direct output.
        See Also:
        mirrorToFile(String, boolean)
      • mirrorToFile

        public void mirrorToFile​(java.lang.String fileName,
                                 boolean append)
        Enable mirroring to the given file. If there is an error the MirrorPrintStream will be flagged as having an error.
        Parameters:
        fileName - The name of the file to direct output.
        append - if true, then bytes will be written to the end of the file rather than the beginning
      • disableMirror

        public void disableMirror()
        Disable the mirror operation.
      • setError

        protected void setError()
        Sets the error state of the stream to true.

        This method will cause subsequent invocations of checkError() to return true until clearError() is invoked.

        Overrides:
        setError in class java.io.PrintStream
      • clearError

        protected void clearError()
        Clears the internal error state of this stream.

        This method will cause subsequent invocations of checkError() to return false until another write operation fails and invokes setError().

        Overrides:
        clearError in class java.io.PrintStream