You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by ma...@apache.org on 2024/03/22 16:11:37 UTC

(opennlp) 01/01: OPENNLP-1545 ZipStream is not closed - fixes the unclosed ZipInputStream issue by adding a try-with-resources block

This is an automated email from the ASF dual-hosted git repository.

mawiesne pushed a commit to branch OPENNLP-1545-ZipStream-is-not-closed
in repository https://gitbox.apache.org/repos/asf/opennlp.git

commit da766a34b05732fa08ec60cfd2b35bd01b9eb534
Author: Martin Wiesner <ma...@hs-heilbronn.de>
AuthorDate: Fri Mar 22 17:11:27 2024 +0100

    OPENNLP-1545 ZipStream is not closed
    - fixes the unclosed ZipInputStream issue by adding a try-with-resources block
---
 .../java/opennlp/tools/util/model/BaseModel.java   | 47 +++++++++++-----------
 1 file changed, 24 insertions(+), 23 deletions(-)

diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
index 468311c5..951ffbd5 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java
@@ -314,40 +314,41 @@ public abstract class BaseModel implements ArtifactProvider, Serializable {
   private void finishLoadingArtifacts(InputStream in)
       throws IOException {
 
-    final ZipInputStream zip = new ZipInputStream(in);
+    try (final ZipInputStream zip = new ZipInputStream(in)) {
+      Map<String, Object> artifactMap = new HashMap<>();
 
-    Map<String, Object> artifactMap = new HashMap<>();
+      ZipEntry entry;
+      while ((entry = zip.getNextEntry()) != null ) {
 
-    ZipEntry entry;
-    while ((entry = zip.getNextEntry()) != null ) {
+        // Note: The manifest.properties file will be read here again,
+        // there should be no need to prevent that.
 
-      // Note: The manifest.properties file will be read here again,
-      // there should be no need to prevent that.
+        String entryName = entry.getName();
+        String extension = getEntryExtension(entryName);
 
-      String entryName = entry.getName();
-      String extension = getEntryExtension(entryName);
+        ArtifactSerializer<?> factory = artifactSerializers.get(extension);
 
-      ArtifactSerializer<?> factory = artifactSerializers.get(extension);
+        String artifactSerializerClazzName =
+                getManifestProperty(SERIALIZER_CLASS_NAME_PREFIX + entryName);
 
-      String artifactSerializerClazzName =
-          getManifestProperty(SERIALIZER_CLASS_NAME_PREFIX + entryName);
+        if (artifactSerializerClazzName != null) {
+          factory = ExtensionLoader.instantiateExtension(
+                  ArtifactSerializer.class, artifactSerializerClazzName);
+        }
 
-      if (artifactSerializerClazzName != null) {
-        factory = ExtensionLoader.instantiateExtension(ArtifactSerializer.class, artifactSerializerClazzName);
-      }
+        if (factory != null) {
+          artifactMap.put(entryName, factory.create(zip));
+        } else {
+          throw new InvalidFormatException("Unknown artifact format: " + extension);
+        }
 
-      if (factory != null) {
-        artifactMap.put(entryName, factory.create(zip));
-      } else {
-        throw new InvalidFormatException("Unknown artifact format: " + extension);
+        zip.closeEntry();
       }
 
-      zip.closeEntry();
-    }
-
-    this.artifactMap.putAll(artifactMap);
+      this.artifactMap.putAll(artifactMap);
 
-    finishedLoadingArtifacts = true;
+      finishedLoadingArtifacts = true;
+    }
   }
 
   /**