org.das2.util.Expect

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.


before

String before the last match(if there was a match), updated after each expect() call


match

String representing the last match(if there was a match), updated after each expect() call


isSuccess

Whether the last match was successful, updated after each expect() call


RETV_TIMEOUT


RETV_EOF


RETV_IOEXCEPTION


byteToPrintableString

byteToPrintableString( byte b ) → String

Returns:

java.lang.String

search for examples view on GitHub view source


bytesToPrintableString

bytesToPrintableString( byte[] bytes ) → String

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.

Parameters

bytes - bytes to be printed

Returns:

String representation of the byte array

search for examples view on GitHub view source


expect

expect( java.lang.Object[] patterns ) → int

Convenience method, same as calling {@link #expect(int, Object...) expect(default_timeout, patterns)}

Parameters

patterns -

Returns:

int

search for examples view on GitHub view source


expectEOF

expectEOF( int timeout ) → int

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.

Parameters

timeout -

Returns:

same as return value of {@link #expect(int, List)}

search for examples view on GitHub view source


expectEOFOrThrow

expectEOFOrThrow( int timeout ) → int

Throws checked exceptions when expectEOF was not successful.

Parameters

timeout - timeout in seconds

Returns:

same as return value of {@link #expect(int, List)}

search for examples view on GitHub view source


expectOrThrow

expectOrThrow( int timeout, java.lang.Object[] patterns ) → int

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.

Parameters

timeout -
patterns -

Returns:

same as {@link #expect(int, Object...) expect(timeout, patterns)}

search for examples view on GitHub view source


forwardInputStreamTo

forwardInputStreamTo( java.io.PrintStream duplicatedTo ) → void

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) {...
 }
 

Parameters

duplicatedTo - call with null if you want to turn off

Returns:

void (returns nothing)

search for examples view on GitHub view source


getDefault_timeout

getDefault_timeout( ) → int

Returns:

int

search for examples view on GitHub view source


getProcess

getProcess( ) → Process

Returns:

the spawned process, if this {@link Expect} object is created by spawning a process

search for examples view on GitHub view source


isNotransfer

isNotransfer( ) → boolean

Returns:

boolean

search for examples view on GitHub view source


isRestart_timeout_upon_receive

isRestart_timeout_upon_receive( ) → boolean

Returns:

boolean

search for examples view on GitHub view source


printDebugInfo

printDebugInfo( ) → void

print internal debug information to stderr.

Returns:

void (returns nothing)

search for examples view on GitHub view source


send

send( String str ) → void

Parameters

str - Convenience method to send a string to output handle

Returns:

void (returns nothing)

search for examples view on GitHub view source


setDefault_timeout

setDefault_timeout( int default_timeout ) → void

Returns:

void (returns nothing)

search for examples view on GitHub view source


setNotransfer

setNotransfer( boolean notransfer ) → void

Returns:

void (returns nothing)

search for examples view on GitHub view source


setRestart_timeout_upon_receive

setRestart_timeout_upon_receive( boolean restart_timeout_upon_receive ) → void

Returns:

void (returns nothing)

search for examples view on GitHub view source


spawn

spawn( String command ) → Expect

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.

Parameters

command -

Returns:

Expect object created using the input and output handles from the spawned process

search for examples view on GitHub view source