Return an optional instead of throwing an exception.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / OggVorbisIdentifier.java
1 /*
2  * Sonitus - OggVorbisIdentifier.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.IOException;
21 import java.io.InputStream;
22
23 import net.pterodactylus.sonitus.data.Format;
24
25 import com.google.common.base.Optional;
26 import com.jcraft.jogg.Packet;
27 import com.jcraft.jogg.Page;
28 import com.jcraft.jogg.StreamState;
29 import com.jcraft.jogg.SyncState;
30 import com.jcraft.jorbis.Comment;
31 import com.jcraft.jorbis.Info;
32
33 /**
34  * Identifies Ogg Vorbis files.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class OggVorbisIdentifier {
39
40         /** The default size of the read buffer. */
41         private static final int BUFFER_SIZE = 4096;
42
43         /** Suppress default constructor. */
44         private OggVorbisIdentifier() {
45                 /* nothing here. */
46         }
47
48         /**
49          * Tries to parse the given stream as Ogg Vorbis file and returns a {@link
50          * Format} describing the stream.
51          *
52          * @param inputStream
53          *              The input stream to identify as Ogg Vorbis
54          * @return The identified format, or {@link com.google.common.base.Optional#absent()}
55          *         if the stream could not be identified
56          * @throws IOException
57          *              if an I/O error occurs
58          */
59         public static Optional<Format> identify(InputStream inputStream) throws IOException {
60
61                 /* stuff needed to decode Ogg. */
62                 Packet packet = new Packet();
63                 Page page = new Page();
64                 StreamState streamState = new StreamState();
65                 SyncState syncState = new SyncState();
66
67                 /* stuff needed to decode Vorbis. */
68                 Comment comment = new Comment();
69                 Info info = new Info();
70
71                 /* initialize jorbis. */
72                 syncState.init();
73                 int bufferSize = BUFFER_SIZE;
74                 int index = syncState.buffer(bufferSize);
75                 byte[] buffer = syncState.data;
76
77                 boolean streamStateInitialized = false;
78                 int packetsRead = 0;
79
80                 /* read until we have read the three packets required to decode the header. */
81                 while (packetsRead < 3) {
82                         int read = inputStream.read(buffer, index, bufferSize);
83                         syncState.wrote(read);
84                         switch (syncState.pageout(page)) {
85                                 case -1:
86                                         return Optional.absent();
87                                 case 1:
88                                         if (!streamStateInitialized) {
89                                                 /* init stream state. */
90                                                 streamState.init(page.serialno());
91                                                 streamState.reset();
92                                                 info.init();
93                                                 comment.init();
94                                                 streamStateInitialized = true;
95                                         }
96                                         if (streamState.pagein(page) == -1) {
97                                                 return Optional.absent();
98                                         }
99                                         switch (streamState.packetout(packet)) {
100                                                 case -1:
101                                                         return Optional.absent();
102                                                 case 1:
103                                                         info.synthesis_headerin(comment, packet);
104                                                         packetsRead++;
105                                                 default:
106                                                         /* continue. */
107                                         }
108
109                                 default:
110                                         /* continue. */
111                         }
112                         index = syncState.buffer(bufferSize);
113                         buffer = syncState.data;
114                 }
115
116                 return Optional.of(new Format(info.channels, info.rate, "Vorbis"));
117         }
118
119 }