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