aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2016-07-20 23:20:02 +0000
committerNick Burch <nick@apache.org>2016-07-20 23:20:02 +0000
commit2750df0433d1f90c163fadbaa8cd2d706cd19c48 (patch)
tree2a9414df055efbcc355471b9d948214f686fb040
parent2d44399e0b3ee5a3b16ac2b0ae8bcb63c1cbfcb8 (diff)
downloadpoi-2750df0433d1f90c163fadbaa8cd2d706cd19c48.tar.gz
poi-2750df0433d1f90c163fadbaa8cd2d706cd19c48.zip
#57919 HSLF writing to new File
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753622 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java45
-rw-r--r--src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java49
2 files changed, 69 insertions, 25 deletions
diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
index 223e6036e4..1adfcff069 100644
--- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
+++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java
@@ -545,18 +545,53 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable {
@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 extends POIDocument implements Closeable {
* 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
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java b/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java
index 10e6225706..06bfd1798b 100644
--- a/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java
+++ b/src/scratchpad/testcases/org/apache/poi/hslf/TestReWrite.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
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.HSLFSlideShow;
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