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