]> source.dussan.org Git - poi.git/commitdiff
When deleting a document in NPOIFS, free the underlying blocks
authorNick Burch <nick@apache.org>
Fri, 25 Apr 2014 22:00:30 +0000 (22:00 +0000)
committerNick Burch <nick@apache.org>
Fri, 25 Apr 2014 22:00:30 +0000 (22:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590160 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java
src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java
src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java

index 84eca0507c3042acbd5b28b223a12dba521f1da2..8d39bbfa24a81633b51afc6a7472fe31c5f9891a 100644 (file)
@@ -290,13 +290,17 @@ public class DirectoryNode
         if (rval)
         {
             _entries.remove(entry);
-               _byname.remove(entry.getName());
-
-               if(_ofilesystem != null) {
-               _ofilesystem.remove(entry);
-               } else {
-                  _nfilesystem.remove(entry);
-               }
+            _byname.remove(entry.getName());
+
+            if(_ofilesystem != null) {
+                _ofilesystem.remove(entry);
+            } else {
+                try {
+                    _nfilesystem.remove(entry);
+                } catch (IOException e) {
+                    // TODO Work out how to report this, given we can't change the method signature...
+                }
+            }
         }
         return rval;
     }
index a61c7dc65514381483a19a1e447a27dc7dc018fd..9ed99f4c44c072842a69c9eab512e8ce78e8077a 100644 (file)
@@ -128,6 +128,14 @@ public final class NPOIFSDocument implements POIFSViewable {
        _property.setStartBlock(_stream.getStartBlock());     
    }
    
+   /**
+    * Frees the underlying stream and property
+    */
+   void free() throws IOException {
+       _stream.free();
+       _property.setStartBlock(POIFSConstants.END_OF_CHAIN);
+   }
+   
    int getDocumentBlockSize() {
       return _block_size;
    }
index 06b1fd19f77068d72dd977b1349d5c137d3807ab..f915d84e22d99916f4bd20576874f3dfc1a606b8 100644 (file)
@@ -44,6 +44,7 @@ import org.apache.poi.poifs.nio.ByteArrayBackedDataSource;
 import org.apache.poi.poifs.nio.DataSource;
 import org.apache.poi.poifs.nio.FileBackedDataSource;
 import org.apache.poi.poifs.property.DirectoryProperty;
+import org.apache.poi.poifs.property.DocumentProperty;
 import org.apache.poi.poifs.property.NPropertyTable;
 import org.apache.poi.poifs.storage.BATBlock;
 import org.apache.poi.poifs.storage.BATBlock.BATBlockAndIndex;
@@ -431,7 +432,11 @@ public class NPOIFSFileSystem extends BlockStore
        // The header block doesn't count, so add one
        long blockWanted = offset + 1;
        long startAt = blockWanted * bigBlockSize.getBigBlockSize();
-       return _data.read(bigBlockSize.getBigBlockSize(), startAt);
+       try {
+           return _data.read(bigBlockSize.getBigBlockSize(), startAt);
+       } catch (IndexOutOfBoundsException e) {
+           throw new IndexOutOfBoundsException("Block " + offset + " not found - " + e);
+       }
     }
     
     /**
@@ -820,9 +825,15 @@ public class NPOIFSFileSystem extends BlockStore
      *
      * @param entry to be removed
      */
-
-    void remove(EntryNode entry)
+    void remove(EntryNode entry) throws IOException
     {
+        // If it's a document, free the blocks
+        if (entry instanceof DocumentEntry) {
+            NPOIFSDocument doc = new NPOIFSDocument((DocumentProperty)entry.getProperty(), this);
+            doc.free();
+        }
+        
+        // Now zap it from the properties list
         _property_table.removeProperty(entry.getProperty());
     }