View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.telnet;
18  
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  /**
23   * Simple stream responder. Waits for strings on an input stream and answers sending corresponfing strings on an output stream. The reader runs in a separate
24   * thread.
25   */
26  public class TelnetTestResponder implements Runnable {
27      InputStream _is;
28      OutputStream _os;
29      String _inputs[], _outputs[];
30      long _timeout;
31  
32      /**
33       * Constructor. Starts a new thread for the reader.
34       * <p>
35       *
36       * @param is      - InputStream on which to read.
37       * @param os      - OutputStream on which to answer.
38       * @param inputs  - Array of waited for Strings.
39       * @param outputs - Array of answers.
40       * @param timeout - milliseconds
41       */
42      public TelnetTestResponder(final InputStream is, final OutputStream os, final String inputs[], final String outputs[], final long timeout) {
43          _is = is;
44          _os = os;
45          _timeout = timeout;
46          _inputs = inputs;
47          _outputs = outputs;
48          final Thread reader = new Thread(this);
49  
50          reader.start();
51      }
52  
53      /**
54       * Runs the responder
55       */
56      @Override
57      public void run() {
58          boolean result = false;
59          final byte[] buffer = new byte[32];
60          final long starttime = System.currentTimeMillis();
61  
62          try {
63              final StringBuilder readbytes = new StringBuilder();
64              while (!result && System.currentTimeMillis() - starttime < _timeout) {
65                  if (_is.available() > 0) {
66                      final int ret_read = _is.read(buffer);
67                      readbytes.append(new String(buffer, 0, ret_read));
68  
69                      for (int ii = 0; ii < _inputs.length; ii++) {
70                          if (readbytes.indexOf(_inputs[ii]) >= 0) {
71                              Thread.sleep(1000 * ii);
72                              _os.write(_outputs[ii].getBytes());
73                              result = true;
74                          }
75                      }
76                  } else {
77                      Thread.sleep(500);
78                  }
79              }
80  
81          } catch (final Exception e) {
82              System.err.println("Error while waiting endstring. " + e.getMessage());
83          }
84      }
85  }