Author: kiwiwings
Date: Sun Mar 13 14:12:43 2016
New Revision: 1734809

URL: http://svn.apache.org/viewvc?rev=1734809&view=rev
Log:
sonar fix

Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java?rev=1734809&r1=1734808&r2=1734809&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java 
(original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java Sun 
Mar 13 14:12:43 2016
@@ -17,63 +17,64 @@
 
 package org.apache.poi.hslf.dev;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.nio.charset.Charset;
 
 import org.apache.poi.hslf.record.RecordTypes;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.LittleEndian;
 
 /**
  * Utility class which dumps raw contents of a ppt file into XML format
- *
- * @author Yegor Kozlov
  */
 
 public final class PPTXMLDump {
-    public static final int HEADER_SIZE = 8; //size of the record header
-    public static final int PICT_HEADER_SIZE = 25; //size of the picture header
-    public final static String PPDOC_ENTRY = "PowerPoint Document";
-    public final static String PICTURES_ENTRY = "Pictures";
-    public final static String CR = System.getProperty("line.separator");
-
-    protected Writer out;
-    protected byte[] docstream;
-    protected byte[] pictstream;
-    protected boolean hexHeader = true;
+    private static final int HEADER_SIZE = 8; //size of the record header
+    private static final int PICT_HEADER_SIZE = 25; //size of the picture 
header
+    private static final String PPDOC_ENTRY = "PowerPoint Document";
+    private static final String PICTURES_ENTRY = "Pictures";
+    private static final String CR = System.getProperty("line.separator");
+
+    private Writer out;
+    private byte[] docstream;
+    private byte[] pictstream;
+    private boolean hexHeader = true;
 
     public PPTXMLDump(File ppt) throws IOException {
         NPOIFSFileSystem fs = new NPOIFSFileSystem(ppt, true);
-        DocumentInputStream is = null;
-        
         try {
-            //read the document entry from OLE file system
-            DocumentEntry entry = 
(DocumentEntry)fs.getRoot().getEntry(PPDOC_ENTRY);
-            docstream = new byte[entry.getSize()];
-            is = fs.createDocumentInputStream(PPDOC_ENTRY);
-            is.read(docstream);
-            is.close();
-
-            entry = (DocumentEntry)fs.getRoot().getEntry(PICTURES_ENTRY);
-            pictstream = new byte[entry.getSize()];
-            is = fs.createDocumentInputStream(PICTURES_ENTRY);
-            is.read(pictstream);
-        } catch(FileNotFoundException e){
-            //silently catch errors if the presentation does not contain 
pictures
+            docstream = readEntry(fs, PPDOC_ENTRY);
+            pictstream = readEntry(fs, PICTURES_ENTRY);
         } finally {
-            if (is != null) is.close();
             fs.close();
         }
     }
 
+    private static byte[] readEntry(NPOIFSFileSystem fs, String entry)
+    throws IOException {
+        DirectoryNode dn = fs.getRoot();
+        if (!dn.hasEntry(entry)) {
+            return null;
+        }
+        InputStream is = dn.createDocumentInputStream(entry);
+        try {
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            IOUtils.copy(is, bos);
+            return bos.toByteArray();
+        } finally {
+            is.close();
+        }
+    }
+    
     /**
      * Dump the structure of the supplied PPT file into XML
      * @param outWriter <code>Writer</code> to write out
@@ -111,7 +112,9 @@ public final class PPTXMLDump {
     public void dump(byte[] data, int offset, int length, int padding) throws 
IOException {
         int pos = offset;
         while (pos <= (offset + length - HEADER_SIZE)){
-            if (pos < 0) break;
+            if (pos < 0) {
+                break;
+            }
 
             //read record header
             int info = LittleEndian.getUShort(data, pos);



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to