());
if (retv == RETV_EOF) {
this.isSuccess = true;
this.before = this.buffer.toString();
this.buffer.delete(0, buffer.length());
}
return retv;
}
/**Convenience method, same as calling {@link #expectEOF(int)
* expectEOF(default_timeout)}
* @return same as return value of {@link #expect(int, List)}
*/
public int expectEOF() {
return expectEOF(default_timeout);
}
/**
* Throws checked exceptions when expectEOF was not successful.
* @param timeout timeout in seconds
* @return same as return value of {@link #expect(int, List)}
* @throws external.Expect.TimeoutException
* @throws java.io.IOException
*/
public int expectEOFOrThrow(int timeout) throws TimeoutException,
IOException {
int retv = expectEOF(timeout);
if (retv == RETV_TIMEOUT)
throw new TimeoutException();
if (retv == RETV_IOEXCEPTION)
throw thrownIOE;
return retv;
}
/**
* Convenience method, same as calling {@link #expectEOF(int)
* expectEOF(default_timeout)}
* @return same as return value of {@link #expect(int, List)}
* @throws IOException
* @throws TimeoutException
*/
public int expectEOFOrThrow() throws TimeoutException, IOException {
return expectEOFOrThrow(default_timeout);
}
/**useful when calling {@link #expectOrThrow(int, Object...)}*/
private IOException thrownIOE;
/**
* 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.
*
* @param timeout
* @param patterns
* @throws TimeoutException
* when expect times out
* @throws EOFException
* when EOF is encountered
* @throws IOException
* when there is a problem reading from the InputStream
* @return same as {@link #expect(int, Object...) expect(timeout, patterns)}
*/
public int expectOrThrow(int timeout, Object... patterns)
throws TimeoutException, EOFException, IOException {
int retv = expect(timeout, patterns);
switch (retv) {
case RETV_TIMEOUT:
throw new TimeoutException();
case RETV_EOF:
throw new EOFException();
case RETV_IOEXCEPTION:
throw thrownIOE;
default:
return retv;
}
}
/**Convenience method, same as calling {@link #expectOrThrow(int, Object...)
* expectOrThrow(default_timeout, patterns) }
* @param patterns
* @return same as {@link #expect(int, Object...) expect(timeout, patterns)}
* @throws external.Expect.TimeoutException
* @throws external.Expect.EOFException
* @throws java.io.IOException*/
public int expectOrThrow(Object... patterns) throws TimeoutException,
EOFException, IOException {
return expectOrThrow(default_timeout, patterns);
}
private void clearGlobalVariables() {
isSuccess = false;
match = null;
before = null;
}
/**
* 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. For example, when you connect through SSH, send an "exit" command
* first, and then call this method.
*
*
* When this method is called, the thread which write to the sink of the
* pipe will end.
*/
public void close() {
try {
this.output.close();
} catch (IOException e) {
log.log( Level.WARNING, "Exception when closing OutputStream", e);
//e.printStackTrace();
}
try {
this.inputChannel.close();
} catch (IOException e) {
log.log( Level.WARNING, "Exception when closing input Channel", e);
//e.printStackTrace();
}
}
public int getDefault_timeout() {
return default_timeout;
}
public void setDefault_timeout(int default_timeout) {
this.default_timeout = default_timeout;
}
public boolean isRestart_timeout_upon_receive() {
return restart_timeout_upon_receive;
}
public void setRestart_timeout_upon_receive(boolean restart_timeout_upon_receive) {
this.restart_timeout_upon_receive = restart_timeout_upon_receive;
}
public void setNotransfer(boolean notransfer) {
this.notransfer = notransfer;
}
public boolean isNotransfer() {
return notransfer;
}
/**
* 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.
*
* @param bytes
* bytes to be printed
* @return String representation of the byte array
*/
public static String bytesToPrintableString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(byteToPrintableString(b));
return sb.toString();
}
public static String byteToPrintableString(byte b) {
String s = new String(new byte[] { b });
// control characters
if (b >= 0 && b < 32) s = "^" + (char) (b + 64);
else if (b == 127) s = "^?";
// some escape characters
if (b == 9) s = "\\t";
if (b == 10) s = "\\n";
if (b == 13) s = "\\r";
return s;
}
@SuppressWarnings("serial")
public static class TimeoutException extends Exception{
}
@SuppressWarnings("serial")
public static class EOFException extends Exception{
}
private static PrintStream duplicatedTo = null;
/**
* 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)}:
*
* {@code
* synchronized(Expect.duplicatedTo) {...}
* }
*
* @param duplicatedTo
* call with null if you want to turn off
*/
public static void forwardInputStreamTo(PrintStream duplicatedTo) {
Expect.duplicatedTo = duplicatedTo;
}
}