]> source.dussan.org Git - poi.git/commitdiff
fix some cases where iterator usage does not check hasNext
authorPJ Fanning <fanningpj@apache.org>
Thu, 14 Mar 2019 04:01:10 +0000 (04:01 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 14 Mar 2019 04:01:10 +0000 (04:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1855480 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/ImageUtils.java
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/GeometrySection.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java

index af54e26df34ccdc72015a8a46f2a55ce69ee5f7d..9a2b1778c7e1a38b4aaf831c7ab54777ebd040f1 100644 (file)
@@ -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) {
index 504a33621023d4cd3d994024c1479a562498d88f..cb212e343b1e2b19e3eb0b7e121cedaed24a85b3 100644 (file)
@@ -98,7 +98,7 @@ public class GeometrySection extends XDGFSection {
         Iterator<GeometryRow> rows = getCombinedRows().iterator();
 
         // special cases
-        GeometryRow first = rows.next();
+        GeometryRow first = rows.hasNext() ? rows.next() : null;
 
         if (first instanceof Ellipse) {
             return ((Ellipse)first).getPath();
index e6a271da634691ec094d56d0775d788a41010f94..49b114512ba17ebc97826ed9c23df4eab4315d9e 100644 (file)
@@ -1220,7 +1220,7 @@ public final class HSLFTextParagraph implements TextParagraph<HSLFShape,HSLFText
         assert(!paragraphs.isEmpty() && !paragraphs.get(0).getTextRuns().isEmpty());
 
         Iterator<HSLFTextParagraph> paraIter = paragraphs.iterator();
-        HSLFTextParagraph htp = paraIter.next(); // keep first
+        HSLFTextParagraph htp = paraIter.hasNext() ? paraIter.next() : null; // keep first
         assert (htp != null);
         while (paraIter.hasNext()) {
             paraIter.next();