Provides similar functions as the Unix Expect tool.
There are two ways to create an Expect object: a constructor that takes an
{@link InputStream} handle and {@link OutputStream} handle; or spawning a
process by providing a command String.
The API is loosely based on Perl Expect library:
http://search.cpan.org/~rgiersig/Expect-1.15/Expect.pod
If you are not familiar with the Tcl version of Expect, take a look at:
http://oreilly.com/catalog/expect/chapter/ch03.html
Expect uses a thread to convert InputStream to a SelectableChannel; other
than this, no multi-threading is used.
A call to expect() will block for at most timeout seconds. Expect is not
designed to be thread-safe, in other words, do not call methods of the same
Expect object in different threads.
String before the last match(if there was a match), updated after each expect() call
String representing the last match(if there was a match), updated after each expect() call
Whether the last match was successful, updated after each expect() call
Static method used for convert byte array to string, each byte is converted to an ASCII character, if the byte represents a control character, it is replaced by a printable caret notation http://en.wikipedia.org/wiki/ASCII , or an escape code if possible.
Convenience method, same as calling {@link #expect(int, Object...) expect(default_timeout, patterns)}
Convenience method, internally it calls {@link #expect(int, List) expect(timeout, new ArrayList<Pattern>())}. Given an empty list, {@link #expect(int, List)} will not perform any regex matching, therefore the only conditions for it to return is EOF or timeout (or IOException). If EOF is detected, {@link #isSuccess} and {@link #before} are properly set.
Throws checked exceptions when expectEOF was not successful.
This method calls {@link #expect(int, Object...) expect(timeout, patterns)}, and throws checked exceptions when expect was not successful. Useful when you want to simplify error handling: for example, when you send a series of commands to an SSH server, you expect a prompt after each send, however the server may die or the prompt may take forever to appear, you would want to skip the following commands if those occurred. In such a case this method will be handy.
While performing expect operations on the InputStream provided, duplicate the contents obtained from InputStream to a PrintStream (you can use System.err or System.out). DO NOT call this function while there are live Expect objects as this may cause the piping thread to end due to unsynchronized code; if you need this feature, add the following to both {@link #inputStreamToSelectableChannel(InputStream)} and {@link #forwardInputStreamTo(PrintStream)}:
synchronized(Expect.duplicatedTo) {...
}
print internal debug information to stderr.
Creates an Expect object by spawning a command.
To Linux users, perhaps you need to use "bash -i" if you want to spawn
Bash.
Note: error stream of the process is redirected to output stream.