poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1590577 - in /poi/trunk/src: examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java java/org/apache/poi/poifs/nio/FileBackedDataSource.java
Date Mon, 28 Apr 2014 09:22:58 GMT
Author: nick
Date: Mon Apr 28 09:22:58 2014
New Revision: 1590577

URL: http://svn.apache.org/r1590577
Log:
Update the HPSF change properties example to use NPOIFS + in-place writes, and have NPOIFS
give a helpful error if you try to in-place write on a read only open

Modified:
    poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java

Modified: poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java?rev=1590577&r1=1590576&r2=1590577&view=diff
==============================================================================
--- poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
(original)
+++ poi/trunk/src/examples/src/org/apache/poi/hpsf/examples/ModifyDocumentSummaryInformation.java
Mon Apr 28 09:22:58 2014
@@ -18,12 +18,8 @@
 package org.apache.poi.hpsf.examples;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.util.Date;
 
 import org.apache.poi.hpsf.CustomProperties;
@@ -38,7 +34,7 @@ import org.apache.poi.hpsf.WritingNotSup
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.DocumentEntry;
 import org.apache.poi.poifs.filesystem.DocumentInputStream;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 
 /**
  * <p>This is a sample application showing how to easily modify properties in
@@ -99,12 +95,10 @@ public class ModifyDocumentSummaryInform
         /* Read the name of the POI filesystem to modify from the command line.
          * For brevity to boundary check is performed on the command-line
          * arguments. */
-        File poiFilesystem = new File(args[0]);
+        File summaryFile = new File(args[0]);
 
         /* Open the POI filesystem. */
-        InputStream is = new FileInputStream(poiFilesystem);
-        POIFSFileSystem poifs = new POIFSFileSystem(is);
-        is.close();
+        NPOIFSFileSystem poifs = new NPOIFSFileSystem(summaryFile, false);
 
         /* Read the summary information. */
         DirectoryEntry dir = poifs.getRoot();
@@ -174,6 +168,7 @@ public class ModifyDocumentSummaryInform
 
         /* Read a custom property. */
         Object value = customProperties.get("Sample Number");
+        System.out.println("Custom Sample Number is now " + value);
 
         /* Write the custom properties back to the document summary
          * information. */
@@ -185,10 +180,9 @@ public class ModifyDocumentSummaryInform
         dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
 
         /* Write the POI filesystem back to the original file. Please note that
-         * in production code you should never write directly to the origin
-         * file! In case of a writing error everything would be lost. */
-        OutputStream out = new FileOutputStream(poiFilesystem);
-        poifs.writeFilesystem(out);
-        out.close();
+         * in production code you should take care when write directly to the 
+         * origin, to make sure you don't loose things on error */
+        poifs.writeFilesystem();
+        poifs.close();
     }
 }

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java?rev=1590577&r1=1590576&r2=1590577&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java Mon Apr 28 09:22:58
2014
@@ -693,6 +693,12 @@ public class NPOIFSFileSystem extends Bl
                 "not be called. Use writeFilesystem(OutputStream) instead"
           );
        }
+       if (! ((FileBackedDataSource)_data).isWriteable()) {
+           throw new IllegalArgumentException(
+                "POIFS opened in read only mode, so writeFilesystem() may " +
+                "not be called. Open the FileSystem in read-write mode first"
+           );
+       }
        syncWithDataSource();
     }
 

Modified: poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java?rev=1590577&r1=1590576&r2=1590577&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java Mon Apr 28 09:22:58
2014
@@ -49,6 +49,10 @@ public class FileBackedDataSource extend
       this.channel = channel;
       this.writable = !readOnly;
    }
+   
+   public boolean isWriteable() {
+       return this.writable;
+   }
 
    @Override
    public ByteBuffer read(int length, long position) throws IOException {



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


Mime
View raw message