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