From 87230dce48aa14eec6dc2627f6b781bb20ab424f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 17 Mar 2013 15:00:36 +0100 Subject: [PATCH] =?utf8?q?Save=20a=20thread=20and=20read=20directly=20from?= =?utf8?q?=20the=20process=E2=80=99=20stdout.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sonitus/data/filter/ExternalFilter.java | 23 +++------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java index 9e298b4..6b68c28 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java @@ -21,7 +21,6 @@ import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.util.Arrays; import java.util.logging.Logger; @@ -36,7 +35,6 @@ import net.pterodactylus.sonitus.io.InputStreamDrainer; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import com.google.common.io.ByteStreams; /** * {@link Filter} implementation that runs its {@link Source} through an @@ -52,8 +50,7 @@ public abstract class ExternalFilter implements Filter { /** The source. */ private Source source; - /** The input stream that will hold the converted source. */ - private PipedInputStream pipedInputStream; + private InputStream processInputStream; // // FILTER METHODS @@ -67,7 +64,7 @@ public abstract class ExternalFilter implements Filter { @Override public byte[] get(int bufferSize) throws EOFException, IOException { byte[] buffer = new byte[bufferSize]; - int read = pipedInputStream.read(buffer); + int read = processInputStream.read(buffer); if (read == -1) { throw new EOFException(); } @@ -81,24 +78,11 @@ public abstract class ExternalFilter implements Filter { this.source = source; try { final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.builder().add(binary(source.metadata())).addAll(parameters(source.metadata())).build(), String.class)); - final InputStream processOutput = process.getInputStream(); + processInputStream = process.getInputStream(); final OutputStream processInput = process.getOutputStream(); final InputStream processError = process.getErrorStream(); final PipedOutputStream pipedOutputStream = new PipedOutputStream(); - pipedInputStream = new PipedInputStream(pipedOutputStream); new Thread(new InputStreamDrainer(processError)).start(); - new Thread(new Runnable() { - - @Override - public void run() { - try { - ByteStreams.copy(processOutput, pipedOutputStream); - } catch (IOException ioe1) { - /* okay, just exit. */ - } - logger.finest("Reading stdout finished."); - } - }).start(); new Thread(new Connection(source) { @Override @@ -115,7 +99,6 @@ public abstract class ExternalFilter implements Filter { @Override protected void finish() throws IOException { processInput.close(); - processOutput.close(); processError.close(); } }).start(); -- 2.7.4