    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.exec;
    019
    020import java.io.InputStream;
    021import java.io.OutputStream;
    022
    023import org.apache.commons.exec.util.DebugUtils;
    024
    025/**
    026 * Copies all data from an System.input stream to an output stream of the executed process.
    027 */
    028public class InputStreamPumper implements Runnable {
    029
    030    /**
    031     * Sleep time in milliseconds.
    032     */
    033    public static final int SLEEPING_TIME = 100;
    034
    035    /** The input stream to pump from. */
    036    private final InputStream is;
    037
    038    /** The output stream to pmp into. */
    039    private final OutputStream os;
    040
    041    /** Flag to stop the stream pumping. */
    042    private volatile boolean stop;
    043
    044    /**
    045     * Create a new stream pumper.
    046     *
    047     * @param is input stream to read data from.
    048     * @param os output stream to write data to.
    049     */
    050    public InputStreamPumper(final InputStream is, final OutputStream os) {
    051        this.is = is;
    052        this.os = os;
    053        this.stop = false;
    054    }
    055
    056    /**
    057     * Copies data from the input stream to the output stream. Terminates as soon as the input stream is closed or an error occurs.
    058     */
    059    @Override
    060    public void run() {
    061        try {
    062            while (!stop) {
    063                while (is.available() > 0 && !stop) {
    064                    os.write(is.read());
    065                }
    066                os.flush();
    067                Thread.sleep(SLEEPING_TIME);
    068            }
    069        } catch (final Exception e) {
    070            final String msg = "Got exception while reading/writing the stream";
    071            DebugUtils.handleException(msg, e);
    072        }
    073    }
    074
    075    /**
    076     * Requests processing to stop.
    077     */
    078    public void stopProcessing() {
    079        stop = true;
    080    }
    081
    082}