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