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