You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ki...@apache.org on 2016/12/17 02:35:30 UTC

svn commit: r1774705 - in /poi/trunk/src: examples/src/org/apache/poi/hpsf/examples/ java/org/apache/poi/ scratchpad/src/org/apache/poi/hpbf/extractor/ scratchpad/src/org/apache/poi/hslf/blip/

Author: kiwiwings
Date: Sat Dec 17 02:35:30 2016
New Revision: 1774705

URL: http://svn.apache.org/viewvc?rev=1774705&view=rev
Log:
various sonarqube fixes - mainly resource closing

Modified:
    poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java
    poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java
    poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java
    poi/trunk/src/java/org/apache/poi/POIDocument.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hpbf/extractor/PublisherTextExtractor.java
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java

Modified: poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/CopyCompare.java Sat Dec 17 02:35:30 2016
@@ -243,7 +243,6 @@ public class CopyCompare
     throws NoPropertySetStreamException, MarkUnsupportedException,
            UnsupportedEncodingException, IOException
     {
-        boolean equal = true;
         final DocumentInputStream dis1 = new DocumentInputStream(d1);
         final DocumentInputStream dis2 = new DocumentInputStream(d2);
         try {
@@ -251,23 +250,20 @@ public class CopyCompare
                 PropertySet.isPropertySetStream(dis2)) {
                 final PropertySet ps1 = PropertySetFactory.create(dis1);
                 final PropertySet ps2 = PropertySetFactory.create(dis2);
-                equal = ps1.equals(ps2);
-                if (!equal) {
+                if (!ps1.equals(ps2)) {
                     msg.append("Property sets are not equal.\n");
-                    return equal;
+                    return false;
                 }
             } else {
-                int i1;
-                int i2;
+                int i1, i2;
                 do {
                     i1 = dis1.read();
                     i2 = dis2.read();
                     if (i1 != i2) {
-                        equal = false;
                         msg.append("Documents are not equal.\n");
-                        break;
+                        return false;
                     }
-                } while (equal && i1 == -1);
+                } while (i1 > -1);
             }
         } finally {
             dis2.close();

Modified: poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteAuthorAndTitle.java Sat Dec 17 02:35:30 2016
@@ -166,53 +166,42 @@ public class WriteAuthorAndTitle
 
             Throwable t = null;
 
-            try
-            {
+            try {
                 /* Find out whether the current document is a property set
                  * stream or not. */
-                if (PropertySet.isPropertySetStream(stream))
-                {
-                    /* Yes, the current document is a property set stream.
-                     * Let's create a PropertySet instance from it. */
-                    PropertySet ps = null;
-                    try
-                    {
-                        ps = PropertySetFactory.create(stream);
-                    }
-                    catch (NoPropertySetStreamException ex)
-                    {
+                if (PropertySet.isPropertySetStream(stream)) {
+                    try {
+                        /* Yes, the current document is a property set stream.
+                         * Let's create a PropertySet instance from it. */
+                        PropertySet ps = PropertySetFactory.create(stream);
+
+                        /* Now we know that we really have a property set. The next
+                         * step is to find out whether it is a summary information
+                         * or not. */
+                        if (ps.isSummaryInformation()) {
+                            /* Yes, it is a summary information. We will modify it
+                             * and write the result to the destination POIFS. */
+                            editSI(poiFs, path, name, ps);
+                        } else {
+                            /* No, it is not a summary information. We don't care
+                             * about its internals and copy it unmodified to the
+                             * destination POIFS. */
+                            copy(poiFs, path, name, ps);
+                        }
+                    } catch (NoPropertySetStreamException ex) {
                         /* This exception will not be thrown because we already
                          * checked above. */
                     }
-
-                    /* Now we know that we really have a property set. The next
-                     * step is to find out whether it is a summary information
-                     * or not. */
-                    if (ps.isSummaryInformation())
-                        /* Yes, it is a summary information. We will modify it
-                         * and write the result to the destination POIFS. */
-                        editSI(poiFs, path, name, ps);
-                    else
-                        /* No, it is not a summary information. We don't care
-                         * about its internals and copy it unmodified to the
-                         * destination POIFS. */
-                        copy(poiFs, path, name, ps);
-                }
-                else
+                } else {
                     /* No, the current document is not a property set stream. We
                      * copy it unmodified to the destination POIFS. */
                     copy(poiFs, event.getPath(), event.getName(), stream);
-            }
-            catch (MarkUnsupportedException ex)
-            {
+                }
+            } catch (MarkUnsupportedException ex) {
                 t = ex;
-            }
-            catch (IOException ex)
-            {
+            } catch (IOException ex) {
                 t = ex;
-            }
-            catch (WritingNotSupportedException ex)
-            {
+            } catch (WritingNotSupportedException ex) {
                 t = ex;
             }
 
@@ -221,8 +210,7 @@ public class WriteAuthorAndTitle
              * lines check whether a checked exception occured and throws an
              * unchecked exception. The message of that exception is that of
              * the underlying checked exception. */
-            if (t != null)
-            {
+            if (t != null) {
                 throw new HPSFRuntimeException
                     ("Could not read file \"" + path + "/" + name +
                      "\". Reason: " + Util.toString(t));

Modified: poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java (original)
+++ poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java Sat Dec 17 02:35:30 2016
@@ -97,7 +97,10 @@ public class WriteTitle
         poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
 
         /* Write the whole POI file system to a disk file. */
-        poiFs.writeFilesystem(new FileOutputStream(fileName));
+        FileOutputStream fos = new FileOutputStream(fileName);
+        poiFs.writeFilesystem(fos);
+        fos.close();
+        poiFs.close();
     }
 
 }

Modified: poi/trunk/src/java/org/apache/poi/POIDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/POIDocument.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/POIDocument.java (original)
+++ poi/trunk/src/java/org/apache/poi/POIDocument.java Sat Dec 17 02:35:30 2016
@@ -424,7 +424,7 @@ public abstract class POIDocument implem
     @Internal
     protected boolean initDirectory() {
         if (directory == null) {
-            directory = new NPOIFSFileSystem().getRoot();
+            directory = new NPOIFSFileSystem().getRoot(); // NOSONAR
             return true;
         }
         return false;

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hpbf/extractor/PublisherTextExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hpbf/extractor/PublisherTextExtractor.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hpbf/extractor/PublisherTextExtractor.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hpbf/extractor/PublisherTextExtractor.java Sat Dec 17 02:35:30 2016
@@ -108,10 +108,14 @@ public final class PublisherTextExtracto
 		}
 
 		for(int i=0; i<args.length; i++) {
-			PublisherTextExtractor te = new PublisherTextExtractor(
-					new FileInputStream(args[i])
-			);
-			System.out.println(te.getText());
+		    FileInputStream fis = new FileInputStream(args[i]);
+		    try {
+		        PublisherTextExtractor te = new PublisherTextExtractor(fis);
+		        System.out.println(te.getText());
+		        te.close();
+		    } finally {
+		        fis.close();
+		    }
 		}
 	}
 }

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java?rev=1774705&r1=1774704&r2=1774705&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java Sat Dec 17 02:35:30 2016
@@ -20,6 +20,7 @@ package org.apache.poi.hslf.blip;
 import java.awt.Dimension;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.EOFException;
 import java.io.IOException;
 import java.util.zip.InflaterInputStream;
 
@@ -56,7 +57,11 @@ public final class PICT extends Metafile
         ByteArrayInputStream bis = new ByteArrayInputStream(data);
         Header header = new Header();
         header.read(data, pos);
-        bis.skip(pos + header.getSize());
+        long bs_exp = pos + header.getSize();
+        long bs_act = bis.skip(bs_exp);
+        if (bs_exp != bs_act) {
+            throw new EOFException();
+        }
         byte[] chunk = new byte[4096];
         ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize());
         InflaterInputStream inflater = new InflaterInputStream( bis );



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org