X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fio%2FIdentifyingInputStream.java;h=3a4a9fb7cd139784e37db92e0b02d2e85921db0a;hb=49aade1295ee9893f2561d8d2ef0812e917087d1;hp=537c9a42cf0521a5903c0dcdf050f8dd6f6aaf93;hpb=fae468e1da8a64ae76bde4fd2f37ed7b468dc390;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java index 537c9a4..3a4a9fb 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java +++ b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java @@ -17,36 +17,38 @@ package net.pterodactylus.sonitus.io; +import java.io.EOFException; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; -import net.pterodactylus.sonitus.data.Format; +import net.pterodactylus.sonitus.data.Metadata; import com.google.common.base.Optional; +import com.google.common.io.ByteStreams; /** - * Wrapper around an {@link InputStream} that identifies the {@link Format} of + * Wrapper around an {@link InputStream} that identifies the {@link Metadata} of * the wrapped stream. * * @author David ‘Bombe’ Roden */ public class IdentifyingInputStream extends FilterInputStream { - /** The identified format. */ - private final Format format; + /** The identified metadata. */ + private final Metadata metadata; /** * Creates a new identifying input stream. * * @param inputStream * The input stream to wrap - * @param format - * The format of the stream + * @param metadata + * The metadata of the stream */ - private IdentifyingInputStream(InputStream inputStream, Format format) { + private IdentifyingInputStream(InputStream inputStream, Metadata metadata) { super(inputStream); - this.format = format; + this.metadata = metadata; } // @@ -54,12 +56,12 @@ public class IdentifyingInputStream extends FilterInputStream { // /** - * Returns the identified format. + * Returns the identified metadata. * - * @return The identified format + * @return The identified metadata */ - public Format format() { - return format; + public Metadata metadata() { + return metadata; } // @@ -72,8 +74,8 @@ public class IdentifyingInputStream extends FilterInputStream { * @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 + * the metadata it detected, or {@link Optional#absent()} if no + * metadata could be identified * @throws IOException * if an I/O error occurs */ @@ -82,12 +84,37 @@ public class IdentifyingInputStream extends FilterInputStream { /* remember everything we read here. */ RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream); - /* try Ogg Vorbis first. */ + /* first, try formats with unambiguous layouts. */ + try { + Optional metadata = FlacIdentifier.identify(rememberingInputStream); + if (metadata.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get())); + } + } catch (EOFException eofe1) { + /* ignore. */ + } + + /* try Ogg Vorbis next. */ + try { + rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); + Optional metadata = OggVorbisIdentifier.identify(rememberingInputStream); + if (metadata.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get())); + } + } catch (EOFException eofe1) { + /* ignore. */ + } + + /* finally, try MP3. */ try { - Format format = OggVorbisIdentifier.identify(rememberingInputStream); - return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format)); - } catch (IdentifierException ie1) { rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered()); + InputStream limitedInputStream = ByteStreams.limit(rememberingInputStream, 1048576); + Optional metadata = Mp3Identifier.identify(limitedInputStream); + if (metadata.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get())); + } + } catch (EOFException eofe1) { + /* ignore. */ } return Optional.absent();