1 16 package org.apache.commons.net.telnet; 17 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 21 22 30 public class TelnetTestResponder implements Runnable 31 { 32 InputStream _is; 33 OutputStream _os; 34 String _inputs[], _outputs[]; 35 long _timeout; 36 37 46 public TelnetTestResponder(InputStream is, OutputStream os, String inputs[], String outputs[], long timeout) 47 { 48 _is = is; 49 _os = os; 50 _timeout = timeout; 51 _inputs = inputs; 52 _outputs = outputs; 53 Thread reader = new Thread (this); 54 55 reader.start(); 56 } 57 58 61 public void run() 62 { 63 boolean result = false; 64 byte buffer[] = new byte[32]; 65 long starttime = System.currentTimeMillis(); 66 67 try 68 { 69 String readbytes = new String (); 70 while(!result && 71 ((System.currentTimeMillis() - starttime) < _timeout)) 72 { 73 if(_is.available() > 0) 74 { 75 int ret_read = _is.read(buffer); 76 readbytes = readbytes + new String (buffer, 0, ret_read); 77 78 for(int ii=0; ii<_inputs.length; ii++) 79 { 80 if(readbytes.indexOf(_inputs[ii]) >= 0) 81 { 82 Thread.sleep(1000 * ii); 83 _os.write(_outputs[ii].getBytes()); 84 result = true; 85 } 86 } 87 } 88 else 89 { 90 Thread.sleep(500); 91 } 92 } 93 94 } 95 catch (Exception e) 96 { 97 System.err.println("Error while waiting endstring. " + e.getMessage()); 98 } 99 } 100 } 101 | Popular Tags |