Class RedirectStream

java.lang.Object
java.lang.Thread
org.moeaframework.util.io.RedirectStream
All Implemented Interfaces:
Runnable

public class RedirectStream extends Thread
Redirects all content received by an InputStream to an OutputStream. This thread terminates when the input stream is closed or the end of file is reached. The output stream is never closed.
  • Method Details

    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • redirect

      public static RedirectStream redirect(InputStream inputStream)
      Reads all the contents of the specified input stream. The contents are immediately deleted and are not redirected anywhere.
      Parameters:
      inputStream - the input stream from which content is read
      Returns:
      the thread, which can be joined to await termination
    • redirect

      public static RedirectStream redirect(InputStream inputStream, OutputStream outputStream)
      Redirects all the contents from the specified input stream to the specified output stream.
      Parameters:
      inputStream - the input stream from which content is read
      outputStream - the output stream to which the content is redirected
      Returns:
      the thread, which can be joined to await termination
    • capture

      public static String capture(ProcessBuilder processBuilder) throws IOException, InterruptedException
      Invokes the process and captures the output. Any error messages are sent to System.err.
      Parameters:
      processBuilder - the process builder
      Returns:
      the captured output
      Throws:
      IOException - if an I/O error occurred while running the process
      InterruptedException - if this thread was interrupted while waiting for the process to terminate
    • pipe

      public static void pipe(ProcessBuilder processBuilder, OutputStream out) throws IOException, InterruptedException
      Invokes the process and pipes output to the given stream. Any error messages are sent to System.err.
      Parameters:
      processBuilder - the process builder
      out - the stream where output is piped
      Throws:
      IOException - if an I/O error occurred while running the process
      InterruptedException - if this thread was interrupted while waiting for the process to terminate
    • pipe

      public static void pipe(ProcessBuilder processBuilder, OutputStream out, OutputStream err) throws IOException, InterruptedException
      Invokes the process and pipes output and error messages to the given streams.
      Parameters:
      processBuilder - the process builder
      out - the stream where output is piped
      err - the stream where error messages are piped
      Throws:
      IOException - if an I/O error occurred while running the process
      InterruptedException - if this thread was interrupted while waiting for the process to terminate
    • invoke

      public static void invoke(ProcessBuilder processBuilder) throws IOException, InterruptedException
      Invokes the process and pipes output and error messages to System.out and System.err, respectively.
      Parameters:
      processBuilder - the process builder
      Throws:
      IOException - if an I/O error occurred while running the process
      InterruptedException - if this thread was interrupted while waiting for the process to terminate