Class TiedOutputStream

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class TiedOutputStream extends PrintStream
TiedOutputStream is a class that ties the standard output and error stream to a single output stream and writes to both this stream and the original standard output. This is useful for when you need the output to be printed to the console and to another output stream at the same time.

Usage example:

 PrintStream debugPrintStream = new PrintStream(new OutputStream() {
     @Override
     public void write(int b) {
         debugTextArea.append(String.valueOf((char)b));
         debugTextArea.setCaretPosition(debugTextArea.getDocument().getLength());
     }
 });

 TiedOutputStream tiedOutputStream = new TiedOutputStream(debugPrintStream);
 tiedOutputStream.tieOutputStreams();
 debugFrame.addWindowListener(new java.awt.event.WindowAdapter() {
     @Override
     public void windowClosed(java.awt.event.WindowEvent windowEvent) {
         tiedOutputStream.resetOutputStreams();
     }
 });
 
  • Constructor Details

    • TiedOutputStream

      public TiedOutputStream(OutputStream outputStream)
      Creates a new TiedOutputStream.
      Parameters:
      outputStream - the output stream to tie to

      Note:

      This needs to call tieOutputStreams to start and then resetOutputStreams when finished.

      See Also:
  • Method Details

    • getOriginalOutputStream

      public PrintStream getOriginalOutputStream()
      Retrieves the original standard output stream (System.out) before it was tied.
      Returns:
      the original PrintStream for standard output
    • getOriginalErrorStream

      public PrintStream getOriginalErrorStream()
      Retrieves the original standard error stream (System.err) before it was tied.
      Returns:
      the original PrintStream for standard error
    • tieOutputStreams

      public void tieOutputStreams()
      Ties the current output stream to both System.out and System.err.

      Overload method that enables copy by default.

      This method replaces the standard output and error streams with this instance, allowing all output to be redirected to the tied output stream.

      See Also:
    • tieOutputStreams

      public void tieOutputStreams(boolean copy)
      Ties the current output stream to both System.out and System.err.

      This method replaces the standard output and error streams with this instance, allowing all output to be redirected to the tied output stream.

      Parameters:
      copy - true to copy the output to the original output stream, false to disable copying
      See Also:
    • resetOutputStreams

      public void resetOutputStreams()
      Resets System.out and System.err to their original streams.

      This method should be called when the tied output stream is no longer needed, to restore the original behavior of the standard output and error streams.

      See Also:
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables or disables the tied output stream.

      When disabled, the tied output stream will not output anything, and the original output stream will handle all output alone.

      Parameters:
      enabled - true to enable the tied output stream, false to disable it
    • setCopyEnabled

      public void setCopyEnabled(boolean copy)
      Sets whether the tied output stream should copy output to the original output stream.

      Note: Tied output stream needs to be enabled to copy

      Parameters:
      copy - true to enable copying, false to disable it
    • isEnabled

      public boolean isEnabled()
      Checks whether the tied output stream is currently enabled.
      Returns:
      true if the tied output stream is enabled, false otherwise
    • isCopyEnabled

      public boolean isCopyEnabled()
      Checks whether the tied output stream is set to copy output to the original output stream.
      Returns:
      true if copying is enabled, false otherwise
    • print

      public void print(Object obj)
      Overrides:
      print in class PrintStream
    • printf

      public PrintStream printf(String format, Object... args)
      Overrides:
      printf in class PrintStream
    • println

      public void println(Object args)
      Overrides:
      println in class PrintStream
    • write

      public void write(int b)
      Overrides:
      write in class PrintStream
    • write

      public void write(byte[] b, int off, int len)
      Overrides:
      write in class PrintStream