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