Changed exception catching in ImageUtils to a larger scope

(IOException -> Exception). Fixed issue with converting a Image to
InputStream for size check operation.
This commit is contained in:
nathan 2016-12-28 00:59:46 +01:00
parent c01f12fcaf
commit e3fbec2c55

View File

@ -309,8 +309,8 @@ class ImageUtils {
try { try {
final BufferedImage image = getTransparentImageAsImage(size); final BufferedImage image = getTransparentImageAsImage(size);
ImageIO.write(image, "png", newFile); ImageIO.write(image, "png", newFile);
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); SystemTray.logger.error("Error creating transparent image for size: {}", size, e);
} }
return newFile; return newFile;
@ -335,7 +335,8 @@ class ImageUtils {
// since it's the error file, we want to delete it on exit! // since it's the error file, we want to delete it on exit!
save.deleteOnExit(); save.deleteOnExit();
return save; return save;
} catch (IOException e) { } catch (Exception e) {
// this must be thrown
throw new RuntimeException("Serious problems! Unable to extract error image, this should NEVER happen!", e); throw new RuntimeException("Serious problems! Unable to extract error image, this should NEVER happen!", e);
} }
} }
@ -347,7 +348,7 @@ class ImageUtils {
if (check != null) { if (check != null) {
return check; return check;
} }
} catch (IOException e) { } catch (Exception e) {
SystemTray.logger.error("Error checking cache for information. Using error icon instead", e); SystemTray.logger.error("Error checking cache for information. Using error icon instead", e);
return getErrorImage(cacheName); return getErrorImage(cacheName);
} }
@ -371,7 +372,7 @@ class ImageUtils {
fileInputStream.close(); fileInputStream.close();
return file; return file;
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error reading image. Using error icon instead", e); SystemTray.logger.error("Error reading image. Using error icon instead", e);
return getErrorImage(size + "default"); return getErrorImage(size + "default");
@ -391,7 +392,7 @@ class ImageUtils {
inputStream.close(); inputStream.close();
return file; return file;
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error reading image. Using error icon instead", e); SystemTray.logger.error("Error reading image. Using error icon instead", e);
return getErrorImage(size + "default"); return getErrorImage(size + "default");
@ -409,12 +410,17 @@ class ImageUtils {
trayImage.flush(); trayImage.flush();
try { try {
ImageInputStream imageInputStream = ImageIO.createImageInputStream(image); BufferedImage bufferedImage = getBufferedImage(trayImage);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", os);
InputStream imageInputStream = new ByteArrayInputStream(os.toByteArray());
File file = resizeAndCache(size, imageInputStream); File file = resizeAndCache(size, imageInputStream);
imageInputStream.close(); imageInputStream.close(); // BAOS doesn't do anything, but here for completeness + documentation
return file; return file;
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error reading image. Using error icon instead", e); SystemTray.logger.error("Error reading image. Using error icon instead", e);
return getErrorImage(size + "default"); return getErrorImage(size + "default");
@ -431,7 +437,7 @@ class ImageUtils {
try { try {
ByteArrayOutputStream byteArrayOutputStream = IO.copyStream(imageStream); ByteArrayOutputStream byteArrayOutputStream = IO.copyStream(imageStream);
return resizeAndCache(size, new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); return resizeAndCache(size, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error reading image. Using error icon instead", e); SystemTray.logger.error("Error reading image. Using error icon instead", e);
return getErrorImage(size + "default"); return getErrorImage(size + "default");
@ -452,7 +458,8 @@ class ImageUtils {
imageStream.close(); imageStream.close();
imageStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); imageStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (IOException e) { } catch (Exception e) {
// this must be thrown
throw new RuntimeException("Unable to read from inputStream.", e); throw new RuntimeException("Unable to read from inputStream.", e);
} }
} }
@ -479,7 +486,7 @@ class ImageUtils {
// we can reuse this URL (it's the correct size). // we can reuse this URL (it's the correct size).
needsResize = false; needsResize = false;
} }
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error resizing image. Using error icon instead", e); SystemTray.logger.error("Error resizing image. Using error icon instead", e);
return getErrorImage(cacheName); return getErrorImage(cacheName);
@ -495,13 +502,13 @@ class ImageUtils {
// now cache that file // now cache that file
try { try {
return CacheUtil.save(cacheName, resizedFile); return CacheUtil.save(cacheName, resizedFile);
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error caching image. Using error icon instead", e); SystemTray.logger.error("Error caching image. Using error icon instead", e);
return getErrorImage(cacheName); return getErrorImage(cacheName);
} }
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error resizing image. Using error icon instead", e); SystemTray.logger.error("Error resizing image. Using error icon instead", e);
return getErrorImage(cacheName); return getErrorImage(cacheName);
@ -511,7 +518,7 @@ class ImageUtils {
// no resize necessary, just cache as is. // no resize necessary, just cache as is.
try { try {
return CacheUtil.save(cacheName, imageStream); return CacheUtil.save(cacheName, imageStream);
} catch (IOException e) { } catch (Exception e) {
// have to serve up the error image instead. // have to serve up the error image instead.
SystemTray.logger.error("Error caching image. Using error icon instead", e); SystemTray.logger.error("Error caching image. Using error icon instead", e);
return getErrorImage(cacheName); return getErrorImage(cacheName);
@ -695,6 +702,7 @@ class ImageUtils {
ImageInputStream in = null; ImageInputStream in = null;
ImageReader reader = null; ImageReader reader = null;
try { try {
// This will ONLY work for File, InputStream, and RandomAccessFile
in = ImageIO.createImageInputStream(fileStream); in = ImageIO.createImageInputStream(fileStream);
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in); final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);