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