From 6125075d3d16aa8d233382a60d3dcdfefeffc7c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 26 Aug 2015 11:19:53 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20assume=20we=20always=20have=20a?= =?utf8?q?=20magnet=20URI?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/rhynodge/states/TorrentState.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java index 48ac228..98578eb 100644 --- a/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java +++ b/src/main/java/net/pterodactylus/rhynodge/states/TorrentState.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Optional; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; @@ -271,11 +272,7 @@ public class TorrentState extends AbstractState implements Iterable */ private String generateId() { if (magnetUri != null) { - String id = extractId(magnetUri); - if (id != null) { - return id; - } - return magnetUri; + return extractId(magnetUri).orElse(magnetUri); } return (downloadUri != null) ? downloadUri : name; } @@ -291,14 +288,17 @@ public class TorrentState extends AbstractState implements Iterable * The magnet URI to extract the “xt” from * @return The extracted ID, or {@code null} if no ID could be found */ - private static String extractId(String magnetUri) { + private static Optional extractId(String magnetUri) { + if ((magnetUri == null) || (magnetUri.length() < 8)) { + return Optional.empty(); + } List parameters = URLEncodedUtils.parse(magnetUri.substring("magnet:?".length()), Charset.forName("UTF-8")); for (NameValuePair parameter : parameters) { if (parameter.getName().equals("xt")) { - return parameter.getValue().toLowerCase(); + return Optional.of(parameter.getValue().toLowerCase()); } } - return null; + return Optional.empty(); } // -- 2.7.4