2 * Sonitus - IdentifyingStream.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sonitus.io;
20 import java.io.EOFException;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
25 import net.pterodactylus.sonitus.data.Metadata;
27 import com.google.common.base.Optional;
28 import com.google.common.io.ByteStreams;
31 * Wrapper around an {@link InputStream} that identifies the {@link Metadata} of
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class IdentifyingInputStream extends FilterInputStream {
38 /** The identified metadata. */
39 private final Metadata metadata;
42 * Creates a new identifying input stream.
45 * The input stream to wrap
47 * The metadata of the stream
49 private IdentifyingInputStream(InputStream inputStream, Metadata metadata) {
51 this.metadata = metadata;
59 * Returns the identified metadata.
61 * @return The identified metadata
63 public Metadata metadata() {
72 * Tries to identify the given input stream.
75 * The input stream to identify
76 * @return An identifying input stream that delivers the original stream and
77 * the metadata it detected, or {@link Optional#absent()} if no
78 * metadata could be identified
80 * if an I/O error occurs
82 public static Optional<IdentifyingInputStream> create(InputStream inputStream) throws IOException {
84 /* remember everything we read here. */
85 RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream);
87 /* first, try formats with unambiguous layouts. */
89 Optional<Metadata> metadata = FlacIdentifier.identify(rememberingInputStream);
90 if (metadata.isPresent()) {
91 return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
93 } catch (EOFException eofe1) {
97 /* try Ogg Vorbis next. */
99 rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
100 Optional<Metadata> metadata = OggVorbisIdentifier.identify(rememberingInputStream);
101 if (metadata.isPresent()) {
102 return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
104 } catch (EOFException eofe1) {
108 /* finally, try MP3. */
110 rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
111 InputStream limitedInputStream = ByteStreams.limit(rememberingInputStream, 1048576);
112 Optional<Metadata> metadata = Mp3Identifier.identify(limitedInputStream);
113 if (metadata.isPresent()) {
114 return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
116 } catch (EOFException eofe1) {
120 return Optional.absent();