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