Save a thread and read directly from the process’ stdout.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
index 497ba2a..6b68c28 100644 (file)
@@ -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;
@@ -29,14 +28,13 @@ import java.util.logging.Logger;
 import net.pterodactylus.sonitus.data.ConnectException;
 import net.pterodactylus.sonitus.data.Connection;
 import net.pterodactylus.sonitus.data.Filter;
-import net.pterodactylus.sonitus.data.Format;
+import net.pterodactylus.sonitus.data.Metadata;
 import net.pterodactylus.sonitus.data.Source;
 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
@@ -49,25 +47,24 @@ public abstract class ExternalFilter implements Filter {
        /** The logger. */
        private final Logger logger = Logger.getLogger(getClass().getName());
 
-       /** The format of the source. */
-       private Format format;
+       /** The source. */
+       private Source source;
 
-       /** The input stream that will hold the converted source. */
-       private PipedInputStream pipedInputStream;
+       private InputStream processInputStream;
 
        //
        // FILTER METHODS
        //
 
        @Override
-       public Format format() {
-               return format;
+       public Metadata metadata() {
+               return source.metadata();
        }
 
        @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();
                }
@@ -78,27 +75,14 @@ public abstract class ExternalFilter implements Filter {
        public void connect(Source source) throws ConnectException {
                Preconditions.checkNotNull(source, "source must not be null");
 
-               format = source.format();
+               this.source = source;
                try {
-                       final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(format)).addAll(parameters(format)).build(), String.class));
-                       final InputStream processOutput = process.getInputStream();
+                       final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(source.metadata())).addAll(parameters(source.metadata())).build(), String.class));
+                       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();
@@ -124,6 +107,11 @@ public abstract class ExternalFilter implements Filter {
                }
        }
 
+       @Override
+       public void metadataUpdated() {
+               /* ignore. */
+       }
+
        //
        // SUBCLASS METHODS
        //
@@ -131,19 +119,19 @@ public abstract class ExternalFilter implements Filter {
        /**
         * Returns the location of the binary to execute.
         *
-        * @param format
-        *              The format being processed
+        * @param metadata
+        *              The metadata being processed
         * @return The location of the binary to execute
         */
-       protected abstract String binary(Format format);
+       protected abstract String binary(Metadata metadata);
 
        /**
         * Returns the parameters for the binary.
         *
-        * @param format
-        *              The format being processed
+        * @param metadata
+        *              The metadata being processed
         * @return The parameters for the binary
         */
-       protected abstract Iterable<String> parameters(Format format);
+       protected abstract Iterable<String> parameters(Metadata metadata);
 
 }