Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / AbstractFilter.java
1 /*
2  * Sonitus - AbstractFilter.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;
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.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
31 import com.google.common.collect.Lists;
32 import com.google.common.io.Closeables;
33
34 /**
35  * Abstract {@link Filter} implementation that takes care of managing {@link
36  * MetadataListener}s and pipes its input to its output.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public abstract class AbstractFilter implements Filter {
41
42         /** The name of this filter. */
43         private final String name;
44
45         /** The list of metadata listeners. */
46         private final List<MetadataListener> metadataListeners = Lists.newCopyOnWriteArrayList();
47
48         /** The current metadata. */
49         private final AtomicReference<Metadata> metadata = new AtomicReference<Metadata>();
50
51         /** The input stream from which to read. */
52         private InputStream inputStream;
53
54         /** The output stream to which to write. */
55         private OutputStream outputStream;
56
57         /**
58          * Creates a new abstract filter.
59          *
60          * @param name
61          *              The name of the filter
62          */
63         protected AbstractFilter(String name) {
64                 this.name = name;
65         }
66
67         //
68         // LISTENER MANAGEMENT
69         //
70
71         @Override
72         public void addMetadataListener(MetadataListener metadataListener) {
73                 metadataListeners.add(metadataListener);
74         }
75
76         @Override
77         public void removeMetadataListener(MetadataListener metadataListener) {
78                 metadataListeners.remove(metadataListener);
79         }
80
81         //
82         // FILTER METHODS
83         //
84
85         @Override
86         public String name() {
87                 return name;
88         }
89
90         @Override
91         public List<Controller<?>> controllers() {
92                 return Collections.emptyList();
93         }
94
95         @Override
96         public Metadata metadata() {
97                 return metadata.get();
98         }
99
100         @Override
101         public void metadataUpdated(Metadata metadata) {
102                 if (metadata.equals(this.metadata.get())) {
103                         return;
104                 }
105                 this.metadata.set(metadata);
106                 fireMetadataUpdated(metadata);
107         }
108
109         @Override
110         public void open(Metadata metadata) throws IOException {
111                 metadataUpdated(metadata);
112                 inputStream = createInputStream();
113                 outputStream = createOutputStream();
114         }
115
116         @Override
117         public void close() {
118                 try {
119                         Closeables.close(outputStream, true);
120                         Closeables.close(inputStream, true);
121                 } catch (IOException e) {
122                         /* won’t throw. */
123                 }
124         }
125
126         @Override
127         public void process(byte[] buffer) throws IOException {
128                 outputStream.write(buffer);
129                 outputStream.flush();
130         }
131
132         @Override
133         public byte[] get(int bufferSize) throws IOException {
134                 byte[] buffer = new byte[bufferSize];
135                 int read = inputStream.read(buffer);
136                 if (read == -1) {
137                         throw new EOFException();
138                 }
139                 return Arrays.copyOf(buffer, read);
140         }
141
142         //
143         // EVENT METHODS
144         //
145
146         /**
147          * Notifies all registered metadata listeners that the metadata has changed.
148          *
149          * @param metadata
150          *              The new metadata
151          */
152         protected void fireMetadataUpdated(Metadata metadata) {
153                 for (MetadataListener metadataListener : metadataListeners) {
154                         metadataListener.metadataUpdated(this, metadata);
155                 }
156         }
157
158         //
159         // SUBCLASS METHODS
160         //
161
162         /**
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!
166          *
167          * @return The input stream to read from
168          * @throws IOException
169          *              if an I/O error occurs
170          */
171         protected InputStream createInputStream() throws IOException {
172                 return new PipedInputStream();
173         }
174
175         /**
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!
179          *
180          * @return The output stream to write to
181          * @throws IOException
182          *              if an I/O error occurs
183          */
184         protected OutputStream createOutputStream() throws IOException {
185                 return new PipedOutputStream((PipedInputStream) inputStream);
186         }
187
188 }