X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2Ffilter%2FExternalFilter.java;h=a482cf62d7dd50db6a88275a40e8c38063abf6c4;hb=633a841142f978235ed9f745b6ba16c278963e62;hp=fa3a2d8d39c87eaa39a1417098b6ad53b53ce1b5;hpb=631032005069a2013cc80bf5471d2a6c6eccd065;p=sonitus.git 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 fa3a2d8..a482cf6 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java +++ b/src/main/java/net/pterodactylus/sonitus/data/filter/ExternalFilter.java @@ -17,117 +17,68 @@ package net.pterodactylus.sonitus.data.filter; -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; -import net.pterodactylus.sonitus.data.ConnectException; -import net.pterodactylus.sonitus.data.Connection; +import net.pterodactylus.sonitus.data.AbstractFilter; import net.pterodactylus.sonitus.data.Filter; -import net.pterodactylus.sonitus.data.Format; -import net.pterodactylus.sonitus.data.Source; +import net.pterodactylus.sonitus.data.Metadata; +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 - * external program. + * {@link net.pterodactylus.sonitus.data.Filter} implementation that runs its + * {@link net.pterodactylus.sonitus.data.Source} through an external program. * * @author David ‘Bombe’ Roden */ -public abstract class ExternalFilter implements Filter { +public abstract class ExternalFilter extends AbstractFilter implements Filter { /** The logger. */ - private static final Logger logger = Logger.getLogger(ExternalFilter.class.getName()); + private final Logger logger = Logger.getLogger(getClass().getName()); - /** The format of the source. */ - private Format format; + /** The external process. */ + private Process process; - /** The input stream that will hold the converted source. */ - private PipedInputStream pipedInputStream; + /** + * Creates a new external filter with the given name. + * + * @param name + * The name of the filter + */ + protected ExternalFilter(String name) { + super(name); + } // // FILTER METHODS + // @Override - public Format format() { - return format; + public void open(Metadata metadata) throws IOException { + process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.builder().add(binary(metadata)).addAll(parameters(metadata)).build(), String.class)); + InputStream processError = process.getErrorStream(); + new Thread(new InputStreamDrainer(processError)).start(); + super.open(metadata); } @Override - public byte[] get(int bufferSize) throws EOFException, IOException { - byte[] buffer = new byte[bufferSize]; - int read = pipedInputStream.read(buffer); - return Arrays.copyOf(buffer, read); + public void close() { + process.destroy(); } @Override - public void connect(Source source) throws ConnectException { - Preconditions.checkNotNull(source, "source must not be null"); - - format = source.format(); - try { - final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.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(); - 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 Runnable() { - - @Override - public void run() { - try { - ByteStreams.copy(processOutput, pipedOutputStream); - } catch (IOException ioe1) { - /* okay, just exit. */ - } - logger.finest("ExternalFilter: Reading stdout finished."); - } - }).start(); - new Thread(new Connection(source) { - - @Override - protected int bufferSize() { - return 4096; - } - - @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) { - - } + protected InputStream createInputStream() throws IOException { + return process.getInputStream(); + } + + @Override + protected OutputStream createOutputStream() throws IOException { + return process.getOutputStream(); } // @@ -137,32 +88,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 parameters(Format format); - - // - // STATIC 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. */ - } - } + protected abstract Iterable parameters(Metadata metadata); }