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