From 49c62ef73df03158725eddc15681b685aadc1e00 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sun, 13 Mar 2016 14:12:43 +0000 Subject: [PATCH] sonar fix git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734809 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hslf/dev/PPTXMLDump.java | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java index cf6f6bc412..a9fec1bdb1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java @@ -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"); + 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"); - protected Writer out; - protected byte[] docstream; - protected byte[] pictstream; - protected boolean hexHeader = true; + 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 Writer 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); -- 2.39.5