Browse Source

ImageImport: close stream correctly, handle null stream correct

Henrik Kunzelmann 3 years ago
parent
commit
ae42115157
1 changed files with 18 additions and 19 deletions
  1. 18 19
      src/utility/ImageImport.java

+ 18 - 19
src/utility/ImageImport.java

@@ -95,30 +95,29 @@ public class ImageImport {
 	/**
 	 * Loads an image from the given path and scales it using the smooth algorithm.
 	 * @param url Path to the image to be loaded.
-	 * @param w Width the loaded image should be scaled to.
-	 * @param h Height the loaded image should be scaled to.
 	 * @return An image loaded from the requested path.
 	 */
 	public static Image loadImage(String url){
-		if(SAVE_MODE!=SAVE_NOTHING){
-			if(imgStorage.containsKey(url))return imgStorage.get(url);
-			else{
-				Image img;
-				try {
-					img= ImageIO.read(loadStream(url));
-				} catch (IOException e) {
-					e.printStackTrace();
-					return defaultImage;
+		boolean save = SAVE_MODE!=SAVE_NOTHING;
+		if (save && imgStorage.containsKey(url))
+			return imgStorage.get(url);
+		else {
+			Image img;
+			try {
+				try (InputStream stream = loadStream(url)) {
+					if (stream == null)
+						return defaultImage;
+					img = ImageIO.read(stream);
 				}
-				imgStorage.put(url, img);
-				return img;
+			} catch (IOException e) {
+				e.printStackTrace();
+				return defaultImage;
 			}
-		}
-		try {
-			return ImageIO.read(loadStream(url));
-		} catch (IOException e) {
-			e.printStackTrace();
-			return defaultImage;
+			if (img == null)
+				return defaultImage;
+			if (save)
+				imgStorage.put(url, img);
+			return img;
 		}
 	}