You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2016/07/20 23:20:02 UTC

svn commit: r1753622 - in /poi/trunk/src/scratchpad: src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java testcases/org/apache/poi/hslf/TestReWrite.java

Author: nick
Date: Wed Jul 20 23:20:02 2016
New Revision: 1753622

URL: http://svn.apache.org/viewvc?rev=1753622&view=rev
Log:
#57919 HSLF writing to new File

Modified:
    poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
    poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java

Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java?rev=1753622&r1=1753621&r2=1753622&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java (original)
+++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java Wed Jul 20 23:20:02 2016
@@ -545,18 +545,53 @@ public final class HSLFSlideShowImpl ext
 	
 	@Override
 	public void write() throws IOException {
-	    throw new IllegalStateException("Coming soon!");
+	    throw new IllegalStateException("In-Place write coming soon! See Bug #57919");
 	}
+	
+    /**
+     * Writes out the slideshow file the is represented by an instance
+     *  of this class.
+     * <p>This will write out only the common OLE2 streams. If you require all
+     *  streams to be written out, use {@link #write(File, boolean)}
+     *  with <code>preserveNodes</code> set to <code>true</code>.
+     * @param newFile The File to write to.
+     * @throws IOException If there is an unexpected IOException from writing to the File
+     */
     @Override
     public void write(File newFile) throws IOException {
-        throw new IllegalStateException("Coming soon!");
+        // Write out, but only the common streams
+        write(newFile, false);
+    }
+    /**
+     * Writes out the slideshow file the is represented by an instance
+     *  of this class.
+     * If you require all streams to be written out (eg Marcos, embeded
+     *  documents), then set <code>preserveNodes</code> set to <code>true</code>
+     * @param newFile The File to write to.
+     * @param preserveNodes Should all OLE2 streams be written back out, or only the common ones?
+     * @throws IOException If there is an unexpected IOException from writing to the File
+     */
+    public void write(File newFile, boolean preserveNodes) throws IOException {
+        // Get a new FileSystem to write into
+        POIFSFileSystem outFS = POIFSFileSystem.create(newFile);
+        
+        try {
+            // Write into the new FileSystem
+            write(outFS, preserveNodes);
+
+            // Send the POIFSFileSystem object out to the underlying stream
+            outFS.writeFilesystem();
+        } finally {
+            outFS.close();
+        }
     }
 
     /**
      * Writes out the slideshow file the is represented by an instance
      *  of this class.
-     * It will write out the common OLE2 streams. If you require all
-     *  streams to be written out, pass in preserveNodes
+     * <p>This will write out only the common OLE2 streams. If you require all
+     *  streams to be written out, use {@link #write(OutputStream, boolean)}
+     *  with <code>preserveNodes</code> set to <code>true</code>.
      * @param out The OutputStream to write to.
      * @throws IOException If there is an unexpected IOException from
      *           the passed in OutputStream
@@ -570,7 +605,7 @@ public final class HSLFSlideShowImpl ext
      * Writes out the slideshow file the is represented by an instance
      *  of this class.
      * If you require all streams to be written out (eg Marcos, embeded
-     *  documents), then set preserveNodes to true
+     *  documents), then set <code>preserveNodes</code> set to <code>true</code>
      * @param out The OutputStream to write to.
      * @param preserveNodes Should all OLE2 streams be written back out, or only the common ones?
      * @throws IOException If there is an unexpected IOException from

Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java?rev=1753622&r1=1753621&r2=1753622&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java (original)
+++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java Wed Jul 20 23:20:02 2016
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTru
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
 import java.io.IOException;
 
 import org.apache.poi.POIDataSamples;
@@ -31,6 +32,7 @@ import org.apache.poi.hslf.usermodel.HSL
 import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.TempFile;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -72,31 +74,38 @@ public final class TestReWrite {
     }
     
     public void assertWritesOutTheSame(HSLFSlideShowImpl hss, POIFSFileSystem pfs) throws Exception {
-        // Write out to a byte array
+        // Write out to a byte array, and to a temp file
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         hss.write(baos);
+        
+        final File file = TempFile.createTempFile("TestHSLF", ".ppt");
+        hss.write(file);
+        
 
-        // Build an input stream of it
+        // Build an input stream of it, and read back as a POIFS from the stream
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
-
-        // Use POIFS to query that lot
-        POIFSFileSystem npfs = new POIFSFileSystem(bais);
-
-        // Check that the "PowerPoint Document" sections have the same size
-        DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
-        DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
-        assertEquals(oProps.getSize(),nProps.getSize());
-
-        // Check that they contain the same data
-        byte[] _oData = new byte[oProps.getSize()];
-        byte[] _nData = new byte[nProps.getSize()];
-        pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
-        npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
-        for(int i=0; i<_oData.length; i++) {
-            //System.out.println(i + "\t" + Integer.toHexString(i));
-            assertEquals(_oData[i], _nData[i]);
+        POIFSFileSystem npfS = new POIFSFileSystem(bais);
+        
+        // And the same on the temp file
+        POIFSFileSystem npfF = new POIFSFileSystem(file);
+        
+        for (POIFSFileSystem npf : new POIFSFileSystem[] { npfS, npfF }) {
+            // Check that the "PowerPoint Document" sections have the same size
+            DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
+            DocumentEntry nProps = (DocumentEntry)npf.getRoot().getEntry("PowerPoint Document");
+            assertEquals(oProps.getSize(),nProps.getSize());
+    
+            // Check that they contain the same data
+            byte[] _oData = new byte[oProps.getSize()];
+            byte[] _nData = new byte[nProps.getSize()];
+            pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
+            npf.createDocumentInputStream("PowerPoint Document").read(_nData);
+            for(int i=0; i<_oData.length; i++) {
+                //System.out.println(i + "\t" + Integer.toHexString(i));
+                assertEquals(_oData[i], _nData[i]);
+            }
+            npf.close();
         }
-        npfs.close();
     }
 
     @Test



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