From b25e988d943ea6ba523e927788f6f219a14570c3 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 20 Jul 2016 23:31:57 +0000 Subject: [PATCH] If doing an in-place write, update the properties too git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753623 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/POIDocument.java | 14 +++++++++++++- .../apache/poi/hssf/usermodel/HSSFWorkbook.java | 3 +++ .../poi/poifs/filesystem/NPOIFSFileSystem.java | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/POIDocument.java b/src/java/org/apache/poi/POIDocument.java index 80029e373d..06f101d3d6 100644 --- a/src/java/org/apache/poi/POIDocument.java +++ b/src/java/org/apache/poi/POIDocument.java @@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -34,6 +35,8 @@ import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentInputStream; +import org.apache.poi.poifs.filesystem.DocumentNode; +import org.apache.poi.poifs.filesystem.NPOIFSDocument; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -298,7 +301,16 @@ public abstract class POIDocument implements Closeable { mSet.write(bOut); byte[] data = bOut.toByteArray(); ByteArrayInputStream bIn = new ByteArrayInputStream(data); - outFS.createDocument(bIn,name); + + // New or Existing? + // TODO Use a createOrUpdate method for this to be cleaner! + try { + DocumentNode propSetNode = (DocumentNode)outFS.getRoot().getEntry(name); + NPOIFSDocument propSetDoc = new NPOIFSDocument(propSetNode); + propSetDoc.replaceContents(bIn); + } catch (FileNotFoundException e) { + outFS.createDocument(bIn,name); + } logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length); } catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 402b5d7e40..32789ca96d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -1311,6 +1311,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss NPOIFSDocument workbookDoc = new NPOIFSDocument(workbookNode); workbookDoc.replaceContents(new ByteArrayInputStream(getBytes())); + // Update the properties streams in the file + writeProperties(directory.getFileSystem(), null); + // Sync with the File on disk directory.getFileSystem().writeFilesystem(); } diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index e1d4c5e5b5..e81888d06f 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -689,6 +689,8 @@ public class NPOIFSFileSystem extends BlockStore return getRoot().createDocument(name, stream); } + // TODO Add a createOrUpdateDocument method to simplify code + /** * create a new DocumentEntry in the root entry; the data will be * provided later -- 2.39.5