Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / source / FileSource.java
1 /*
2  * Sonitus - FileSource.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.source;
19
20 import static com.google.common.base.Preconditions.checkNotNull;
21
22 import java.io.EOFException;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29
30 import net.pterodactylus.sonitus.data.AbstractFilter;
31 import net.pterodactylus.sonitus.data.Controller;
32 import net.pterodactylus.sonitus.data.Filter;
33 import net.pterodactylus.sonitus.data.Metadata;
34 import net.pterodactylus.sonitus.io.IdentifyingInputStream;
35
36 import com.google.common.base.Optional;
37
38 /**
39  * A {@link Filter} that reads a file from the local file system and does not
40  * expect any input.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class FileSource extends AbstractFilter {
45
46         /** The path of the file. */
47         private final String path;
48
49         /** The input stream. */
50         private InputStream fileInputStream;
51
52         /**
53          * Creates a new file source.
54          *
55          * @param path
56          *              The path of the file
57          * @throws java.io.IOException
58          *              if the file can not be found, or an I/O error occurs
59          */
60         public FileSource(String path) throws IOException {
61                 super(path);
62                 this.path = checkNotNull(path, "path must not be null");
63                 fileInputStream = new FileInputStream(path);
64
65                 /* identify file type. */
66                 Optional<IdentifyingInputStream> identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path));
67                 if (identifyingInputStream.isPresent()) {
68                         metadataUpdated(identifyingInputStream.get().metadata());
69                 } else {
70                         /* fallback. */
71                         metadataUpdated(new Metadata().name(path));
72                 }
73         }
74
75         //
76         // FILTER METHODS
77         //
78
79         @Override
80         public List<Controller<?>> controllers() {
81                 return Collections.emptyList();
82         }
83
84         @Override
85         public byte[] get(int bufferSize) throws IOException {
86                 byte[] buffer = new byte[bufferSize];
87                 int read = fileInputStream.read(buffer);
88                 if (read == -1) {
89                         throw new EOFException();
90                 }
91                 return Arrays.copyOf(buffer, read);
92         }
93
94         //
95         // OBJECT METHODS
96         //
97
98         @Override
99         public String toString() {
100                 return String.format("%s (%s)", path, metadata());
101         }
102
103 }