Class Redirect


  • public class Redirect
    extends java.lang.Object
    Utility to redirect the stdin/stdout/stderr when running a command
    • Constructor Summary

      Constructors 
      Constructor Description
      Redirect​(java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
      Create a stdio redirector
      Redirect​(java.io.OutputStream stdout, java.io.OutputStream stderr)
      Create a stdio redirector without any input
      Redirect​(java.lang.String stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
      Create a stdio redirector
    • Constructor Detail

      • Redirect

        public Redirect​(java.io.InputStream stdin,
                        java.io.OutputStream stdout,
                        java.io.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​(java.lang.String stdin,
                        java.io.OutputStream stdout,
                        java.io.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​(java.io.OutputStream stdout,
                        java.io.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 Detail

      • 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 java.lang.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:
        java.lang.Exception
      • getStderr

        public java.lang.String getStderr()
      • getStdout

        public java.lang.String getStdout()
      • getContent

        public java.lang.String getContent()