faf4f01d119a7014a6c9171fc95aea786082ac01
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / IdentifyingInputStream.java
1 /*
2  * Sonitus - IdentifyingStream.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.io;
19
20 import java.io.FilterInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import net.pterodactylus.sonitus.data.Metadata;
25
26 import com.google.common.base.Optional;
27 import com.google.common.io.ByteStreams;
28
29 /**
30  * Wrapper around an {@link InputStream} that identifies the {@link Metadata} of
31  * the wrapped stream.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class IdentifyingInputStream extends FilterInputStream {
36
37         /** The identified metadata. */
38         private final Metadata metadata;
39
40         /**
41          * Creates a new identifying input stream.
42          *
43          * @param inputStream
44          *              The input stream to wrap
45          * @param metadata
46          *              The metadata of the stream
47          */
48         private IdentifyingInputStream(InputStream inputStream, Metadata metadata) {
49                 super(inputStream);
50                 this.metadata = metadata;
51         }
52
53         //
54         // ACCESSORS
55         //
56
57         /**
58          * Returns the identified metadata.
59          *
60          * @return The identified metadata
61          */
62         public Metadata metadata() {
63                 return metadata;
64         }
65
66         //
67         // STATIC METHODS
68         //
69
70         /**
71          * Tries to identify the given input stream.
72          *
73          * @param inputStream
74          *              The input stream to identify
75          * @return An identifying input stream that delivers the original stream and
76          *         the metadata it detected, or {@link Optional#absent()} if no
77          *         metadata could be identified
78          * @throws IOException
79          *              if an I/O error occurs
80          */
81         public static Optional<IdentifyingInputStream> create(InputStream inputStream) throws IOException {
82
83                 /* remember everything we read here. */
84                 RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream);
85
86                 /* try Ogg Vorbis first. */
87                 Optional<Metadata> metadata = OggVorbisIdentifier.identify(rememberingInputStream);
88                 if (metadata.isPresent()) {
89                         return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
90                 }
91
92                 /* try MP3 now. */
93                 rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
94                 InputStream limitedInputStream = ByteStreams.limit(rememberingInputStream, 1048576);
95                 metadata = Mp3Identifier.identify(limitedInputStream);
96                 if (metadata.isPresent()) {
97                         return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
98                 }
99
100                 return Optional.absent();
101         }
102
103 }