+++ /dev/null
-/*
- * Sonitus - IdentifierException.java - Copyright © 2013 David Roden
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sonitus.io;
-
-import java.io.IOException;
-
-/**
- * Exception that signals that a stream could not be identified by {@link
- * IdentifyingInputStream}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class IdentifierException extends IOException {
-
- /** Creates a new identifier exception. */
- public IdentifierException() {
- super();
- }
-
- /**
- * Creates a new identifier exception.
- *
- * @param message
- * The message of the exception
- */
- public IdentifierException(String message) {
- super(message);
- }
-
- /**
- * Creates a new identifier exception.
- *
- * @param cause
- * The root cause of the exception
- */
- public IdentifierException(Throwable cause) {
- super(cause);
- }
-
- /**
- * Creates a new identifier exception.
- *
- * @param message
- * The message of the exception
- * @param cause
- * The root cause of the exception
- */
- public IdentifierException(String message, Throwable cause) {
- super(message, cause);
- }
-
-}
RememberingInputStream rememberingInputStream = new RememberingInputStream(inputStream);
/* try Ogg Vorbis first. */
- try {
- Format format = OggVorbisIdentifier.identify(rememberingInputStream);
- return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format));
- } catch (IdentifierException ie1) {
- rememberingInputStream = new RememberingInputStream(rememberingInputStream.remembered());
+ Optional<Format> format = OggVorbisIdentifier.identify(rememberingInputStream);
+ if (format.isPresent()) {
+ return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format.get()));
}
return Optional.absent();
import net.pterodactylus.sonitus.data.Format;
+import com.google.common.base.Optional;
import com.jcraft.jogg.Packet;
import com.jcraft.jogg.Page;
import com.jcraft.jogg.StreamState;
* Format} describing the stream.
*
* @param inputStream
- * @return
+ * The input stream to identify as Ogg Vorbis
+ * @return The identified format, or {@link com.google.common.base.Optional#absent()}
+ * if the stream could not be identified
* @throws IOException
+ * if an I/O error occurs
*/
- public static Format identify(InputStream inputStream) throws IOException {
+ public static Optional<Format> identify(InputStream inputStream) throws IOException {
/* stuff needed to decode Ogg. */
Packet packet = new Packet();
syncState.wrote(read);
switch (syncState.pageout(page)) {
case -1:
- throw new IdentifierException("Hole in Ogg data!");
+ return Optional.absent();
case 1:
if (!streamStateInitialized) {
/* init stream state. */
streamStateInitialized = true;
}
if (streamState.pagein(page) == -1) {
- throw new IdentifierException("Error parsing Ogg data!");
+ return Optional.absent();
}
switch (streamState.packetout(packet)) {
case -1:
- throw new IdentifierException("Error parsing Ogg data!");
+ return Optional.absent();
case 1:
info.synthesis_headerin(comment, packet);
packetsRead++;
buffer = syncState.data;
}
- return new Format(info.channels, info.rate, "Vorbis");
+ return Optional.of(new Format(info.channels, info.rate, "Vorbis"));
}
}