Add name to all controlled components.
[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.Collections;
23 import java.util.List;
24 import java.util.logging.Logger;
25
26 import net.pterodactylus.sonitus.data.Controller;
27 import net.pterodactylus.sonitus.data.Metadata;
28 import net.pterodactylus.sonitus.data.Sink;
29
30 /**
31  * {@link net.pterodactylus.sonitus.data.Sink} that writes all received data
32  * into a file.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class FileSink implements Sink {
37
38         /** The logger. */
39         private static final Logger logger = Logger.getLogger(FileSink.class.getName());
40
41         /** The path of the file to write to. */
42         private final String path;
43
44         private FileOutputStream fileOutputStream;
45
46         /**
47          * Creates a new file sink that will write to the given path.
48          *
49          * @param path
50          *              The path of the file to write to
51          */
52         public FileSink(String path) {
53                 this.path = path;
54         }
55
56         //
57         // CONTROLLED METHODS
58         //
59
60         @Override
61         public String name() {
62                 return path;
63         }
64
65         @Override
66         public List<Controller<?>> controllers() {
67                 return Collections.emptyList();
68         }
69
70         //
71         // SINK METHODS
72         //
73
74         @Override
75         public void open(Metadata metadata) throws IOException {
76                 fileOutputStream = new FileOutputStream(path);
77         }
78
79         @Override
80         public void close() {
81                 try {
82                         fileOutputStream.close();
83                 } catch (IOException e) {
84                         /* ignore. */
85                 }
86         }
87
88         @Override
89         public void metadataUpdated(Metadata metadata) {
90                 /* ignore. */
91         }
92
93         @Override
94         public void process(byte[] buffer) throws IOException {
95                 fileOutputStream.write(buffer);
96                 logger.finest(String.format("FileSink: Wrote %d Bytes.", buffer.length));
97         }
98
99 }