Class Redirect

java.lang.Object
aQute.lib.redirect.Redirect

public class Redirect extends Object
Utility to redirect the stdin/stdout/stderr when running a command
  • Field Details

    • stdin

      private final InputStream stdin
    • stdout

      private final OutputStream stdout
    • stderr

      private final OutputStream stderr
    • captureStdout

      private boolean captureStdout
    • captureStderr

      private boolean captureStderr
    • cstdout

      private Capture cstdout
    • cstderr

      private Capture cstderr
  • Constructor Details

    • Redirect

      public Redirect(InputStream stdin, OutputStream stdout, OutputStream stderr)
      Create a stdio redirector
      Parameters:
      stdin - the stdin read from or null for System.in
      stdout - the stdout to write to or null for System.out
      stderr - the stderr to write to or null for System.err
    • Redirect

      public Redirect(String stdin, OutputStream stdout, OutputStream stderr)
      Create a stdio redirector
      Parameters:
      stdin - the stdin read from or null for System.in
      stdout - the stdout to write to or null for System.out
      stderr - the stderr to write to or null for System.err
    • Redirect

      public Redirect(OutputStream stdout, OutputStream stderr)
      Create a stdio redirector without any input
      Parameters:
      stdout - the stdout to write to or null for System.out
      stderr - the stderr to write to or null for System.err
  • Method Details

    • captureStdout

      public Redirect captureStdout()
      Capture stdout during an apply. Every apply will be captured seperately.
      Returns:
      this
    • captureStderr

      public Redirect captureStderr()
      Capture stderr during an apply. Every apply will be captured seperately.
      Returns:
      this
    • apply

      public <R> R apply(SupplierWithException<R> f) throws Exception
      Call the supplier and return the result. While the supplier is active, the System streams are redirected as instructed by the constructor. Redirection & capture will only take place on the current thread. The original state will be introduced afterwards.

      Although the system streams are redirected, the original output streams are still written to.

      Type Parameters:
      R - the type for the supplier
      Parameters:
      f - the supplier
      Returns:
      the return of the supplier.
      Throws:
      Exception
    • build

      private PrintStream build(PrintStream original, OutputStream redirect, Capture capture)
    • getStderr

      public String getStderr()
    • getStdout

      public String getStdout()
    • getContent

      public String getContent()