Update javadocs.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / sink / FileSink.java
1 /*
2  * Sonitus - FileSink.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.sink;
19
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.logging.Logger;
23
24 import net.pterodactylus.sonitus.data.AbstractFilter;
25 import net.pterodactylus.sonitus.data.DataPacket;
26 import net.pterodactylus.sonitus.data.Filter;
27 import net.pterodactylus.sonitus.data.Metadata;
28
29 /**
30  * {@link Filter} that writes all received data into a file.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class FileSink extends AbstractFilter {
35
36         /** The logger. */
37         private static final Logger logger = Logger.getLogger(FileSink.class.getName());
38
39         /** The path of the file to write to. */
40         private final String path;
41
42         /** The output stream writing to the file. */
43         private FileOutputStream fileOutputStream;
44
45         /**
46          * Creates a new file sink that will write to the given path.
47          *
48          * @param path
49          *              The path of the file to write to
50          */
51         public FileSink(String path) {
52                 super(path);
53                 this.path = path;
54         }
55
56         //
57         // FILTER METHODS
58         //
59
60         @Override
61         public void open(Metadata metadata) throws IOException {
62                 fileOutputStream = new FileOutputStream(path);
63                 metadataUpdated(metadata);
64         }
65
66         @Override
67         public void close() {
68                 try {
69                         fileOutputStream.close();
70                 } catch (IOException e) {
71                         /* ignore. */
72                 }
73         }
74
75         @Override
76         public void process(DataPacket dataPacket) throws IOException {
77                 fileOutputStream.write(dataPacket.buffer());
78                 logger.finest(String.format("FileSink: Wrote %d Bytes.", dataPacket.buffer().length));
79         }
80
81 }