diff options
author | Dominik Stadler <centic@apache.org> | 2023-12-06 19:51:19 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2023-12-06 19:51:19 +0000 |
commit | d021e6bc5e84913dd06bb5d29596bf26df4ca98f (patch) | |
tree | f857c022c2a85329ee979d4bee6251f60b759864 /poi-ooxml | |
parent | fd9300d3b776738ce6d9cf82ced8dd149a7ebef3 (diff) | |
download | poi-d021e6bc5e84913dd06bb5d29596bf26df4ca98f.tar.gz poi-d021e6bc5e84913dd06bb5d29596bf26df4ca98f.zip |
Simplify handling of exceptions in ZipPackage
The code became overly complex and hard to reason about.
We can avoid some additional catching/rethrowing of exceptions.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1914409 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java | 46 |
1 files changed, 10 insertions, 36 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java index 5c3ce54572..cc650df588 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -210,54 +210,28 @@ public final class ZipPackage extends OPCPackage { throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e); } + ZipArchiveThresholdInputStream zis = null; // If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource try { // read from the file input stream - return openZipEntrySourceStream(fis); - } catch (final InvalidOperationException|UnsupportedFileFormatException e) { - // abort: close the zip input stream - IOUtils.closeQuietly(fis); - throw e; - } catch (final Exception e) { - // abort: close the file input stream - IOUtils.closeQuietly(fis); - throw new InvalidOperationException("Failed to read the file input stream from file: '" + file + "'", e); - } - } - - private static ZipEntrySource openZipEntrySourceStream(InputStream fis) throws InvalidOperationException { - final ZipArchiveThresholdInputStream zis; - // Acquire a resource that is needed to read the next level of openZipEntrySourceStream - try { - // open the zip input stream + // Acquire a resource that is needed to read the next level of openZipEntrySourceStream zis = ZipHelper.openZipStream(fis); // NOSONAR - } catch (final IOException e) { - // If the source cannot be acquired, abort (no resources to free at this level) - throw new InvalidOperationException("Could not open the file input stream", e); - } - // If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource - try { + // If an error occurs while reading the next level of openZipEntrySourceStream, free the acquired resource // read from the zip input stream - return openZipEntrySourceStream(zis); + + // Acquire the final level resource. If this is acquired successfully, the zip package was read successfully from the input stream + return new ZipInputStreamZipEntrySource(zis); } catch (final InvalidOperationException|UnsupportedFileFormatException e) { // abort: close the zip input stream + IOUtils.closeQuietly(fis); IOUtils.closeQuietly(zis); throw e; } catch (final Exception e) { - // abort: close the zip input stream + // abort: close the file input stream + IOUtils.closeQuietly(fis); IOUtils.closeQuietly(zis); - throw new InvalidOperationException("Failed to read the zip entry source stream", e); - } - } - - private static ZipEntrySource openZipEntrySourceStream(ZipArchiveThresholdInputStream zis) throws InvalidOperationException { - // Acquire the final level resource. If this is acquired successfully, the zip package was read successfully from the input stream - try { - // open the zip entry source stream - return new ZipInputStreamZipEntrySource(zis); - } catch (IOException e) { - throw new InvalidOperationException("Could not open the specified zip entry source stream", e); + throw new InvalidOperationException("Failed to read the file input stream from file: '" + file + "'", e); } } |