public final class ShellProcess implements AutoCloseable


Represents a process that is running sh as a shell user.

Opposite to Shell this class doesn't handle the concept of command, rather it allows to read and write in the process streams, i.e. stdin, stdout, stderr. For example, using ShellProcess, it's possible to write a file in shell space like /data/local/tmp launching cp /dev/stdin /data/local/tmp/myfile:

ShellProcess.create().use {

// Writing a file
it.write("cp /dev/stdin /data/local/tmp/myfile")
it.stdin.write(myByteByffer)

// Reading a file
it.write("cat /data/local/tmp/somefile")
assert(it.stdout.bufferedReader().readLine() == "This is a test")
}

For the vast majority of commands used for testing, Shell process is a better candidate and automatically captures the full command output, as opposed to ShellProcess that doesn't distinguish where a command ends and another starts.

Note that this class is not thread safe due to internal state changes, although streams can be read or written by another thread, as long as it's always the same. For example it's possible to read ShellProcess.stdOut from another thread, as long as this doesn't change later.

ShellProcess should be used only when access to streams is required, like in the above example.

Summary

Public methods

void

Closes the current process.

final @NonNull InputStream

An InputStream for the process standard error.

final @NonNull OutputStream

An OutputStream for the process standard output.

final @NonNull InputStream

An InputStream for the process standard output.

final boolean

Returns whether the process is closed.

final void

Writes a string in the process standard input.

Public methods

close

Added in 1.0.0-alpha02
public void close()

Closes the current process. Internally this simply sends the shell process the exit command. This means that the process is not immediately terminated, but gracefully shutdown finishing prior commands execution. Once a shell process is closed, further calls to close don't have any effect, as the stdIn will be closed after the first one.

getStdErr

Added in 1.0.0-alpha02
public final @NonNull InputStream getStdErr()

An InputStream for the process standard error. Note that this is backed by a tcp connection to the native cli utility. If a large output is expected but it's not important to capture it, a shell command may be launched piping in /dev/null.

For example:

cat large_file 2> /dev/null

getStdIn

Added in 1.0.0-alpha02
public final @NonNull OutputStream getStdIn()

An OutputStream for the process standard output.

getStdOut

Added in 1.0.0-alpha02
public final @NonNull InputStream getStdOut()

An InputStream for the process standard output. Note that this is backed by a tcp connection to the native cli utility. If a large output is expected but it's not important to capture it, a shell command may be launched piping in /dev/null.

For example:

cat large_file /dev/null

isClosed

Added in 1.0.0-alpha02
public final boolean isClosed()

Returns whether the process is closed.

writeLine

Added in 1.0.0-alpha02
public final void writeLine(@NonNull String string)

Writes a string in the process standard input.

Parameters
@NonNull String string

a utf-8 string to write in the process stdin.