refactor getThumbnail to reduce if/else clutter

This commit is contained in:
millmanorama 2017-03-22 11:54:10 +01:00
parent 46ca7ee6a1
commit b981923546

View File

@ -308,26 +308,27 @@ public class ImageUtils {
* Intercepting the image reading code for GIFs here allows us * Intercepting the image reading code for GIFs here allows us
* to rescale easily, but we lose animations. * to rescale easily, but we lose animations.
*/ */
try { try (BufferedInputStream bufferedReadContentStream = getBufferedReadContentStream(file);) {
return ScalrWrapper.resizeHighQuality(ImageIO.read(getBufferedReadContentStream(file)), iconSize, iconSize); final BufferedImage image = ImageIO.read(bufferedReadContentStream);
if (image != null) {
return ScalrWrapper.resizeHighQuality(image, iconSize, iconSize);
}
} catch (IOException iOException) { } catch (IOException iOException) {
LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), iOException); //NON-NLS LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), iOException); //NON-NLS
}
return DEFAULT_THUMBNAIL; return DEFAULT_THUMBNAIL;
} }
} else {
Task<javafx.scene.image.Image> thumbnailTask = newGetThumbnailTask(file, iconSize, true); Task<javafx.scene.image.Image> thumbnailTask = newGetThumbnailTask(file, iconSize, true);
thumbnailTask.run(); thumbnailTask.run();
try { try {
return SwingFXUtils.fromFXImage(thumbnailTask.get(), null); return SwingFXUtils.fromFXImage(thumbnailTask.get(), null);
} catch (InterruptedException | ExecutionException ex) { } catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), ex); //NON-NLS LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), ex); //NON-NLS
}
}
return DEFAULT_THUMBNAIL; return DEFAULT_THUMBNAIL;
} }
}
} else {
return DEFAULT_THUMBNAIL;
}
}
/** /**
* Get a BufferedInputStream wrapped around a ReadContentStream for the * Get a BufferedInputStream wrapped around a ReadContentStream for the
@ -567,8 +568,8 @@ public class ImageUtils {
* @see #getImageHeight(org.sleuthkit.datamodel.AbstractFile) * @see #getImageHeight(org.sleuthkit.datamodel.AbstractFile)
*/ */
private static <T> T getImageProperty(AbstractFile file, final String errorTemplate, PropertyExtractor<T> propertyExtractor) throws IOException { private static <T> T getImageProperty(AbstractFile file, final String errorTemplate, PropertyExtractor<T> propertyExtractor) throws IOException {
try (InputStream inputStream = getBufferedReadContentStream(file);) { try (InputStream inputStream = getBufferedReadContentStream(file);
try (ImageInputStream input = ImageIO.createImageInputStream(inputStream)) { ImageInputStream input = ImageIO.createImageInputStream(inputStream)) {
if (input == null) { if (input == null) {
IIOException iioException = new IIOException("Could not create ImageInputStream."); IIOException iioException = new IIOException("Could not create ImageInputStream.");
LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file)); LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));
@ -580,7 +581,6 @@ public class ImageUtils {
ImageReader reader = readers.next(); ImageReader reader = readers.next();
reader.setInput(input); reader.setInput(input);
try { try {
return propertyExtractor.extract(reader); return propertyExtractor.extract(reader);
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.log(Level.WARNING, errorTemplate + ex.toString(), getContentPathSafe(file)); LOGGER.log(Level.WARNING, errorTemplate + ex.toString(), getContentPathSafe(file));
@ -591,12 +591,10 @@ public class ImageUtils {
} else { } else {
IIOException iioException = new IIOException("No ImageReader found."); IIOException iioException = new IIOException("No ImageReader found.");
LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file)); LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));
throw iioException; throw iioException;
} }
} }
} }
}
/** /**
* Create a new Task that will get a thumbnail for the given image of the * Create a new Task that will get a thumbnail for the given image of the