Class ThreadBoundOutputStream

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

    public class ThreadBoundOutputStream
    extends java.io.FilterOutputStream
    ThreadBoundOutputStream allows a different OutputStream to be used for the current Thread and any child threads when necessary, otherwise the default OutputStream is used. This is useful for replacing System.err or System.out to capture output in a multi-threaded system, allowing using different sink OutputStreams for each Thread (and their sub-threads).
    Convenience methods bindSystemOut() and bindSystemErr() allow easy replacement of the System.out and System.err printstreams, and access to the ThreadBoundOutputStream at any time after doing so.
    Setting the correct OutputStream should be done with installThreadStream(java.io.OutputStream), and removed with removeThreadStream().
    Example code which replaces System.out and sets a two different FileOutputStreams as the sinks for System.out for multpile threads:
     public static void main(String[] args) throws FileNotFoundException {
         //bind to System.out, or retrieve already bound instance
         final ThreadBoundOutputStream newout = ThreadBoundOutputStream.bindSystemOut();
    
         //start another thread which redirect System.out to a file
         Thread t1 = new Thread(new Runnable(){
             public void run() {
                 try {
                     newout.installThreadStream(new FileOutputStream("thread1.output"));
                     method1("thread1"); //writes to the thread1.output file
                 } catch (FileNotFoundException e) { }
             }
         });
     
         t1.start();
         method1("main"); //prints to original System.out
     }
     public static void method1(String thread) {
         //pre-existing code which writes to System.out
         System.out.println("This output will be written to the correct outputstream in thread: " + thread);
     }
     
    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      ThreadBoundOutputStream​(java.io.OutputStream stream)
      Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ThreadBoundOutputStream bindSystemErr()
      Bind the System.err PrintStream to a ThreadBoundOutputStream and return it.
      static ThreadBoundOutputStream bindSystemOut()
      Bind the System.out PrintStream to a ThreadBoundOutputStream and return it.
      void close()  
      void flush()  
      static java.io.PrintStream getOrigSystemErr​(java.io.PrintStream out)
      Get the original System.err PrintStream if the input is the bound version
      static java.io.PrintStream getOrigSystemOut​(java.io.PrintStream out)
      Get the original System.out PrintStream if the input is the bound version
      java.io.OutputStream getSink()
      Return the original OutputStream sink
      java.io.OutputStream getThreadStream()  
      void installThreadStream​(java.io.OutputStream stream)
      Install an outputstream for the current thread and any child threads
      java.io.OutputStream removeThreadStream()
      Remove the custom stream for the current thread.
      static void unbindSystemErr()
      Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut().
      static void unbindSystemOut()
      Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut().
      void write​(int i)  
      • Methods inherited from class java.io.FilterOutputStream

        write, 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

      • ThreadBoundOutputStream

        public ThreadBoundOutputStream​(java.io.OutputStream stream)
        Create a ThreadBoundOutputStream with a particular OutputStream as the default write destination.
        Parameters:
        stream - default sink
    • Method Detail

      • installThreadStream

        public void installThreadStream​(java.io.OutputStream stream)
        Install an outputstream for the current thread and any child threads
        Parameters:
        stream - the new output stream
      • removeThreadStream

        public java.io.OutputStream removeThreadStream()
        Remove the custom stream for the current thread.
        Returns:
        thread bound OutputStream or null if none exists
      • getThreadStream

        public java.io.OutputStream getThreadStream()
      • write

        public void write​(int i)
                   throws java.io.IOException
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
      • bindSystemOut

        public static ThreadBoundOutputStream bindSystemOut()
        Bind the System.out PrintStream to a ThreadBoundOutputStream and return it. If System.out is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed with unbindSystemOut().
        Returns:
        A ThreadBoundOutputStream bound to System.out
      • getOrigSystemOut

        public static java.io.PrintStream getOrigSystemOut​(java.io.PrintStream out)
        Get the original System.out PrintStream if the input is the bound version
        Parameters:
        out - pass in the current value of System.out
        Returns:
        the original System.out after binding, or the out parameter
      • unbindSystemOut

        public static void unbindSystemOut()
        Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut(). WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound.Use the removeThreadStream() method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.
      • bindSystemErr

        public static ThreadBoundOutputStream bindSystemErr()
        Bind the System.err PrintStream to a ThreadBoundOutputStream and return it. If System.err is already bound, returns the pre-bound ThreadBoundOutputStream. The binding can be removed with unbindSystemErr().
        Returns:
        A ThreadBoundOutputStream bound to System.err
      • getOrigSystemErr

        public static java.io.PrintStream getOrigSystemErr​(java.io.PrintStream out)
        Get the original System.err PrintStream if the input is the bound version
        Parameters:
        out - pass in the current value of System.err
        Returns:
        the original System.err after binding, or the out parameter
      • unbindSystemErr

        public static void unbindSystemErr()
        Resets the System.out printstream to the original PrintStream prior to the last call to bindSystemOut(). WARNING: you should only call unbindSystemOut if you know that no other threads are depending on the System.out to be bound. Use the removeThreadStream() method to remove any OutputStream bound to a particular thread with a ThreadBoundOutputStream.
      • getSink

        public java.io.OutputStream getSink()
        Return the original OutputStream sink
        Returns:
        the OutputStream