IOUtil.java

  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. package org.apache.commons.net.examples.util;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;

  21. import org.apache.commons.net.io.Util;
  22. import org.apache.commons.net.util.NetConstants;

  23. /**
  24.  * 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
  25.  * 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
  26.  * 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
  27.  * threads terminate when the remote input source closes.
  28.  */

  29. public final class IOUtil {

  30.     public static void readWrite(final InputStream remoteInput, final OutputStream remoteOutput, final InputStream localInput, final OutputStream localOutput) {
  31.         final Thread reader;
  32.         final Thread writer;

  33.         reader = new Thread(() -> {
  34.             int ch;

  35.             try {
  36.                 while (!Thread.interrupted() && (ch = localInput.read()) != NetConstants.EOS) {
  37.                     remoteOutput.write(ch);
  38.                     remoteOutput.flush();
  39.                 }
  40.             } catch (final IOException e) {
  41.                 // e.printStackTrace();
  42.             }
  43.         });

  44.         writer = new Thread(() -> {
  45.             try {
  46.                 Util.copyStream(remoteInput, localOutput);
  47.             } catch (final IOException e) {
  48.                 e.printStackTrace();
  49.                 System.exit(1);
  50.             }
  51.         });

  52.         writer.setPriority(Thread.currentThread().getPriority() + 1);

  53.         writer.start();
  54.         reader.setDaemon(true);
  55.         reader.start();

  56.         try {
  57.             writer.join();
  58.             reader.interrupt();
  59.         } catch (final InterruptedException e) {
  60.             // Ignored
  61.         }
  62.     }

  63. }