Add FLAC identifier and fix comments.
[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.EOFException;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import net.pterodactylus.sonitus.data.Metadata;
26
27 import com.google.common.base.Optional;
28 import com.google.common.io.ByteStreams;
29
30 /**
31  * Wrapper around an {@link InputStream} that identifies the {@link Metadata} of
32  * the wrapped stream.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class IdentifyingInputStream extends FilterInputStream {
37
38         /** The identified metadata. */
39         private final Metadata metadata;
40
41         /**
42          * Creates a new identifying input stream.
43          *
44          * @param inputStream
45          *              The input stream to wrap
46          * @param metadata
47          *              The metadata of the stream
48          */
49         private IdentifyingInputStream(InputStream inputStream, Metadata metadata) {
50                 super(inputStream);
51                 this.metadata = metadata;
52         }
53
54         //
55         // ACCESSORS
56         //
57
58         /**
59          * Returns the identified metadata.
60          *
61          * @return The identified metadata
62          */
63         public Metadata metadata() {
64                 return metadata;
65         }
66
67         //
68         // STATIC METHODS
69         //
70
71         /**
72          * Tries to identify the given input stream.
73          *
74          * @param inputStream
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
79          * @throws IOException
80          *              if an I/O error occurs
81          */
82         public static Optional<IdentifyingInputStream> create(InputStream inputStream) throws IOException {
83
84                 /* remember everything we read here. */
85                 RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream);
86
87                 /* first, try formats with unambiguous layouts. */
88                 try {
89                         Optional<Metadata> metadata = FlacIdentifier.identify(rememberingInputStream);
90                         if (metadata.isPresent()) {
91                                 return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), metadata.get()));
92                         }
93                 } catch (EOFException eofe1) {
94                         /* ignore. */
95                 }
96
97                 /* try Ogg Vorbis next. */
98                 try {
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()));
103                         }
104                 } catch (EOFException eofe1) {
105                         /* ignore. */
106                 }
107
108                 /* finally, try MP3. */
109                 try {
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()));
115                         }
116                 } catch (EOFException eofe1) {
117                         /* ignore. */
118                 }
119
120                 return Optional.absent();
121         }
122
123 }