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  
18  package examples.util;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import org.apache.commons.net.io.Util;
24  
25  /***
26   * This is a utility class providing a reader/writer capability required
27   * by the weatherTelnet, rexec, rshell, and rlogin example programs.
28   * The only point of the class is to hold the static method readWrite
29   * which spawns a reader thread and a writer thread.  The reader thread
30   * reads from a local input source (presumably stdin) and writes the
31   * data to a remote output destination.  The writer thread reads from
32   * a remote input source and writes to a local output destination.
33   * The threads terminate when the remote input source closes.
34   ***/
35  
36  public final class IOUtil
37  {
38  
39      public static final void readWrite(final InputStream remoteInput,
40                                         final OutputStream remoteOutput,
41                                         final InputStream localInput,
42                                         final OutputStream localOutput)
43      {
44          Thread reader, writer;
45  
46          reader = new Thread()
47                   {
48                       @Override
49                       public void run()
50                       {
51                           int ch;
52  
53                           try
54                           {
55                               while (!interrupted() && (ch = localInput.read()) != -1)
56                               {
57                                   remoteOutput.write(ch);
58                                   remoteOutput.flush();
59                               }
60                           }
61                           catch (IOException e)
62                           {
63                               //e.printStackTrace();
64                           }
65                       }
66                   }
67                   ;
68  
69  
70          writer = new Thread()
71                   {
72                       @Override
73                       public void run()
74                       {
75                           try
76                           {
77                               Util.copyStream(remoteInput, localOutput);
78                           }
79                           catch (IOException e)
80                           {
81                               e.printStackTrace();
82                               System.exit(1);
83                           }
84                       }
85                   };
86  
87  
88          writer.setPriority(Thread.currentThread().getPriority() + 1);
89  
90          writer.start();
91          reader.setDaemon(true);
92          reader.start();
93  
94          try
95          {
96              writer.join();
97              reader.interrupt();
98          }
99          catch (InterruptedException e)
100         {
101             // Ignored
102         }
103     }
104 
105 }
106