com.mindbright.util
Class ExpectOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended bycom.mindbright.util.ExpectOutputStream

public final class ExpectOutputStream
extends java.io.OutputStream

This class implements an ouput stream which captures data until a certain expected string has been encountered. To use it the caller should first have a class which implements Expector. For example:

 class MyClass implements ExpectOutputStream.Expector {
   public void reached(ExpectOutputStream out, byte[] buf, int len) {
     System.out.println("Before this we got " + len + " bytes");
   }
   public void closed(ExpectOutputStream out, byte[] buf, int len) {
     System.out.println("Stream closed");
   }
 
MyClass can then create an instance of ExpectOutputStream which waits for the desired string, which in this example is END_OF_OUTPUT:
   outputStream = new ExpectOutputStream(this, "END_OF_OUTPUT");

   // Attach to remote console
   SSH2ConsoleRemote console = ...
   console.changeStdOut(outputStream);
 
And that is it. The reached function will be called when the string END_OF_OUTPUT is output from the console. The buffer passed will contain all text up until that point.

For a full usage example see the RemoteShellScript example.

See Also:
SSH2ConsoleRemote, RemoteShellScript

Nested Class Summary
static interface ExpectOutputStream.Expector
          Interface to be implemented by classes interested when the event ExpectOutputStream waits for happens.
 
Constructor Summary
ExpectOutputStream(ExpectOutputStream.Expector expector)
          Creates an expecting output stream which does not expect anything.
ExpectOutputStream(ExpectOutputStream.Expector expector, byte[] buf, int off, int len)
          Creates an expecting output stream which waits for a specified string.
ExpectOutputStream(ExpectOutputStream.Expector expector, java.lang.String boundary)
          Creates an expecting output stream which waits for a specified string.
 
Method Summary
 void close()
          See java.io.OutputStream
 void expect(byte[] boundary)
          Changes the string this instance is waiting for.
 void expect(java.lang.String boundary)
          Changes the string this instance is waiting for.
 void write(byte[] b, int off, int len)
          See java.io.OutputStream
 void write(int b)
          See java.io.OutputStream
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpectOutputStream

public ExpectOutputStream(ExpectOutputStream.Expector expector)
Creates an expecting output stream which does not expect anything.

Parameters:
expector - class interested in when the expected string occurs

ExpectOutputStream

public ExpectOutputStream(ExpectOutputStream.Expector expector,
                          java.lang.String boundary)
Creates an expecting output stream which waits for a specified string.

Parameters:
expector - class interested in when the expected string occurs
boundary - the string to wait for

ExpectOutputStream

public ExpectOutputStream(ExpectOutputStream.Expector expector,
                          byte[] buf,
                          int off,
                          int len)
Creates an expecting output stream which waits for a specified string.

Parameters:
expector - class interested in when the expected string occurs
buf - array holding the string it should wait for
off - offset in array where the string starts
len - length of string in array
Method Detail

expect

public void expect(java.lang.String boundary)
Changes the string this instance is waiting for.

Parameters:
boundary - the string to wait for

expect

public void expect(byte[] boundary)
Changes the string this instance is waiting for.

Parameters:
boundary - the string to wait for

write

public void write(int b)
See java.io.OutputStream


write

public void write(byte[] b,
                  int off,
                  int len)
See java.io.OutputStream


close

public void close()
See java.io.OutputStream