/******************************************************************************* * ALMA - Atacama Large Millimeter Array * Copyright (c) ESO - European Southern Observatory, 2011 * (in the framework of the ALMA collaboration). * All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *******************************************************************************/ package alma.acs.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * Continuously reads the stdout and stderr streams of {@link Process} in separate threads, * so that the OS will not block out the JVM, as it could otherwise happen if lines are read from these streams sequentially in one thread. *
* This class is only useful if the external process's output is either not interesting at all for the user, * or if the output should be read only after the process has terminated. * Otherwise the streams should be read directly without using this class. *
* The {@link #gobble(long, TimeUnit)} call returns either when both stdout and stderr streams deliver a null,
* or if the timeout is reached. @TODO check if relying on the null from both streams is
* as robust as {@link Process#waitFor()}.
*
* @author hsommer
*/
public class ProcessStreamGobbler
{
private final Process proc;
private final List
* Use this method if you do not want to wait for the process to end.
* The status can anyway be checked using {@link #hasTerminated()}.
*/
public void gobbleAsync() {
try {
gobble(-1, null);
} catch (InterruptedException ex) {
// this should never happen, since InterruptedException is only thrown while we would wait for the external process, which here we don't.
ex.printStackTrace();
}
}
/**
* Starts fetching process output from stdout/stderr, storing it internally
* so that it can later be read via {@link #getStdout()} and {@link #getStderr()}.
*
* Use this method if you want to wait for the process to end, limited by a timeout.
*
* @TODO: set max sizes for the buffers
*
* @param timeout maximum time to wait for the process to finish
* @param unit unit for the timeout
* @return true
if returning because the process ended,
* otherwise false
if the timeout applied (in which case the streams will continue to be read though).
* @throws InterruptedException
*/
public boolean gobble(long timeout, TimeUnit unit) throws InterruptedException {
ExecutorService exsrv = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue