Skip to content

Instantly share code, notes, and snippets.

@jeffgrover
Created August 20, 2012 19:29
Show Gist options
  • Save jeffgrover/3406965 to your computer and use it in GitHub Desktop.
Save jeffgrover/3406965 to your computer and use it in GitHub Desktop.
Pushing input and capturing output and errors from an org.apache.commons.exec ExecuteStreamHandler
import org.apache.commons.exec.ExecuteStreamHandler;
import java.io.*;
class SendReceiveStreamHandler implements ExecuteStreamHandler {
private OutputStream stdin;
private InputStream stderr;
private InputStream stdout;
private BufferedWriter stdinWriter;
private Thread readOutputThread;
private Thread readErrorThread;
private boolean ready = false;
private Receiver receiver;
StringBuilder errorOutput = new StringBuilder();
public String getErrorOutput() {
return errorOutput.toString();
}
public void sendData(byte[] data) throws IOException {
while (!ready) // Make sure stream is initialized before sending
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// It's okay to interrupt
}
}
stdinWriter.write(new String(data));
stdinWriter.flush();
}
public void noMoreData() throws IOException {
if (null != stdinWriter) {
stdinWriter.flush();
stdinWriter.close();
}
}
@Override
public void setProcessInputStream(OutputStream os) throws IOException {
stdin = os;
stdinWriter = new BufferedWriter(new OutputStreamWriter(stdin));
}
@Override
public void setProcessErrorStream(InputStream is) throws IOException {
stderr = is;
}
@Override
public void setProcessOutputStream(InputStream is) throws IOException {
stdout = is;
}
@Override
public void start() {
errorOutput = new StringBuilder();
readErrorThread = new Thread(new Runnable() {
BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
public void run() {
ready = true;
String nextLine;
try {
while ((nextLine = reader.readLine()) != null) {
errorOutput.append(nextLine);
errorOutput.append('\n');
}
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errorOutput.append(sw.toString());
}
}
});
readErrorThread.start();
}
@Override
public void stop() {
ready = false;
BufferedReader reader = new BufferedReader(new InputStreamReader(stderr));
String nextLine;
try {
while ((nextLine = reader.readLine()) != null) {
errorOutput.append(nextLine);
}
} catch (IOException e) {
// It's okay, we're done processing anyway... on the way out
}
if (null != receiver)
receiver.fireTransferCompleteEvent(new TransferCompleteEvent(receiver, errorOutput.toString()));
}
public void setReceiver(Receiver receiver) {
this.receiver = receiver;
}
public void clearErrorOutput() {
errorOutput = new StringBuilder();
}
public void startReadOutputThread(int chunkSize) {
final int bufferSize = chunkSize;
readOutputThread = new Thread(new Runnable() {
InputStreamReader reader = new InputStreamReader(stdout);
char[] chunkBuffer = new char[bufferSize];
public void run() {
ready = true;
try {
int bytesRead = reader.read(chunkBuffer);
while (bytesRead > 0) {
if (null != receiver)
receiver.fireDataReceivedEvent(new DataReceivedEvent(receiver, new String(chunkBuffer).substring(0, bytesRead).getBytes()));
bytesRead = reader.read(chunkBuffer);
}
} catch (IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
errorOutput.append(sw.toString());
}
}
});
readOutputThread.start();
}
public void setErrorOutput(String output) {
errorOutput = new StringBuilder(output);
}
}
@stefan-reich
Copy link

I don't understand the stop() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment