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;
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;
29 import java.util.concurrent.atomic.AtomicReference;
31 import com.google.common.collect.Lists;
32 import com.google.common.io.Closeables;
35 * Abstract {@link Filter} implementation that takes care of managing {@link
36 * MetadataListener}s and pipes its input to its output.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public abstract class AbstractFilter implements Filter {
42 /** The name of this filter. */
43 private final String name;
45 /** The list of metadata listeners. */
46 private final List<MetadataListener> metadataListeners = Lists.newCopyOnWriteArrayList();
48 /** The current metadata. */
49 private final AtomicReference<Metadata> metadata = new AtomicReference<Metadata>();
51 /** The input stream from which to read. */
52 private InputStream inputStream;
54 /** The output stream to which to write. */
55 private OutputStream outputStream;
58 * Creates a new abstract filter.
61 * The name of the filter
63 protected AbstractFilter(String name) {
68 // LISTENER MANAGEMENT
72 public void addMetadataListener(MetadataListener metadataListener) {
73 metadataListeners.add(metadataListener);
77 public void removeMetadataListener(MetadataListener metadataListener) {
78 metadataListeners.remove(metadataListener);
86 public String name() {
91 public List<Controller<?>> controllers() {
92 return Collections.emptyList();
96 public Metadata metadata() {
97 return metadata.get();
101 public void metadataUpdated(Metadata metadata) {
102 if (metadata.equals(this.metadata.get())) {
105 this.metadata.set(metadata);
106 fireMetadataUpdated(metadata);
110 public void open(Metadata metadata) throws IOException {
111 metadataUpdated(metadata);
112 inputStream = createInputStream();
113 outputStream = createOutputStream();
117 public void close() {
119 Closeables.close(outputStream, true);
120 Closeables.close(inputStream, true);
121 } catch (IOException e) {
127 public void process(byte[] buffer) throws IOException {
128 outputStream.write(buffer);
129 outputStream.flush();
133 public byte[] get(int bufferSize) throws IOException {
134 byte[] buffer = new byte[bufferSize];
135 int read = inputStream.read(buffer);
137 throw new EOFException();
139 return Arrays.copyOf(buffer, read);
147 * Notifies all registered metadata listeners that the metadata has changed.
152 protected void fireMetadataUpdated(Metadata metadata) {
153 for (MetadataListener metadataListener : metadataListeners) {
154 metadataListener.metadataUpdated(this, metadata);
163 * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
164 * will read the audio data. If you override this, you have to override {@link
165 * #createOutputStream()}, too!
167 * @return The input stream to read from
168 * @throws IOException
169 * if an I/O error occurs
171 protected InputStream createInputStream() throws IOException {
172 return new PipedInputStream();
176 * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
177 * will write the audio data. If you override this, you have to override {@link
178 * #createInputStream()}, too!
180 * @return The output stream to write to
181 * @throws IOException
182 * if an I/O error occurs
184 protected OutputStream createOutputStream() throws IOException {
185 return new PipedOutputStream((PipedInputStream) inputStream);