Class ProcessInputStream

java.lang.Object
java.io.InputStream
nl.knaw.dans.lib.util.ProcessInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class ProcessInputStream extends InputStream
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    void
    Closes the input stream and ensures that the process exited successfully.
    void
    mark(int readlimit)
     
    boolean
     
    int
     
    int
    read(byte[] b)
     
    int
    read(byte[] b, int off, int len)
     
    void
     
    long
    skip(long n)
     
    start(org.apache.commons.exec.CommandLine commandLine)
    Starts the given command and returns an InputStream that: streams the process stdout; throws an IOException if the process exits non-zero or fails to start.
    start(org.apache.commons.exec.CommandLine commandLine, int... successExitCodes)
    Starts the given command and returns an InputStream that: streams the process stdout; throws an IOException if the process exits non-zero or fails to start.
    start(org.apache.commons.exec.CommandLine commandLine, File workingDirectory, Duration timeout, int... successExitCodes)
    Starts the given command with an optional working directory and timeout, returning an InputStream that: streams the process stdout; and treats the provided exit codes as success (all others trigger IOException).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_BYTES_ERROR_SNIPPET

      public static final int MAX_BYTES_ERROR_SNIPPET
      See Also:
  • Method Details

    • start

      public static ProcessInputStream start(org.apache.commons.exec.CommandLine commandLine) throws IOException
      Starts the given command and returns an InputStream that:
      • streams the process stdout;
      • throws an IOException if the process exits non-zero or fails to start.
      Parameters:
      commandLine - the command to start
      Returns:
      an InputStream that reads from the process stdout
      Throws:
      IOException - if the process fails to start or exits non-zero
    • start

      public static ProcessInputStream start(org.apache.commons.exec.CommandLine commandLine, int... successExitCodes) throws IOException
      Starts the given command and returns an InputStream that:
      • streams the process stdout;
      • throws an IOException if the process exits non-zero or fails to start.
      Parameters:
      commandLine - the command to start
      successExitCodes - exit codes considered successful (defaults to {0} if null or empty)
      Returns:
      an InputStream that reads from the process stdout
      Throws:
      IOException - if the process fails to start or exits non-zero
    • start

      public static ProcessInputStream start(org.apache.commons.exec.CommandLine commandLine, File workingDirectory, Duration timeout, int... successExitCodes) throws IOException
      Starts the given command with an optional working directory and timeout, returning an InputStream that:
      • streams the process stdout; and
      • treats the provided exit codes as success (all others trigger IOException).
      Parameters:
      commandLine - the command to start
      workingDirectory - optional working directory for the process (null to leave unset)
      timeout - optional timeout (null for no timeout)
      successExitCodes - exit codes considered successful (defaults to {0} if null or empty)
      Throws:
      IOException
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • close

      public void close() throws IOException
      Closes the input stream and ensures that the process exited successfully. Note that this method will block until the process completes.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if the process fails to start or exits with an error code.