org.etlunit.io.ModifiedPumpStreamHandler Maven / Gradle / Ivy
package org.etlunit.io;
import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.PumpStreamHandler;
import java.io.*;
import java.util.concurrent.Semaphore;
public class ModifiedPumpStreamHandler implements ExecuteStreamHandler
{
private final PumpStreamHandler pumpStreamHandler;
private final Semaphore streamCounter = new Semaphore(3);
/**
* These two pipes are for process clients to write output into the input stream of the
* external process.
*/
//private final PipedOutputStream processStandardInputWriter = new PipedOutputStream();
//private final PipedInputStream processStandardInputReader;
/**
* These two streams are for process clients to read the standard out / err streams
* from the external process.
*/
private final PipedOutputStream processStandardOutputWriter = new PipedOutputStream();
private final PipedInputStream processStandardOutputReader;
private OutputStream processInputStream;
private Writer writer;
private final Reader reader;
private final NfurcatingOutputStream outputAggregator;
public ModifiedPumpStreamHandler() throws IOException
{
this(null);
}
public ModifiedPumpStreamHandler(File output) throws IOException
{
processStandardOutputReader = new PipedInputStream(processStandardOutputWriter, 65536);
//processStandardInputReader = new PipedInputStream(processStandardInputWriter, 65536);
OutputStream os = processStandardOutputWriter;
if (output != null)
{
if (output.exists() && !output.canWrite())
{
throw new IOException("Target file not accessible: " + output.getAbsolutePath());
}
BufferedOutputStream osBuff = new BufferedOutputStream(new FileOutputStream(output));
outputAggregator = new NfurcatingOutputStream(osBuff);
os = outputAggregator.bifurcate(processStandardOutputWriter);
}
else
{
outputAggregator = null;
}
pumpStreamHandler = new CustomPumpStreamHandler(
os//,
//processStandardInputReader
);
//writer = new OutputStreamWriter(new BifurcatingOutputStream(processStandardInputWriter, System.out));
reader = new InputStreamReader(processStandardOutputReader);
// start off with all 3 permits taken. Each stream injection will release one
streamCounter.acquireUninterruptibly(3);
}
public InputStream getProcessOutputStream()
{
return processStandardOutputReader;
}
public OutputStream getProcessInputStream()
{
return processInputStream;
}
public Writer getProcessInput()
{
return writer;
}
public Reader getProcessOutput()
{
return reader;
}
public void setProcessOutputStream(InputStream is)
{
pumpStreamHandler.setProcessOutputStream(is);
streamCounter.release();
}
public void stop()
{
pumpStreamHandler.stop();
if (outputAggregator != null)
{
try
{
outputAggregator.close();
}
catch (IOException e)
{
throw new IllegalArgumentException(e);
}
}
}
public void start()
{
pumpStreamHandler.start();
}
public void setProcessInputStream(OutputStream os) throws IOException
{
if (outputAggregator != null)
{
processInputStream = outputAggregator.bifurcate(os);
}
else
{
processInputStream = os;
}
writer = new OutputStreamWriter(processInputStream);
//pumpStreamHandler.setProcessInputStream(os);
streamCounter.release();
}
public void setProcessErrorStream(InputStream is)
{
pumpStreamHandler.setProcessErrorStream(is);
streamCounter.release();
}
public void waitForStreams()
{
streamCounter.acquireUninterruptibly(3);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy