Don’t connect sources and sinks directly, use a pipeline to move data around.
[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
28 import net.pterodactylus.sonitus.data.Filter;
29 import net.pterodactylus.sonitus.data.Metadata;
30
31 import com.google.common.io.Closeables;
32
33 /**
34  * Dummy {@link Filter} implementation that pipes its input to its output.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class DummyFilter implements Filter {
39
40         /** The input stream from which to read. */
41         private InputStream inputStream;
42
43         /** The output stream to which to write. */
44         private OutputStream outputStream;
45
46         /** The current metadata. */
47         private Metadata metadata;
48
49         //
50         // FILTER METHODS
51         //
52
53         @Override
54         public void open(Metadata metadata) throws IOException {
55                 this.metadata = metadata;
56                 inputStream = createInputStream();
57                 outputStream = createOutputStream();
58         }
59
60         @Override
61         public void close() {
62                 try {
63                         Closeables.close(outputStream, true);
64                         Closeables.close(inputStream, true);
65                 } catch (IOException e) {
66                         /* won’t throw. */
67                 }
68         }
69
70         @Override
71         public Metadata metadata() {
72                 return metadata;
73         }
74
75         @Override
76         public void metadataUpdated(Metadata metadata) {
77                 this.metadata = metadata;
78         }
79
80         @Override
81         public void process(byte[] buffer) throws IOException {
82                 outputStream.write(buffer);
83                 outputStream.flush();
84         }
85
86         @Override
87         public byte[] get(int bufferSize) throws IOException {
88                 byte[] buffer = new byte[bufferSize];
89                 int read = inputStream.read(buffer);
90                 if (read == -1) {
91                         throw new EOFException();
92                 }
93                 return Arrays.copyOf(buffer, read);
94         }
95
96         //
97         // SUBCLASS METHODS
98         //
99
100         /**
101          * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
102          * will read the audio data. If you override this, you have to override {@link
103          * #createOutputStream()}, too!
104          *
105          * @return The input stream to read from
106          * @throws IOException
107          *              if an I/O error occurs
108          */
109         protected InputStream createInputStream() throws IOException {
110                 return new PipedInputStream();
111         }
112
113         /**
114          * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
115          * will write the audio data. If you override this, you have to override {@link
116          * #createInputStream()}, too!
117          *
118          * @return The output stream to write to
119          * @throws IOException
120          *              if an I/O error occurs
121          */
122         protected OutputStream createOutputStream() throws IOException {
123                 return new PipedOutputStream((PipedInputStream) inputStream);
124         }
125
126 }