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