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