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