public class Expect
extends java.lang.Object
InputStream
handle and OutputStream
handle; or spawning a
process by providing a command String. Modifier and Type | Class and Description |
---|---|
static class |
Expect.EOFException |
static class |
Expect.TimeoutException |
Modifier and Type | Field and Description |
---|---|
java.lang.String |
before
String before the last match(if there was a match),
updated after each expect() call
|
boolean |
isSuccess
Whether the last match was successful,
updated after each expect() call
|
java.lang.String |
match
String representing the last match(if there was a match),
updated after each expect() call
|
static int |
RETV_EOF |
static int |
RETV_IOEXCEPTION |
static int |
RETV_TIMEOUT |
Constructor and Description |
---|
Expect(java.io.InputStream input,
java.io.OutputStream output) |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
bytesToPrintableString(byte[] bytes)
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.
|
static java.lang.String |
byteToPrintableString(byte b) |
void |
close()
The OutputStream passed to Expect constructor is closed; the InputStream
is not closed (there is no need to close the InputStream).
It is suggested that this method be called after the InputStream has come to EOF. |
int |
expect(int timeout,
java.util.List<java.util.regex.Pattern> list)
Expect will wait for the input handle to produce one of the patterns in
the list.
|
int |
expect(int timeout,
java.lang.Object... patterns)
Convenience method, internally it constructs a List<Pattern>
using the object array, and call
expect(int, List) using the
List. |
int |
expect(java.lang.Object... patterns)
Convenience method, same as calling
expect(default_timeout, patterns) |
int |
expectEOF()
Convenience method, same as calling
expectEOF(default_timeout) |
int |
expectEOF(int timeout)
Convenience method, internally it calls
expect(timeout, new ArrayList<Pattern>()) . |
int |
expectEOFOrThrow()
Convenience method, same as calling
expectEOF(default_timeout) |
int |
expectEOFOrThrow(int timeout)
Throws checked exceptions when expectEOF was not successful.
|
int |
expectOrThrow(int timeout,
java.lang.Object... patterns)
This method calls
expect(timeout,
patterns) , and throws checked exceptions when expect was not successful. |
int |
expectOrThrow(java.lang.Object... patterns)
Convenience method, same as calling
expectOrThrow(default_timeout, patterns) |
static void |
forwardInputStreamTo(java.io.PrintStream duplicatedTo)
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).
|
int |
getDefault_timeout() |
java.lang.Process |
getProcess() |
boolean |
isNotransfer() |
boolean |
isRestart_timeout_upon_receive() |
void |
printDebugInfo()
print internal debug information to stderr.
|
void |
send(byte[] toWrite) |
void |
send(java.lang.String str) |
void |
setDefault_timeout(int default_timeout) |
void |
setNotransfer(boolean notransfer) |
void |
setRestart_timeout_upon_receive(boolean restart_timeout_upon_receive) |
static Expect |
spawn(java.lang.String command)
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. |
public java.lang.String before
public java.lang.String match
public boolean isSuccess
public static final int RETV_TIMEOUT
public static final int RETV_EOF
public static final int RETV_IOEXCEPTION
public java.lang.Process getProcess()
Expect
object is created by
spawning a processpublic static Expect spawn(java.lang.String command)
command
- public void send(java.lang.String str)
str
- Convenience method to send a string to output handlepublic void send(byte[] toWrite)
toWrite
- Write a byte array to the output handle, notice flush()public int expect(java.lang.Object... patterns)
expect(default_timeout, patterns)
patterns
- public int expect(int timeout, java.lang.Object... patterns)
expect(int, List)
using the
List. The String
s in the object array will be treated as
literals; meanwhile Pattern
s will be directly added to the List.
If the array contains other objects, they will be converted by
Object.toString()
and then used as literal strings.timeout
- timeout in secondspatterns
- array of String regex, Pattern, or Object where toString() is called.public int expect(int timeout, java.util.List<java.util.regex.Pattern> list)
timeout
- timeout in secondslist
- List of Java Pattern
s used for match the internal
buffer obtained by reading the InputStreampublic void printDebugInfo()
public int expectEOF(int timeout)
expect(timeout, new ArrayList<Pattern>())
. Given an empty list,
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, isSuccess
and before
are properly
set.timeout
- expect(int, List)
public int expectEOF()
expectEOF(default_timeout)
expect(int, List)
public int expectEOFOrThrow(int timeout) throws Expect.TimeoutException, java.io.IOException
timeout
- timeout in secondsexpect(int, List)
external.Expect.TimeoutException
java.io.IOException
Expect.TimeoutException
public int expectEOFOrThrow() throws Expect.TimeoutException, java.io.IOException
expectEOF(default_timeout)
expect(int, List)
java.io.IOException
Expect.TimeoutException
public int expectOrThrow(int timeout, java.lang.Object... patterns) throws Expect.TimeoutException, Expect.EOFException, java.io.IOException
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.timeout
- patterns
- expect(timeout, patterns)
Expect.TimeoutException
- when expect times outExpect.EOFException
- when EOF is encounteredjava.io.IOException
- when there is a problem reading from the InputStreampublic int expectOrThrow(java.lang.Object... patterns) throws Expect.TimeoutException, Expect.EOFException, java.io.IOException
expectOrThrow(default_timeout, patterns)
patterns
- expect(timeout, patterns)
external.Expect.TimeoutException
external.Expect.EOFException
java.io.IOException
Expect.TimeoutException
Expect.EOFException
public void close()
public int getDefault_timeout()
public void setDefault_timeout(int default_timeout)
public boolean isRestart_timeout_upon_receive()
public void setRestart_timeout_upon_receive(boolean restart_timeout_upon_receive)
public void setNotransfer(boolean notransfer)
public boolean isNotransfer()
public static java.lang.String bytesToPrintableString(byte[] bytes)
bytes
- bytes to be printedpublic static java.lang.String byteToPrintableString(byte b)
public static void forwardInputStreamTo(java.io.PrintStream duplicatedTo)
inputStreamToSelectableChannel(InputStream)
and
forwardInputStreamTo(PrintStream)
:
synchronized(Expect.duplicatedTo) {...}
duplicatedTo
- call with null if you want to turn off