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 org.apache.commons.net.examples.util;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023
024import org.apache.commons.net.io.Util;
025import org.apache.commons.net.util.NetConstants;
026
027/**
028 * This is a utility class providing a reader/writer capability required by the weatherTelnet, rexec, rshell, and rlogin example programs. The only point of the
029 * class is to hold the static method readWrite which spawns a reader thread and a writer thread. The reader thread reads from a local input source (presumably
030 * stdin) and writes the data to a remote output destination. The writer thread reads from a remote input source and writes to a local output destination. The
031 * threads terminate when the remote input source closes.
032 */
033
034public final class IOUtil {
035
036    public static void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) {
037        final Thread reader;
038        final Thread writer;
039
040        reader = new Thread(() -> {
041            int ch;
042
043            try {
044                while (!Thread.interrupted() && (ch = localInput.read()) != NetConstants.EOS) {
045                    remoteOutput.write(ch);
046                    remoteOutput.flush();
047                }
048            } catch (final IOException e) {
049                // e.printStackTrace();
050            }
051        });
052
053        writer = new Thread(() -> {
054            try {
055                Util.copyStream(remoteInput, localOutput);
056            } catch (final IOException e) {
057                e.printStackTrace();
058                System.exit(1);
059            }
060        });
061
062        writer.setPriority(Thread.currentThread().getPriority() + 1);
063
064        writer.start();
065        reader.setDaemon(true);
066        reader.start();
067
068        try {
069            writer.join();
070            reader.interrupt();
071        } catch (final InterruptedException e) {
072            // Ignored
073        }
074    }
075
076}