From: David ‘Bombe’ Roden Date: Fri, 15 Mar 2013 05:58:48 +0000 (+0100) Subject: Add file source. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b74bb6374b5b3f4d0f30fefe8771e9c48ceead13;hp=d33602f1921d7c12833cdfd144f7207de0fcc67b;p=sonitus.git Add file source. --- diff --git a/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java new file mode 100644 index 0000000..cc61ce0 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/data/source/FileSource.java @@ -0,0 +1,104 @@ +/* + * Sonitus - FileSource.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.data.source; + +import static com.google.common.base.Preconditions.*; +import static net.pterodactylus.sonitus.data.Format.UNKNOWN_CHANNELS; +import static net.pterodactylus.sonitus.data.Format.UNKNOWN_ENCODING; +import static net.pterodactylus.sonitus.data.Format.UNKNOWN_FREQUENCY; + +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Source; +import net.pterodactylus.sonitus.io.IdentifyingInputStream; + +import com.google.common.base.Optional; +import com.google.common.io.ByteStreams; + +/** + * A {@link Source} that is read from the local file system. + * + * @author David ‘Bombe’ Roden + */ +public class FileSource implements Source { + + /** The path of the file. */ + private final String path; + + /** The identified format of the file. */ + private final Format format; + + /** The input stream. */ + private InputStream fileInputStream; + + /** + * Creates a new file source. + * + * @param path + * The path of the file + * @throws IOException + * if the file can not be found, or an I/O error occurs + */ + public FileSource(String path) throws IOException { + this.path = checkNotNull(path, "path must not be null"); + fileInputStream = new FileInputStream(path); + + /* identify file type. */ + Optional identifyingInputStream = IdentifyingInputStream.create(new FileInputStream(path)); + if (identifyingInputStream.isPresent()) { + format = identifyingInputStream.get().format(); + } else { + /* fallback. */ + format = new Format(UNKNOWN_CHANNELS, UNKNOWN_FREQUENCY, UNKNOWN_ENCODING); + } + } + + // + // SOURCE METHODS + // + + @Override + public Format format() { + return format; + } + + @Override + public byte[] get(int bufferSize) throws IOException { + byte[] buffer = new byte[bufferSize]; + int read = ByteStreams.read(fileInputStream, buffer, 0, bufferSize); + if (read == 0) { + throw new EOFException(); + } + return Arrays.copyOf(buffer, read); + } + + // + // OBJECT METHODS + // + + @Override + public String toString() { + return String.format("%s (%s)", path, format); + } + +}