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    *      https://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 in;
28      OutputStream out;
29      String inputs[];
30      String outputs[];
31      long timeout;
32  
33      /**
34       * Constructs a new instance. Starts a new thread for the reader.
35       * <p>
36       *
37       * @param is        InputStream on which to read.
38       * @param os        OutputStream on which to answer.
39       * @param inputs    Array of waited for Strings.
40       * @param outputs   Array of answers.
41       * @param timeout   milliseconds
42       */
43      public TelnetTestResponder(final InputStream is, final OutputStream os, final String inputs[], final String outputs[], final long timeout) {
44          this.in = is;
45          this.out = os;
46          this.timeout = timeout;
47          this.inputs = inputs;
48          this.outputs = outputs;
49          final Thread reader = new Thread(this);
50  
51          reader.start();
52      }
53  
54      /**
55       * Runs the responder
56       */
57      @Override
58      public void run() {
59          boolean result = false;
60          final byte[] buffer = new byte[32];
61          final long starttime = System.currentTimeMillis();
62  
63          try {
64              final StringBuilder readbytes = new StringBuilder();
65              while (!result && System.currentTimeMillis() - starttime < timeout) {
66                  if (in.available() > 0) {
67                      final int ret_read = in.read(buffer);
68                      readbytes.append(new String(buffer, 0, ret_read));
69  
70                      for (int ii = 0; ii < inputs.length; ii++) {
71                          if (readbytes.indexOf(inputs[ii]) >= 0) {
72                              Thread.sleep(1000 * ii);
73                              out.write(outputs[ii].getBytes());
74                              result = true;
75                          }
76                      }
77                  } else {
78                      Thread.sleep(500);
79                  }
80              }
81  
82          } catch (final Exception e) {
83              System.err.println("Error while waiting endstring. " + e.getMessage());
84          }
85      }
86  }