Rename dummy filter to basic filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / BasicFilter.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.AbstractControlledComponent;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.Filter;
33 import net.pterodactylus.sonitus.data.Metadata;
34
35 import com.google.common.io.Closeables;
36
37 /**
38  * Basic {@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 BasicFilter extends AbstractControlledComponent implements Filter {
43
44         /** The input stream from which to read. */
45         private InputStream inputStream;
46
47         /** The output stream to which to write. */
48         private OutputStream outputStream;
49
50         /**
51          * Creates a new dummy filter with the given name.
52          *
53          * @param name
54          *              The name of the filter
55          */
56         public BasicFilter(String name) {
57                 super(name);
58         }
59
60         //
61         // CONTROLLED METHODS
62         //
63
64         @Override
65         public List<Controller<?>> controllers() {
66                 return Collections.emptyList();
67         }
68
69         //
70         // FILTER METHODS
71         //
72
73         @Override
74         public void open(Metadata metadata) throws IOException {
75                 metadataUpdated(metadata);
76                 inputStream = createInputStream();
77                 outputStream = createOutputStream();
78         }
79
80         @Override
81         public void close() {
82                 try {
83                         Closeables.close(outputStream, true);
84                         Closeables.close(inputStream, true);
85                 } catch (IOException e) {
86                         /* won’t throw. */
87                 }
88         }
89
90         @Override
91         public void process(byte[] buffer) throws IOException {
92                 outputStream.write(buffer);
93                 outputStream.flush();
94         }
95
96         @Override
97         public byte[] get(int bufferSize) throws IOException {
98                 byte[] buffer = new byte[bufferSize];
99                 int read = inputStream.read(buffer);
100                 if (read == -1) {
101                         throw new EOFException();
102                 }
103                 return Arrays.copyOf(buffer, read);
104         }
105
106         //
107         // SUBCLASS METHODS
108         //
109
110         /**
111          * Creates the input stream from which {@link net.pterodactylus.sonitus.data.Pipeline}
112          * will read the audio data. If you override this, you have to override {@link
113          * #createOutputStream()}, too!
114          *
115          * @return The input stream to read from
116          * @throws IOException
117          *              if an I/O error occurs
118          */
119         protected InputStream createInputStream() throws IOException {
120                 return new PipedInputStream();
121         }
122
123         /**
124          * Creates the output stream to which {@link net.pterodactylus.sonitus.data.Pipeline}
125          * will write the audio data. If you override this, you have to override {@link
126          * #createInputStream()}, too!
127          *
128          * @return The output stream to write to
129          * @throws IOException
130          *              if an I/O error occurs
131          */
132         protected OutputStream createOutputStream() throws IOException {
133                 return new PipedOutputStream((PipedInputStream) inputStream);
134         }
135
136 }