diff options
author | Nick Burch <nick@apache.org> | 2016-07-21 23:09:07 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2016-07-21 23:09:07 +0000 |
commit | 6d59eef6e47f6266b3f64d2a90c9cbe153b8cb90 (patch) | |
tree | 193963e0248bb5cbba10c18a377593cbc757f366 /src/java/org/apache/poi/poifs/filesystem | |
parent | 48f255f37e272fa4f131d534753af21159432420 (diff) | |
download | poi-6d59eef6e47f6266b3f64d2a90c9cbe153b8cb90.tar.gz poi-6d59eef6e47f6266b3f64d2a90c9cbe153b8cb90.zip |
Provide a createOrUpdateDocument method at the POIFS level, and use to simplify writing code #57919
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753739 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/poifs/filesystem')
-rw-r--r-- | src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java | 32 | ||||
-rw-r--r-- | src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java | 22 |
2 files changed, 52 insertions, 2 deletions
diff --git a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java index 9be71a5ccf..ccb008507b 100644 --- a/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java +++ b/src/java/org/apache/poi/poifs/filesystem/DirectoryNode.java @@ -479,6 +479,38 @@ public class DirectoryNode } /** + * Set the contents of a document, creating if needed, + * otherwise updating. Returns the created / updated DocumentEntry + * + * @param name the name of the new or existing DocumentEntry + * @param stream the InputStream from which to populate the DocumentEntry + * + * @return the new or updated DocumentEntry + * + * @exception IOException + */ + + public DocumentEntry createOrUpdateDocument(final String name, + final InputStream stream) + throws IOException + { + if (! hasEntry(name)) { + return createDocument(name, stream); + } else { + DocumentNode existing = (DocumentNode)getEntry(name); + if (_nfilesystem != null) { + NPOIFSDocument nDoc = new NPOIFSDocument(existing); + nDoc.replaceContents(stream); + return existing; + } else { + // Do it the hard way for Old POIFS... + deleteEntry(existing); + return createDocument(name, stream); + } + } + } + + /** * Gets the storage clsid of the directory entry * * @return storage Class ID diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index e81888d06f..f987c43b31 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -689,8 +689,6 @@ 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 @@ -728,6 +726,26 @@ public class NPOIFSFileSystem extends BlockStore } /** + * Set the contents of a document in the root directory, + * creating if needed, otherwise updating + * + * @param stream the InputStream from which the document's data + * will be obtained + * @param name the name of the new or existing POIFSDocument + * + * @return the new or updated DocumentEntry + * + * @exception IOException on error populating the POIFSDocument + */ + + public DocumentEntry createOrUpdateDocument(final InputStream stream, + final String name) + throws IOException + { + return getRoot().createOrUpdateDocument(name, stream); + } + + /** * Does the filesystem support an in-place write via * {@link #writeFilesystem()} ? If false, only writing out to * a brand new file via {@link #writeFilesystem(OutputStream)} |