2 * Sonitus - ExternalFilter.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import java.io.EOFException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.io.PipedOutputStream;
25 import java.util.Arrays;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sonitus.data.ConnectException;
29 import net.pterodactylus.sonitus.data.Connection;
30 import net.pterodactylus.sonitus.data.Filter;
31 import net.pterodactylus.sonitus.data.Metadata;
32 import net.pterodactylus.sonitus.data.Source;
33 import net.pterodactylus.sonitus.io.InputStreamDrainer;
35 import com.google.common.base.Preconditions;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.Iterables;
40 * {@link Filter} implementation that runs its {@link Source} through an
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public abstract class ExternalFilter implements Filter {
48 private final Logger logger = Logger.getLogger(getClass().getName());
51 private Source source;
53 private InputStream processInputStream;
60 public Metadata metadata() {
61 return source.metadata();
65 public byte[] get(int bufferSize) throws EOFException, IOException {
66 byte[] buffer = new byte[bufferSize];
67 int read = processInputStream.read(buffer);
69 throw new EOFException();
71 return Arrays.copyOf(buffer, read);
75 public void connect(Source source) throws ConnectException {
76 Preconditions.checkNotNull(source, "source must not be null");
80 final Process process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(source.metadata())).addAll(parameters(source.metadata())).build(), String.class));
81 processInputStream = process.getInputStream();
82 final OutputStream processInput = process.getOutputStream();
83 final InputStream processError = process.getErrorStream();
84 final PipedOutputStream pipedOutputStream = new PipedOutputStream();
85 new Thread(new InputStreamDrainer(processError)).start();
86 new Thread(new Connection(source) {
89 protected int bufferSize() {
94 protected void feed(byte[] buffer) throws IOException {
95 processInput.write(buffer);
100 protected void finish() throws IOException {
101 processInput.close();
102 processError.close();
105 } catch (IOException ioe1) {
111 public void metadataUpdated() {
120 * Returns the location of the binary to execute.
123 * The metadata being processed
124 * @return The location of the binary to execute
126 protected abstract String binary(Metadata metadata);
129 * Returns the parameters for the binary.
132 * The metadata being processed
133 * @return The parameters for the binary
135 protected abstract Iterable<String> parameters(Metadata metadata);