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,25 +308,26 @@ 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;
}
} else {
Task<javafx.scene.image.Image> thumbnailTask = newGetThumbnailTask(file, iconSize, true);
thumbnailTask.run();
try {
return SwingFXUtils.fromFXImage(thumbnailTask.get(), null);
} catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), ex); //NON-NLS
return DEFAULT_THUMBNAIL;
} }
return DEFAULT_THUMBNAIL;
}
Task<javafx.scene.image.Image> thumbnailTask = newGetThumbnailTask(file, iconSize, true);
thumbnailTask.run();
try {
return SwingFXUtils.fromFXImage(thumbnailTask.get(), null);
} catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.WARNING, "Failed to get thumbnail for " + getContentPathSafe(content), ex); //NON-NLS
} }
} else {
return DEFAULT_THUMBNAIL;
} }
return DEFAULT_THUMBNAIL;
} }
/** /**
@ -567,33 +568,30 @@ 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));
throw iioException; throw iioException;
} }
Iterator<ImageReader> readers = ImageIO.getImageReaders(input); Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) { if (readers.hasNext()) {
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)); throw ex;
throw ex; } finally {
} finally { reader.dispose();
reader.dispose();
}
} else {
IIOException iioException = new IIOException("No ImageReader found.");
LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));
throw iioException;
} }
} else {
IIOException iioException = new IIOException("No ImageReader found.");
LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));
throw iioException;
} }
} }
} }