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
018package examples.util;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import 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 ***/
035
036public final class IOUtil
037{
038
039    public static final void readWrite(final InputStream remoteInput,
040                                       final OutputStream remoteOutput,
041                                       final InputStream localInput,
042                                       final OutputStream localOutput)
043    {
044        Thread reader, writer;
045
046        reader = new Thread()
047                 {
048                     @Override
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                     @Override
073                     public void run()
074                     {
075                         try
076                         {
077                             Util.copyStream(remoteInput, localOutput);
078                         }
079                         catch (IOException e)
080                         {
081                             e.printStackTrace();
082                             System.exit(1);
083                         }
084                     }
085                 };
086
087
088        writer.setPriority(Thread.currentThread().getPriority() + 1);
089
090        writer.start();
091        reader.setDaemon(true);
092        reader.start();
093
094        try
095        {
096            writer.join();
097            reader.interrupt();
098        }
099        catch (InterruptedException e)
100        {
101            // Ignored
102        }
103    }
104
105}
106