Save a thread and read directly from the process’ stdout.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
1 /*
2  * Sonitus - ExternalFilter.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
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;
27
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;
34
35 import com.google.common.base.Preconditions;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.Iterables;
38
39 /**
40  * {@link Filter} implementation that runs its {@link Source} through an
41  * external program.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public abstract class ExternalFilter implements Filter {
46
47         /** The logger. */
48         private final Logger logger = Logger.getLogger(getClass().getName());
49
50         /** The source. */
51         private Source source;
52
53         private InputStream processInputStream;
54
55         //
56         // FILTER METHODS
57         //
58
59         @Override
60         public Metadata metadata() {
61                 return source.metadata();
62         }
63
64         @Override
65         public byte[] get(int bufferSize) throws EOFException, IOException {
66                 byte[] buffer = new byte[bufferSize];
67                 int read = processInputStream.read(buffer);
68                 if (read == -1) {
69                         throw new EOFException();
70                 }
71                 return Arrays.copyOf(buffer, read);
72         }
73
74         @Override
75         public void connect(Source source) throws ConnectException {
76                 Preconditions.checkNotNull(source, "source must not be null");
77
78                 this.source = source;
79                 try {
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) {
87
88                                 @Override
89                                 protected int bufferSize() {
90                                         return 4096;
91                                 }
92
93                                 @Override
94                                 protected void feed(byte[] buffer) throws IOException {
95                                         processInput.write(buffer);
96                                         processInput.flush();
97                                 }
98
99                                 @Override
100                                 protected void finish() throws IOException {
101                                         processInput.close();
102                                         processError.close();
103                                 }
104                         }).start();
105                 } catch (IOException ioe1) {
106
107                 }
108         }
109
110         @Override
111         public void metadataUpdated() {
112                 /* ignore. */
113         }
114
115         //
116         // SUBCLASS METHODS
117         //
118
119         /**
120          * Returns the location of the binary to execute.
121          *
122          * @param metadata
123          *              The metadata being processed
124          * @return The location of the binary to execute
125          */
126         protected abstract String binary(Metadata metadata);
127
128         /**
129          * Returns the parameters for the binary.
130          *
131          * @param metadata
132          *              The metadata being processed
133          * @return The parameters for the binary
134          */
135         protected abstract Iterable<String> parameters(Metadata metadata);
136
137 }