]> source.dussan.org Git - poi.git/commitdiff
If doing an in-place write, update the properties too
authorNick Burch <nick@apache.org>
Wed, 20 Jul 2016 23:31:57 +0000 (23:31 +0000)
committerNick Burch <nick@apache.org>
Wed, 20 Jul 2016 23:31:57 +0000 (23:31 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753623 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/POIDocument.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java

index 80029e373d263e8464d1643499163bfcbdadc58d..06f101d3d6c77088c9ecca061541fd3eaf856eb3 100644 (file)
@@ -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) {
index 402b5d7e40a1ae9ff6ffc0a62c0982862bebc374..32789ca96d2f9c851fa92552e4cfcc46062ce437 100644 (file)
@@ -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();
     }
index e1d4c5e5b54e1f558d54041c1a870b373b9f47f2..e81888d06ff3b446e30dfc519a115c46b270f8da 100644 (file)
@@ -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