poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1777526 - in /poi/trunk/src: examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
Date Thu, 05 Jan 2017 23:40:57 GMT
Author: kiwiwings
Date: Thu Jan  5 23:40:57 2017
New Revision: 1777526

URL: http://svn.apache.org/viewvc?rev=1777526&view=rev
Log:
SonarQube fixes

Modified:
    poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java
    poi/trunk/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java

Modified: poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java?rev=1777526&r1=1777525&r2=1777526&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java
(original)
+++ poi/trunk/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java
Thu Jan  5 23:40:57 2017
@@ -16,11 +16,10 @@
 ==================================================================== */
 package org.apache.poi.hssf.usermodel.examples;
 
+import java.io.Closeable;
 import java.io.FileInputStream;
-import java.util.Iterator;
 
 import org.apache.poi.hslf.usermodel.HSLFSlideShow;
-import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
 import org.apache.poi.hssf.usermodel.HSSFObjectData;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hwpf.HWPFDocument;
@@ -39,26 +38,19 @@ public class EmbeddedObjects {
         for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
             //the OLE2 Class Name of the object
             String oleName = obj.getOLE2ClassName();
+            DirectoryNode dn = (obj.hasDirectoryEntry()) ? (DirectoryNode) obj.getDirectory()
: null;
+            Closeable document = null;
             if (oleName.equals("Worksheet")) {
-                DirectoryNode dn = (DirectoryNode) obj.getDirectory();
-                HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);
-                //System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets());
-                embeddedWorkbook.close();
+                document = new HSSFWorkbook(dn, fs, false);
             } else if (oleName.equals("Document")) {
-                DirectoryNode dn = (DirectoryNode) obj.getDirectory();
-                HWPFDocument embeddedWordDocument = new HWPFDocument(dn);
-                //System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text());
+                document = new HWPFDocument(dn);
             }  else if (oleName.equals("Presentation")) {
-                DirectoryNode dn = (DirectoryNode) obj.getDirectory();
-                HSLFSlideShow embeddedPowerPointDocument = new HSLFSlideShow(new HSLFSlideShowImpl(dn));
-                //System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length);
+                document = new HSLFSlideShow(dn);
             } else {
-                if(obj.hasDirectoryEntry()){
+                if(dn != null){
                     // The DirectoryEntry is a DocumentNode. Examine its entries to find
out what it is
-                    DirectoryNode dn = (DirectoryNode) obj.getDirectory();
-                    for (Iterator<Entry> entries = dn.getEntries(); entries.hasNext();)
{
-                        Entry entry = entries.next();
-                        //System.out.println(oleName + "." + entry.getName());
+                    for (Entry entry : dn) {
+                         String name = entry.getName();
                     }
                 } else {
                     // There is no DirectoryEntry
@@ -66,6 +58,9 @@ public class EmbeddedObjects {
                     byte[] objectData = obj.getObjectData();
                 }
             }
+            if (document != null) {
+                document.close();
+            }
         }
         workbook.close();
     }

Modified: poi/trunk/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java?rev=1777526&r1=1777525&r2=1777526&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java Thu Jan
 5 23:40:57 2017
@@ -53,10 +53,10 @@ public class CryptoAPIDecryptor extends
 
     private long length = -1L;
     private int chunkSize = -1;
-    
+
     static class StreamDescriptorEntry {
         static BitField flagStream = BitFieldFactory.getInstance(1);
-        
+
         int streamOffset;
         int streamSize;
         int block;
@@ -149,17 +149,16 @@ public class CryptoAPIDecryptor extends
             throws IOException, GeneralSecurityException {
         return new CryptoAPICipherInputStream(stream, size, initialPos);
     }
-    
+
     /**
      * Decrypt the Document-/SummaryInformation and other optionally streams.
      * Opposed to other crypto modes, cryptoapi is record based and can't be used
      * to stream-decrypt a whole file
-     * 
+     *
      * @see <a href="http://msdn.microsoft.com/en-us/library/dd943321(v=office.12).aspx">2.3.5.4
RC4 CryptoAPI Encrypted Summary Stream</a>
      */
     public POIFSFileSystem getSummaryEntries(DirectoryNode root, String encryptedStream)
     throws IOException, GeneralSecurityException {
-        POIFSFileSystem fsOut = new POIFSFileSystem();
         // HSLF: encryptedStream
         // HSSF: encryption
         DocumentNode es = (DocumentNode) root.getEntry(encryptedStream);
@@ -169,6 +168,7 @@ public class CryptoAPIDecryptor extends
         dis.close();
         CryptoAPIDocumentInputStream sbis = new CryptoAPIDocumentInputStream(this, bos.toByteArray());
         LittleEndianInputStream leis = new LittleEndianInputStream(sbis);
+        POIFSFileSystem fsOut = null;
         try {
             int streamDescriptorArrayOffset = (int) leis.readUInt();
             /* int streamDescriptorArraySize = (int) */ leis.readUInt();
@@ -193,7 +193,8 @@ public class CryptoAPIDecryptor extends
                 leis.readShort();
                 assert(entry.streamName.length() == nameSize);
             }
-    
+
+            fsOut = new POIFSFileSystem();
             for (StreamDescriptorEntry entry : entries) {
                 sbis.seek(entry.streamOffset);
                 sbis.setBlock(entry.block);
@@ -201,11 +202,19 @@ public class CryptoAPIDecryptor extends
                 fsOut.createDocument(is, entry.streamName);
                 is.close();
             }
+        } catch (Exception e) {
+            IOUtils.closeQuietly(fsOut);
+            if (e instanceof GeneralSecurityException) {
+                throw (GeneralSecurityException)e;
+            } else if (e instanceof IOException) {
+                throw (IOException)e;
+            } else {
+                throw new IOException("summary entries can't be read", e);
+            }
         } finally {
             IOUtils.closeQuietly(leis);
             IOUtils.closeQuietly(sbis);
         }
-        sbis = null;
         return fsOut;
     }
 
@@ -220,10 +229,11 @@ public class CryptoAPIDecryptor extends
         return length;
     }
 
