From fae468e1da8a64ae76bde4fd2f37ed7b468dc390 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Mar 2013 22:29:51 +0100 Subject: [PATCH] Add input stream that identifies its content. --- .../sonitus/io/IdentifyingInputStream.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java diff --git a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java new file mode 100644 index 0000000..537c9a4 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java @@ -0,0 +1,96 @@ +/* + * Sonitus - IdentifyingStream.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.io; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +import net.pterodactylus.sonitus.data.Format; + +import com.google.common.base.Optional; + +/** + * Wrapper around an {@link InputStream} that identifies the {@link Format} of + * the wrapped stream. + * + * @author David ‘Bombe’ Roden + */ +public class IdentifyingInputStream extends FilterInputStream { + + /** The identified format. */ + private final Format format; + + /** + * Creates a new identifying input stream. + * + * @param inputStream + * The input stream to wrap + * @param format + * The format of the stream + */ + private IdentifyingInputStream(InputStream inputStream, Format format) { + super(inputStream); + this.format = format; + } + + // + // ACCESSORS + // + + /** + * Returns the identified format. + * + * @return The identified format + */ + public Format format() { + return format; + } + + // + // STATIC METHODS + // + + /** + * Tries to identify the given input stream. + * + * @param inputStream + * The input stream to identify + * @return An identifying input stream that delivers the original stream and + * the format it detected, or {@link com.google.common.base.Optional#absent()} + * if no format could be identified + * @throws IOException + * if an I/O error occurs + */ + public static Optional create(InputStream inputStream) throws IOException { + + /* remember everything we read here. */ + RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream); + + /* try Ogg Vorbis first. */ + try { + Format format = OggVorbisIdentifier.identify(rememberingInputStream); + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format)); + } catch (IdentifierException ie1) { + rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); + } + + return Optional.absent(); + } + +} -- 2.7.4