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