2 * Sonitus - AbstractFilter.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.PipedInputStream;
25 import java.io.PipedOutputStream;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
30 import net.pterodactylus.sonitus.data.Controller;
31 import net.pterodactylus.sonitus.data.Filter;
32 import net.pterodactylus.sonitus.data.Metadata;
33 import net.pterodactylus.sonitus.data.event.MetadataUpdated;
35 import com.google.common.eventbus.EventBus;
36 import com.google.common.io.Closeables;
39 * Dummy {@link Filter} implementation that pipes its input to its output.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class DummyFilter implements Filter {
45 /** The name of this filter. */
46 private final String name;
49 private final EventBus eventBus;
51 /** The input stream from which to read. */
52 private InputStream inputStream;
54 /** The output stream to which to write. */
55 private OutputStream outputStream;
57 /** The current metadata. */
58 private Metadata metadata;
61 * Creates a new dummy filter with the given name.
66 * The name of the filter
68 public DummyFilter(EventBus eventBus, String name) {
69 this.eventBus = eventBus;
78 public String name() {
83 public List<Controller<?>> controllers() {
84 return Collections.emptyList();
92 public void open(Metadata metadata) throws IOException {
93 metadataUpdated(metadata);
94 inputStream = createInputStream();
95 outputStream = createOutputStream();
101 Closeables.close(outputStream, true);
102 Closeables.close(inputStream, true);
103 } catch (IOException e) {
109 public Metadata metadata() {
114 public void metadataUpdated(Metadata metadata) {
115 this.metadata = metadata;
116 eventBus.post(new MetadataUpdated(this, metadata));
120 public void process(byte[] buffer) throws IOException {
121 outputStream.write(buffer);
122 outputStream.flush();
126 public byte[] get(int bufferSize) throws IOException {
127 byte[] buffer = new byte[bufferSize];
128 int read = inputStream.read(buffer);
130 throw new EOFException();
132 return Arrays.copyOf(buffer, read);
140 * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
141 * will read the audio data. If you override this, you have to override {@link
142 * #createOutputStream()}, too!
144 * @return The input stream to read from
145 * @throws IOException
146 * if an I/O error occurs
148 protected InputStream createInputStream() throws IOException {
149 return new PipedInputStream();
153 * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
154 * will write the audio data. If you override this, you have to override {@link
155 * #createInputStream()}, too!
157 * @return The output stream to write to
158 * @throws IOException
159 * if an I/O error occurs
161 protected OutputStream createOutputStream() throws IOException {
162 return new PipedOutputStream((PipedInputStream) inputStream);