Initialize logger with real name of implementing class.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
index ff838d9..5106cf2 100644 (file)
@@ -46,13 +46,7 @@ import com.google.common.io.ByteStreams;
 public abstract class ExternalFilter implements Filter {
 
        /** The logger. */
-       private static final Logger logger = Logger.getLogger(ExternalFilter.class.getName());
-
-       /** The binary to execute. */
-       private final String binary;
-
-       /** The parameters for the binary. */
-       private final Iterable<String> parameters;
+       private final Logger logger = Logger.getLogger(getClass().getName());
 
        /** The format of the source. */
        private Format format;
@@ -60,19 +54,6 @@ public abstract class ExternalFilter implements Filter {
        /** 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
 
@@ -94,7 +75,7 @@ public abstract class ExternalFilter implements Filter {
 
                format = source.format();
                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(format)).addAll(parameters(format)).build(), String.class));
                        final InputStream processOutput = process.getInputStream();
                        final OutputStream processInput = process.getOutputStream();
                        final InputStream processError = process.getErrorStream();
@@ -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,6 +131,28 @@ public abstract class ExternalFilter implements Filter {
        }
 
        //
+       // SUBCLASS METHODS
+       //
+
+       /**
+        * 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);
+
+       //
        // STATIC METHODS
        //