#57919 HSLF writing to new File

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753622 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-07-20 23:20:02 +00:00
parent d240667115
commit af36e63bff
2 changed files with 69 additions and 25 deletions

View File

@ -545,18 +545,53 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable {
@Override
public void write() throws IOException {
throw new IllegalStateException("Coming soon!");
}
@Override
public void write(File newFile) 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.
* 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(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 {
// 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.
* <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

View File

@ -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);
// Build an input stream of it
final File file = TempFile.createTempFile("TestHSLF", ".ppt");
hss.write(file);
// Build an input stream of it, and read back as a POIFS from the stream
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
POIFSFileSystem npfS = new POIFSFileSystem(bais);
// Use POIFS to query that lot
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)npfs.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);
npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
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]);
}
npfs.close();
npf.close();
}
}
@Test