From 13dfa6b0fe6538ec5d67db22da8fa05bd6af0af9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 20 Jul 2015 18:53:09 +0200 Subject: [PATCH] Prevent potential NullPointerException --- src/main/java/net/pterodactylus/sone/web/UploadImagePage.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 30a5890..a1e987a 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web; +import static com.google.common.base.Optional.fromNullable; import static java.util.logging.Logger.getLogger; import java.awt.Image; @@ -55,8 +56,8 @@ import freenet.support.api.HTTPUploadedFile; */ public class UploadImagePage extends SoneTemplatePage { - /** The logger. */ private static final Logger logger = getLogger(UploadImagePage.class.getName()); + private static final String UNKNOWN_MIME_TYPE = "application/octet-stream"; /** * Creates a new “upload image” page. @@ -154,12 +155,13 @@ public class UploadImagePage extends SoneTemplatePage { ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream); Iterator imageReaders = ImageIO.getImageReaders(imageInputStream); if (imageReaders.hasNext()) { - return imageReaders.next().getOriginatingProvider().getMIMETypes()[0]; + return fromNullable(imageReaders.next().getOriginatingProvider().getMIMETypes()) + .or(new String[] { UNKNOWN_MIME_TYPE })[0]; } } catch (IOException ioe1) { logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1); } - return "application/octet-stream"; + return UNKNOWN_MIME_TYPE; } } -- 2.7.4