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