You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2014/04/26 00:40:51 UTC

svn commit: r1590177 - /poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java

Author: nick
Date: Fri Apr 25 22:40:51 2014
New Revision: 1590177

URL: http://svn.apache.org/r1590177
Log:
Support for replacing the contents of a Document in a NPOIFSFileSytem, in place

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java?rev=1590177&r1=1590176&r2=1590177&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java Fri Apr 25 22:40:51 2014
@@ -46,6 +46,14 @@ public final class NPOIFSDocument implem
    /**
     * Constructor for an existing Document 
     */
+   public NPOIFSDocument(DocumentNode document) throws IOException {
+       this((DocumentProperty)document.getProperty(), 
+            ((DirectoryNode)document.getParent()).getNFileSystem());
+   }
+   
+   /**
+    * Constructor for an existing Document 
+    */
    public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem) 
       throws IOException
    {
@@ -72,32 +80,10 @@ public final class NPOIFSDocument implem
    {
       this._filesystem = filesystem;
 
-      final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
-      BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
-      bis.mark(bigBlockSize);
-
-      // Do we need to store as a mini stream or a full one?
-      if(bis.skip(bigBlockSize) < bigBlockSize) {
-         _stream = new NPOIFSStream(filesystem.getMiniStore());
-         _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
-      } else {
-         _stream = new NPOIFSStream(filesystem);
-         _block_size = _filesystem.getBlockStoreBlockSize();
-      }
-
-      // start from the beginning 
-      bis.reset();
-      
       // Store it
-      OutputStream os = _stream.getOutputStream();
-      byte buf[] = new byte[1024];
-      int length = 0;
-      
-      for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
-          os.write(buf, 0, readBytes);
-      }
+      int length = store(stream);
 
-      // And build the property for it
+      // Build the property for it
       this._property = new DocumentProperty(name, length);
       _property.setStartBlock(_stream.getStartBlock());     
    }
@@ -129,6 +115,38 @@ public final class NPOIFSDocument implem
    }
    
    /**
+    * Stores the given data for this Document
+    */
+   private int store(InputStream stream) throws IOException {
+       final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
+       BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
+       bis.mark(bigBlockSize);
+
+       // Do we need to store as a mini stream or a full one?
+       if(bis.skip(bigBlockSize) < bigBlockSize) {
+          _stream = new NPOIFSStream(_filesystem.getMiniStore());
+          _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
+       } else {
+          _stream = new NPOIFSStream(_filesystem);
+          _block_size = _filesystem.getBlockStoreBlockSize();
+       }
+
+       // start from the beginning 
+       bis.reset();
+       
+       // Store it
+       OutputStream os = _stream.getOutputStream();
+       byte buf[] = new byte[1024];
+       int length = 0;
+       
+       for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
+           os.write(buf, 0, readBytes);
+       }
+       
+       return length;
+   }
+   
+   /**
     * Frees the underlying stream and property
     */
    void free() throws IOException {
@@ -155,6 +173,11 @@ public final class NPOIFSDocument implem
    public int getSize() {
       return _property.getSize();
    }
+   
+   public void replaceContents(InputStream stream) throws IOException {
+       free();
+       store(stream);
+   }
 
    /**
     * @return the instance's DocumentProperty



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