]> source.dussan.org Git - poi.git/commitdiff
SonarQube fixes
authorAndreas Beeker <kiwiwings@apache.org>
Sat, 7 Jan 2017 00:26:46 +0000 (00:26 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sat, 7 Jan 2017 00:26:46 +0000 (00:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777739 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/HexDump.java
src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
src/ooxml/java/org/apache/poi/xdgf/usermodel/section/CombinedIterable.java
src/ooxml/java/org/apache/poi/xslf/extractor/XSLFPowerPointExtractor.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFPicture.java

index 01a8f509115b753fa8707470dc4fd7d43ea4b87c..7f803b9584696a05b8fb747dbdde028d917dd417 100644 (file)
@@ -17,9 +17,7 @@
 
 package org.apache.poi.util;
 
-import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -168,7 +166,9 @@ public class HexDump {
 
     public static char toAscii(int dataB) {
         char charB = (char)(dataB & 0xFF);
-        if (Character.isISOControl(charB)) return '.';
+        if (Character.isISOControl(charB)) {
+            return '.';
+        }
         
         switch (charB) {
             // printable, but not compilable with current compiler encoding
@@ -408,12 +408,10 @@ public class HexDump {
     }    
     
     
-    public static void main(String[] args) throws Exception {
-        File file = new File(args[0]);
-        InputStream in = new BufferedInputStream(new FileInputStream(file));
-        byte[] b = new byte[(int)file.length()];
-        in.read(b);
-        System.out.println(HexDump.dump(b, 0, 0));
+    public static void main(String[] args) throws IOException {
+        InputStream in = new FileInputStream(args[0]);
+        byte[] b = IOUtils.toByteArray(in);
         in.close();
+        System.out.println(HexDump.dump(b, 0, 0));
     }
 }
index 2c5fe70b73b3865ae1144e1b2111cf8960155317..48ee58e829e246b90125f44632d1a46fbc9ff484 100644 (file)
@@ -56,7 +56,9 @@ public final class LittleEndianByteArrayInputStream extends ByteArrayInputStream
            final int size = LittleEndianConsts.INT_SIZE;
                checkPosition(size);
                int le = LittleEndian.getInt(buf, pos);
-               super.skip(size);
+        if (super.skip(size) < size) {
+            throw new RuntimeException("Buffer overrun");
+        }
                return le;
        }
 
@@ -65,7 +67,9 @@ public final class LittleEndianByteArrayInputStream extends ByteArrayInputStream
            final int size = LittleEndianConsts.LONG_SIZE;
                checkPosition(size);
                long le = LittleEndian.getLong(buf, pos);
-               super.skip(size);
+               if (super.skip(size) < size) {
+                   throw new RuntimeException("Buffer overrun");
+               }
                return le;
        }
 
@@ -84,7 +88,9 @@ public final class LittleEndianByteArrayInputStream extends ByteArrayInputStream
         final int size = LittleEndianConsts.SHORT_SIZE;
         checkPosition(size);
         int le = LittleEndian.getUShort(buf, pos);
-        super.skip(size);
+        if (super.skip(size) < size) {
+            throw new RuntimeException("Buffer overrun");
+        }
         return le;
        }
 
index bca48bc09651562c0659f5486a8d476e7a2da8b9..d51e9070df242a6bb0a6d5ea039fa60d33f4a27a 100644 (file)
@@ -118,7 +118,7 @@ public class CombinedIterable<T> implements Iterable<T> {
 
                     } else {
                         lastI = masterIdx;
-                        val = currentMaster.getValue();
+                        val = (currentMaster != null) ? currentMaster.getValue() : null;
                         currentMaster = null;
                     }
 
index 5f757d0c59214cd72076f28e9a8bd4b3650f4281..d3ff89613f362d8d56c42aab94ddaff92e594b18 100644 (file)
@@ -101,7 +101,8 @@ public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
        /**
         * Gets the slide text, but not the notes text
         */
-       public String getText() {
+       @Override
+    public String getText() {
                return getText(slidesByDefault, notesByDefault);
        }
        
@@ -162,12 +163,10 @@ public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
           
           // If requested, get text from the master and it's layout 
           if(masterText) {
-             if(layout != null) {
-                extractText(layout, true, text);
-             }
-             if(master != null) {
-                extractText(master, true, text);
-             }
+             assert (layout != null);
+             extractText(layout, true, text);
+             assert (master != null);
+             extractText(master, true, text);
           }
 
           // If the slide has comments, do those too
index cee2d0eb04eb8c4cdb319e918deeb18bb0c59123..d022450cbeff1ee921158ed045a7af8c9aa63a09 100644 (file)
@@ -115,11 +115,7 @@ implements SlideShow<XSLFShape,XSLFTextParagraph> {
         } catch (Exception e){
             throw new POIXMLException(e);
         } finally {
-            try {
-                is.close();
-            } catch (Exception e) {
-                throw new POIXMLException(e);
-            }
+            IOUtils.closeQuietly(is);
         }
     }
 
index 241c650628542a5bf3fb4c866c96d14184bc3c69..9635882973e05af77183ea92c46c40364da232d3 100644 (file)
@@ -17,6 +17,9 @@
 
 package org.apache.poi.xssf.streaming;
 
+import java.awt.Dimension;
+import java.io.IOException;
+
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.usermodel.Picture;
 import org.apache.poi.ss.usermodel.Row;
@@ -26,15 +29,19 @@ import org.apache.poi.ss.util.ImageUtils;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFAnchor;
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
+import org.apache.poi.xssf.usermodel.XSSFDrawing;
+import org.apache.poi.xssf.usermodel.XSSFPicture;
+import org.apache.poi.xssf.usermodel.XSSFPictureData;
+import org.apache.poi.xssf.usermodel.XSSFShape;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties;
 import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
 
-import java.awt.Dimension;
-import java.io.IOException;
-
 /**
  * Streaming version of Picture.
  * Most of the code is a copy of the non-streaming XSSFPicture code.
@@ -140,38 +147,31 @@ public final class SXSSFPicture implements Picture {
         double scaledHeight = size.getHeight() * scale;
 
         float w = 0;
-        int col2 = anchor.getCol1();
-        int dx2 = 0;
+        int col2 = anchor.getCol1()-1;
 
-        for (;;) {
-            w += getColumnWidthInPixels(col2);
-            if(w > scaledWidth) break;
-            col2++;
+        while (w <= scaledWidth) {
+            w += getColumnWidthInPixels(++col2);
         }
 
-        if(w > scaledWidth) {
-            double cw = getColumnWidthInPixels(col2 );
-            double delta = w - scaledWidth;
-            dx2 = (int)(XSSFShape.EMU_PER_PIXEL * (cw - delta));
-        }
+        assert (w > scaledWidth);
+        double cw = getColumnWidthInPixels(col2);
+        double deltaW = w - scaledWidth;
+        int dx2 = (int)(XSSFShape.EMU_PER_PIXEL * (cw - deltaW));
+
         anchor.setCol2(col2);
         anchor.setDx2(dx2);
 
         double h = 0;
-        int row2 = anchor.getRow1();
-        int dy2 = 0;
+        int row2 = anchor.getRow1()-1;
 
-        for (;;) {
-            h += getRowHeightInPixels(row2);
-            if(h > scaledHeight) break;
-            row2++;
+        while (h <= scaledHeight) {
+            h += getRowHeightInPixels(++row2);
         }
 
-        if(h > scaledHeight) {
-            double ch = getRowHeightInPixels(row2);
-            double delta = h - scaledHeight;
-            dy2 = (int)(XSSFShape.EMU_PER_PIXEL * (ch - delta));
-        }
+        assert (h > scaledHeight);
+        double ch = getRowHeightInPixels(row2);
+        double deltaH = h - scaledHeight;
+        int dy2 = (int)(XSSFShape.EMU_PER_PIXEL * (ch - deltaH));
         anchor.setRow2(row2);
         anchor.setDy2(dy2);