From: Andreas Beeker Date: Thu, 5 Jan 2017 23:40:57 +0000 (+0000) Subject: SonarQube fixes X-Git-Tag: REL_3_16_BETA2~47 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fd1ff9f2db4a572f849c995ae339985ef70c7eb7;p=poi.git SonarQube fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1777526 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java index 00dbaccdfc..57df51c1af 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/EmbeddedObjects.java @@ -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 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(); } diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java index edbd6ea3e5..af9d62b85b 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java @@ -53,10 +53,10 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable { 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 Decryptor implements Cloneable { 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 2.3.5.4 RC4 CryptoAPI Encrypted Summary Stream */ 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 Decryptor implements Cloneable { 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 Decryptor implements Cloneable { 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 Decryptor implements Cloneable { 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 Decryptor implements Cloneable { 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 Decryptor implements Cloneable { public CryptoAPICipherInputStream(InputStream stream, long size, int initialPos) throws GeneralSecurityException { super(stream, size, chunkSize, initialPos); - } + } } } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java index d20cea0cb4..5b9e2141e4 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -131,7 +131,7 @@ public final class ZipPackage extends OPCPackage { 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 OPCPackage { // 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 OPCPackage { // 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);