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