+    @Override
     public void setChunkSize(int chunkSize) {
         this.chunkSize = chunkSize;
     }
-    
+
     @Override
     public CryptoAPIDecryptor clone() throws CloneNotSupportedException {
         return (CryptoAPIDecryptor)super.clone();
@@ -240,6 +250,6 @@ public class CryptoAPIDecryptor extends
         public CryptoAPICipherInputStream(InputStream stream, long size, int initialPos)
                 throws GeneralSecurityException {
             super(stream, size, chunkSize, initialPos);
-        }    
+        }
     }
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java?rev=1777526&r1=1777525&r2=1777526&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java Thu Jan  5 23:40:57
2017
@@ -131,7 +131,7 @@ public final class ZipPackage extends OP
 
         ZipEntrySource ze;
         try {
-            final ZipFile zipFile = ZipHelper.openZipFile(file);
+            final ZipFile zipFile = ZipHelper.openZipFile(file); // NOSONAR
             ze = new ZipFileZipEntrySource(zipFile);
         } catch (IOException e) {
             // probably not happening with write access - not sure how to handle the default
read-write access ...
@@ -149,7 +149,7 @@ public final class ZipPackage extends OP
         // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
         try {
             // open the file input stream
-            fis = new FileInputStream(file);
+            fis = new FileInputStream(file); // NOSONAR
         } catch (final FileNotFoundException e) {
             // If the source cannot be acquired, abort (no resources to free at this level)
             throw new InvalidOperationException("Can't open the specified file input stream
from file: '" + file + "'", e);
@@ -175,7 +175,7 @@ public final class ZipPackage extends OP
         // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
         try {
             // open the zip input stream
-            zis = ZipHelper.openZipStream(fis);
+            zis = ZipHelper.openZipStream(fis); // NOSONAR
         } catch (final IOException e) {
             // If the source cannot be acquired, abort (no resources to free at this level)
             throw new InvalidOperationException("Could not open the file input stream", e);



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


Mime
View raw message