Add metadata listeners to components.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / DummyFilter.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.filter;
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
30 import net.pterodactylus.sonitus.data.AbstractControlledComponent;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.Filter;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.data.event.MetadataUpdated;
35
36 import com.google.common.eventbus.EventBus;
37 import com.google.common.io.Closeables;
38
39 /**
40  * Dummy {@link Filter} implementation that pipes its input to its output.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class DummyFilter extends AbstractControlledComponent implements Filter {
45
46         /** The name of this filter. */
47         private final String name;
48
49         /** The event bus. */
50         private final EventBus eventBus;
51
52         /** The input stream from which to read. */
53         private InputStream inputStream;
54
55         /** The output stream to which to write. */
56         private OutputStream outputStream;
57
58         /** The current metadata. */
59         private Metadata metadata;
60
61         /**
62          * Creates a new dummy filter with the given name.
63          *
64          * @param eventBus
65          *              The event bus
66          * @param name
67          *              The name of the filter
68          */
69         public DummyFilter(EventBus eventBus, String name) {
70                 this.eventBus = eventBus;
71                 this.name = name;
72         }
73
74         //
75         // CONTROLLED METHODS
76         //
77
78         @Override
79         public String name() {
80                 return name;
81         }
82
83         @Override
84         public List<Controller<?>> controllers() {
85                 return Collections.emptyList();
86         }
87
88         //
89         // FILTER METHODS
90         //
91
92         @Override
93         public void open(Metadata metadata) throws IOException {
94                 metadataUpdated(metadata);
95                 inputStream = createInputStream();
96                 outputStream = createOutputStream();
97         }
98
99         @Override
100         public void close() {
101                 try {
102                         Closeables.close(outputStream, true);
103                         Closeables.close(inputStream, true);
104                 } catch (IOException e) {
105                         /* won’t throw. */
106                 }
107         }
108
109         @Override
110         public Metadata metadata() {
111                 return metadata;
112         }
113
114         @Override
115         public void metadataUpdated(Metadata metadata) {
116                 this.metadata = metadata;
117                 fireMetadataUpdated(metadata);
118                 eventBus.post(new MetadataUpdated(this, metadata));
119         }
120
121         @Override
122         public void process(byte[] buffer) throws IOException {
123                 outputStream.write(buffer);
124                 outputStream.flush();
125         }
126
127         @Override
128         public byte[] get(int bufferSize) throws IOException {
129                 byte[] buffer = new byte[bufferSize];
130                 int read = inputStream.read(buffer);
131                 if (read == -1) {
132                         throw new EOFException();
133                 }
134                 return Arrays.copyOf(buffer, read);
135         }
136
137         //
138         // SUBCLASS METHODS
139         //
140
141         /**
142          * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
143          * will read the audio data. If you override this, you have to override {@link
144          * #createOutputStream()}, too!
145          *
146          * @return The input stream to read from
147          * @throws IOException
148          *              if an I/O error occurs
149          */
150         protected InputStream createInputStream() throws IOException {
151                 return new PipedInputStream();
152         }
153
154         /**
155          * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
156          * will write the audio data. If you override this, you have to override {@link
157          * #createInputStream()}, too!
158          *
159          * @return The output stream to write to
160          * @throws IOException
161          *              if an I/O error occurs
162          */
163         protected OutputStream createOutputStream() throws IOException {
164                 return new PipedOutputStream((PipedInputStream) inputStream);
165         }
166
167 }