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;
30 import java.util.logging.Logger;
32 import com.google.common.collect.Lists;
33 import com.google.common.io.Closeables;
36 * Abstract {@link Filter} implementation that takes care of managing {@link
37 * MetadataListener}s and pipes its input to its output.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public abstract class AbstractFilter implements Filter {
44 private static final Logger logger = Logger.getLogger(AbstractFilter.class.getName());
46 /** The name of this filter. */
47 private final String name;
49 /** The list of metadata listeners. */
50 private final List<MetadataListener> metadataListeners = Lists.newCopyOnWriteArrayList();
52 /** The current metadata. */
53 private final AtomicReference<Metadata> metadata = new AtomicReference<Metadata>();
55 /** The input stream from which to read. */
56 private InputStream inputStream;
58 /** The output stream to which to write. */
59 private OutputStream outputStream;
62 * Creates a new abstract filter.
65 * The name of the filter
67 protected AbstractFilter(String name) {
72 // LISTENER MANAGEMENT
76 public void addMetadataListener(MetadataListener metadataListener) {
77 metadataListeners.add(metadataListener);
81 public void removeMetadataListener(MetadataListener metadataListener) {
82 metadataListeners.remove(metadataListener);
90 public String name() {
95 public List<Controller<?>> controllers() {
96 return Collections.emptyList();
100 public Metadata metadata() {
101 return metadata.get();
105 public void metadataUpdated(Metadata metadata) {
106 if (metadata.equals(this.metadata.get())) {
109 this.metadata.set(metadata);
110 fireMetadataUpdated(metadata);
114 public void open(Metadata metadata) throws IOException {
115 metadataUpdated(metadata);
116 inputStream = createInputStream();
117 outputStream = createOutputStream();
121 public void close() {
123 Closeables.close(outputStream, true);
124 Closeables.close(inputStream, true);
125 } catch (IOException e) {
131 public void process(DataPacket dataPacket) throws IOException {
132 if (dataPacket.metadata().isPresent() && !dataPacket.metadata().get().equalsIgnoreComment(this.metadata.get())) {
133 metadataUpdated(dataPacket.metadata().get());
135 logger.finest(String.format("Writing %d bytes to %s...", dataPacket.buffer().length, name()));
136 outputStream.write(dataPacket.buffer());
137 outputStream.flush();
141 public DataPacket get(int bufferSize) throws IOException {
142 byte[] buffer = new byte[bufferSize];
143 int read = inputStream.read(buffer);
145 throw new EOFException();
147 return new DataPacket(metadata(), Arrays.copyOf(buffer, read));
155 * Notifies all registered metadata listeners that the metadata has changed.
160 protected void fireMetadataUpdated(Metadata metadata) {
161 for (MetadataListener metadataListener : metadataListeners) {
162 metadataListener.metadataUpdated(this, metadata);
171 * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
172 * will read the audio data. If you override this, you have to override {@link
173 * #createOutputStream()}, too!
175 * @return The input stream to read from
176 * @throws IOException
177 * if an I/O error occurs
179 protected InputStream createInputStream() throws IOException {
180 return new PipedInputStream();
184 * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
185 * will write the audio data. If you override this, you have to override {@link
186 * #createInputStream()}, too!
188 * @return The output stream to write to
189 * @throws IOException
190 * if an I/O error occurs
192 protected OutputStream createOutputStream() throws IOException {
193 return new PipedOutputStream((PipedInputStream) inputStream);