diff options
author | PJ Fanning <fanningpj@apache.org> | 2019-03-14 04:01:10 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2019-03-14 04:01:10 +0000 |
commit | 126606c838dcba5d937de565661bd464abdbaab4 (patch) | |
tree | d59a2a3ec75ca34717ee27a55773d8c59fdb59f9 /src/java/org/apache/poi/ss/util | |
parent | 7f12edf4165db70c6d282de1989b147464148188 (diff) | |
download | poi-126606c838dcba5d937de565661bd464abdbaab4.tar.gz poi-126606c838dcba5d937de565661bd464abdbaab4.zip |
fix some cases where iterator usage does not check hasNext
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1855480 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss/util')
-rw-r--r-- | src/java/org/apache/poi/ss/util/ImageUtils.java | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/src/java/org/apache/poi/ss/util/ImageUtils.java b/src/java/org/apache/poi/ss/util/ImageUtils.java index af54e26df3..9a2b1778c7 100644 --- a/src/java/org/apache/poi/ss/util/ImageUtils.java +++ b/src/java/org/apache/poi/ss/util/ImageUtils.java @@ -59,7 +59,7 @@ public class ImageUtils { * * @return image dimension in pixels */ - public static Dimension getImageDimension(InputStream is, int type){ + public static Dimension getImageDimension(InputStream is, int type) { Dimension size = new Dimension(); switch (type){ @@ -70,28 +70,29 @@ public class ImageUtils { case Workbook.PICTURE_TYPE_DIB: try { //read the image using javax.imageio.* - ImageInputStream iis = ImageIO.createImageInputStream( is ); - try { + try (ImageInputStream iis = ImageIO.createImageInputStream(is)) { Iterator<ImageReader> i = ImageIO.getImageReaders( iis ); - ImageReader r = i.next(); - try { - r.setInput( iis ); - BufferedImage img = r.read(0); - - int[] dpi = getResolution(r); - - //if DPI is zero then assume standard 96 DPI - //since cannot divide by zero - if (dpi[0] == 0) dpi[0] = PIXEL_DPI; - if (dpi[1] == 0) dpi[1] = PIXEL_DPI; - - size.width = img.getWidth()*PIXEL_DPI/dpi[0]; - size.height = img.getHeight()*PIXEL_DPI/dpi[1]; - } finally { - r.dispose(); + if (i.hasNext()) { + ImageReader r = i.next(); + try { + r.setInput( iis ); + BufferedImage img = r.read(0); + + int[] dpi = getResolution(r); + + //if DPI is zero then assume standard 96 DPI + //since cannot divide by zero + if (dpi[0] == 0) dpi[0] = PIXEL_DPI; + if (dpi[1] == 0) dpi[1] = PIXEL_DPI; + + size.width = img.getWidth()*PIXEL_DPI/dpi[0]; + size.height = img.getHeight()*PIXEL_DPI/dpi[1]; + } finally { + r.dispose(); + } + } else { + logger.log(POILogger.WARN, "ImageIO found no images"); } - } finally { - iis.close(); } } catch (IOException e) { |