From: Nick Burch Date: Fri, 25 Apr 2014 22:00:30 +0000 (+0000) Subject: When deleting a document in NPOIFS, free the underlying blocks X-Git-Tag: REL_3_11_BETA1~165 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=188c7aa10cce0710a26b2068575e23a0f2bbc05e;p=poi.git When deleting a document in NPOIFS, free the underlying blocks git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590160 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java index 84eca0507c..8d39bbfa24 100644 --- a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java +++ b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java @@ -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; } diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java index a61c7dc655..9ed99f4c44 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java @@ -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; } diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index 06b1fd19f7..f915d84e22 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -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()); }