Add method to expose a source’s metadata.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
index ff838d9..3bfb774 100644 (file)
@@ -30,7 +30,9 @@ 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;
@@ -46,45 +48,35 @@ import com.google.common.io.ByteStreams;
 public abstract class ExternalFilter implements Filter {
 
        /** The logger. */
-       private static final Logger logger = Logger.getLogger(ExternalFilter.class.getName());
+       private final Logger logger = Logger.getLogger(getClass().getName());
 
-       /** The binary to execute. */
-       private final String binary;
-
-       /** The parameters for the binary. */
-       private final Iterable<String> parameters;
-
-       /** 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;
 
-       /**
-        * Creates a new external filter.
-        *
-        * @param binary
-        *              The binary to execute
-        * @param parameters
-        *              The parameter for the binary
-        */
-       public ExternalFilter(String binary, Iterable<String> parameters) {
-               this.binary = binary;
-               this.parameters = parameters;
-       }
-
        //
        // FILTER METHODS
+       //
 
        @Override
        public Format format() {
-               return format;
+               return source.format();
+       }
+
+       @Override
+       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);
+               if (read == -1) {
+                       throw new EOFException();
+               }
                return Arrays.copyOf(buffer, read);
        }
 
@@ -92,26 +84,15 @@ 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 {
-                       Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary).addAll(parameters).build(), String.class));
+                       final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(source.format())).addAll(parameters(source.format())).build(), String.class));
                        final InputStream processOutput = process.getInputStream();
                        final OutputStream processInput = process.getOutputStream();
                        final InputStream processError = process.getErrorStream();
                        final PipedOutputStream pipedOutputStream = new PipedOutputStream();
                        pipedInputStream = new PipedInputStream(pipedOutputStream);
-                       new Thread(new Runnable() {
-
-                               @Override
-                               public void run() {
-                                       try {
-                                               drainInputStream(processError);
-                                       } catch (IOException ioe1) {
-                                               /* ignore, just let the thread exit. */
-                                       }
-                                       logger.finest("ExternalFilter: Reading stderr finished.");
-                               }
-                       }).start();
+                       new Thread(new InputStreamDrainer(processError)).start();
                        new Thread(new Runnable() {
 
                                @Override
@@ -121,7 +102,7 @@ public abstract class ExternalFilter implements Filter {
                                        } catch (IOException ioe1) {
                                                /* okay, just exit. */
                                        }
-                                       logger.finest("ExternalFilter: Reading stdout finished.");
+                                       logger.finest("Reading stdout finished.");
                                }
                        }).start();
                        new Thread(new Connection(source) {
@@ -134,6 +115,14 @@ public abstract class ExternalFilter implements Filter {
                                @Override
                                protected void feed(byte[] buffer) throws IOException {
                                        processInput.write(buffer);
+                                       processInput.flush();
+                               }
+
+                               @Override
+                               protected void finish() throws IOException {
+                                       processInput.close();
+                                       processOutput.close();
+                                       processError.close();
                                }
                        }).start();
                } catch (IOException ioe1) {
@@ -142,16 +131,25 @@ public abstract class ExternalFilter implements Filter {
        }
 
        //
-       // STATIC METHODS
+       // SUBCLASS METHODS
        //
 
-       private static void drainInputStream(InputStream inputStream) throws IOException {
-               byte[] buffer = new byte[4096];
-               int read;
-               while ((read = inputStream.read(buffer)) != -1) {
-                       logger.finest(String.format("ExternalFilter: Drained %d Bytes.", read));
-                       /* do nothing, just read the damn thing. */
-               }
-       }
+       /**
+        * Returns the location of the binary to execute.
+        *
+        * @param format
+        *              The format being processed
+        * @return The location of the binary to execute
+        */
+       protected abstract String binary(Format format);
+
+       /**
+        * Returns the parameters for the binary.
+        *
+        * @param format
+        *              The format being processed
+        * @return The parameters for the binary
+        */
+       protected abstract Iterable<String> parameters(Format format);
 
 }