Module id.xfunction

Class XProcess

Object
XProcess

public class XProcess extends Object
Wraps standard java.lang.Process class with convenient methods.

Some commands may block until you start reading their stdout or stderr. This may be problem when you just want to run a command ignoring its output. Use flush methods in that case.

  • Constructor Details

    • XProcess

      public XProcess(Process process, List<String> secrets)
    • XProcess

      public XProcess(Process process, Stream<String> stdout, Stream<String> stderr, int code)
      This ctor supposed to be used in tests when you want to mock results of XExec
    • XProcess

      public XProcess(Process process, Stream<String> stdout, Stream<String> stderr, List<String> secrets, int code)
      This ctor supposed to be used in tests when you want to mock results of XExec
  • Method Details

    • stdout

      public String stdout()
      Returns standard output as a string. This call will consume stdout stream meaning that you can call it only once. If you want to call it multiple times make sure to call stdoutAsync(boolean) or forwardStdoutAsync(boolean) first.
      Throws:
      IllegalStateException - if called more than once
      See Also:
    • stdoutAsync

      public XProcess stdoutAsync(boolean ignore)
      Consumes stdout stream into internal buffer which later can be obtained through stdout() or ignores it. This call is async.

      When process writes to stdout it may get blocked until somebody starts reading it. This methods starts reading immediately in background (so that process will not block) and preserves the output.

    • forwardStdoutAsync

      public XProcess forwardStdoutAsync(boolean ignore)
      Consumes stdout stream by forwarding it to System.out. This call is async.
      Parameters:
      ignore - save forwarded output into internal buffer which later can be obtained through stdout() or ignore it
    • stdoutAsync

      public XProcess stdoutAsync(Consumer<String> consumer)
      Consumes stdout stream by forwarding it to consumer. This call is async.
    • stderr

      public String stderr()
      Returns standard error output as a string. This call will consume stderr stream meaning that you can call it only once. If you want to call it multiple times make sure to call stderrAsync(boolean) or forwardStderrAsync(boolean) first.
      Throws:
      IllegalStateException - if called more than once
      See Also:
    • stderrThrow

      public XProcess stderrThrow()
      Block until process finishes and then check its return code. If it is non 0 then throw an exception with data from stderr.
    • stderrAsync

      public XProcess stderrAsync(boolean ignore)
      Consumes stderr stream into internal buffer which later can be obtained through stderr() or ignores it. This call is async.

      When process writes to stderr it may get blocked until somebody starts reading it. This methods starts reading immediately in background (so that process will not block) and preserves the output.

    • stderrAsync

      public XProcess stderrAsync(Consumer<String> consumer)
      Consumes stderr stream by forwarding it to consumer. This call is async.
    • forwardStderrAsync

      public XProcess forwardStderrAsync(boolean ignore)
      Consumes stderr stream by forwarding it to System.err This call is async.
      Parameters:
      ignore - save forwarded stderr into internal buffer which later can be obtained through stderr() or ignore it
    • outputAsync

      public XProcess outputAsync(boolean ignore)
      Combines stdoutAsync(boolean) and stderrAsync(boolean) into one method

      When process writes to stdout/stderr it may get blocked until somebody starts reading it. This methods starts reading both immediately in background (so that process will not block) and preserves the output.

      See Also:
    • forwardOutputAsync

      public XProcess forwardOutputAsync(boolean ignore)
      Consumes stdout and stderr by forwarding them to System.out and System.err respectively.
      Parameters:
      ignore - save forwarded outputs into internal buffers which later can be obtained through stdout() and stderr() or ignore it
      See Also:
    • process

      public Process process()
    • stdoutAsStream

      public Stream<String> stdoutAsStream()
      After you consume this stream it will not be longer valid.
    • stderrAsStream

      public Stream<String> stderrAsStream()
      After you consume this stream it will not be longer valid.
    • code

      public CompletableFuture<Integer> code()
      Returns:
      process return code
    • await

      public int await()
      Waits for process to complete and returns code safely wrapping all checked exceptions to RuntimeException.

      Make sure to use outputAsync(boolean) method to ignore both output/stderr.

    • destroyAllForcibly

      public void destroyAllForcibly()
      Java original method Process.destroyForcibly() does not destroy child processes. This method destroys all children processes including parent one.