From d0f12db4a8fe02601743e4b23618d44e193fd474 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Mar 2013 22:40:24 +0100 Subject: [PATCH] Return an optional instead of throwing an exception. --- .../sonitus/io/IdentifierException.java | 67 ---------------------- .../sonitus/io/IdentifyingInputStream.java | 8 +-- .../sonitus/io/OggVorbisIdentifier.java | 16 ++++-- 3 files changed, 13 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sonitus/io/IdentifierException.java diff --git a/src/main/java/net/pterodactylus/sonitus/io/IdentifierException.java b/src/main/java/net/pterodactylus/sonitus/io/IdentifierException.java deleted file mode 100644 index e6c03f6..0000000 --- a/src/main/java/net/pterodactylus/sonitus/io/IdentifierException.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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 . - */ - -package net.pterodactylus.sonitus.io; - -import java.io.IOException; - -/** - * Exception that signals that a stream could not be identified by {@link - * IdentifyingInputStream}. - * - * @author David ‘Bombe’ Roden - */ -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); - } - -} diff --git a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java index 537c9a4..7cc69b0 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java +++ b/src/main/java/net/pterodactylus/sonitus/io/IdentifyingInputStream.java @@ -83,11 +83,9 @@ public class IdentifyingInputStream extends FilterInputStream { 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 = OggVorbisIdentifier.identify(rememberingInputStream); + if (format.isPresent()) { + return Optional.of(new IdentifyingInputStream(rememberingInputStream.remembered(), format.get())); } return Optional.absent(); diff --git a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java index ab732b8..d889b97 100644 --- a/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java +++ b/src/main/java/net/pterodactylus/sonitus/io/OggVorbisIdentifier.java @@ -22,6 +22,7 @@ import java.io.InputStream; 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; @@ -49,10 +50,13 @@ public class OggVorbisIdentifier { * 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 identify(InputStream inputStream) throws IOException { /* stuff needed to decode Ogg. */ Packet packet = new Packet(); @@ -79,7 +83,7 @@ public class OggVorbisIdentifier { 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. */ @@ -90,11 +94,11 @@ public class OggVorbisIdentifier { 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++; @@ -109,7 +113,7 @@ public class OggVorbisIdentifier { buffer = syncState.data; } - return new Format(info.channels, info.rate, "Vorbis"); + return Optional.of(new Format(info.channels, info.rate, "Vorbis")); } } -- 2.7.4