001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package examples;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.OutputStream;
023    import org.apache.commons.net.io.Util;
024    
025    /***
026     * This is a utility class providing a reader/writer capability required
027     * by the weatherTelnet, rexec, rshell, and rlogin example programs.
028     * The only point of the class is to hold the static method readWrite
029     * which spawns a reader thread and a writer thread.  The reader thread
030     * reads from a local input source (presumably stdin) and writes the
031     * data to a remote output destination.  The writer thread reads from
032     * a remote input source and writes to a local output destination.
033     * The threads terminate when the remote input source closes.
034     * <p>
035     ***/
036    
037    public final class IOUtil
038    {
039    
040        public static final void readWrite(final InputStream remoteInput,
041                                           final OutputStream remoteOutput,
042                                           final InputStream localInput,
043                                           final OutputStream localOutput)
044        {
045            Thread reader, writer;
046    
047            reader = new Thread()
048                     {
049                         public void run()
050                         {
051                             int ch;
052    
053                             try
054                             {
055                                 while (!interrupted() && (ch = localInput.read()) != -1)
056                                 {
057                                     remoteOutput.write(ch);
058                                     remoteOutput.flush();
059                                 }
060                             }
061                             catch (IOException e)
062                             {
063                                 //e.printStackTrace();
064                             }
065                         }
066                     }
067                     ;
068    
069    
070            writer = new Thread()
071                     {
072                         public void run()
073                         {
074                             try
075                             {
076                                 Util.copyStream(remoteInput, localOutput);
077                             }
078                             catch (IOException e)
079                             {
080                                 e.printStackTrace();
081                                 System.exit(1);
082                             }
083                         }
084                     };
085    
086    
087            writer.setPriority(Thread.currentThread().getPriority() + 1);
088    
089            writer.start();
090            reader.setDaemon(true);
091            reader.start();
092    
093            try
094            {
095                writer.join();
096                reader.interrupt();
097            }
098            catch (InterruptedException e)
099            {
100            }
101        }
102    
103    }
104