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