This is an automated email from the ASF dual-hosted git repository. rzo1 pushed a commit to branch OPENNLP-1545 in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit b6605c26f8c3e453e8f2ca9b5447c9e994a7136d Author: Richard Zowalla <[email protected]> AuthorDate: Tue May 20 20:41:44 2025 +0200 OPENNLP-1545 - Close ZipInputStream in BaseModel --- .../java/opennlp/tools/util/model/BaseModel.java | 51 +++++++++++----------- 1 file changed, 26 insertions(+), 25 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 106a32e8..35e47763 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 @@ -238,42 +238,43 @@ public abstract class BaseModel implements ArtifactProvider, Serializable { int MODEL_BUFFER_SIZE_LIMIT = Integer.MAX_VALUE; in.mark(MODEL_BUFFER_SIZE_LIMIT); - final ZipInputStream zip = new ZipInputStream(in); + try (final ZipInputStream zip = new ZipInputStream(in)) { - // The model package can contain artifacts which are serialized with 3rd party - // serializers which are configured in the manifest file. To be able to load - // the model the manifest must be read first, and afterwards all the artifacts - // can be de-serialized. + // The model package can contain artifacts which are serialized with 3rd party + // serializers which are configured in the manifest file. To be able to load + // the model the manifest must be read first, and afterwards all the artifacts + // can be de-serialized. - // The ordering of artifacts in a zip package is not guaranteed. The stream is first - // read until the manifest appears, reseted, and read again to load all artifacts. + // The ordering of artifacts in a zip package is not guaranteed. The stream is first + // read until the manifest appears, reset, and read again to load all artifacts. - boolean isSearchingForManifest = true; + boolean isSearchingForManifest = true; - ZipEntry entry; - while ((entry = zip.getNextEntry()) != null && isSearchingForManifest) { + ZipEntry entry; + while ((entry = zip.getNextEntry()) != null && isSearchingForManifest) { - if ("manifest.properties".equals(entry.getName())) { - // TODO: Probably better to use the serializer here directly! - ArtifactSerializer<?> factory = artifactSerializers.get("properties"); - artifactMap.put(entry.getName(), factory.create(zip)); - isSearchingForManifest = false; - } + if ("manifest.properties".equals(entry.getName())) { + // TODO: Probably better to use the serializer here directly! + ArtifactSerializer<?> factory = artifactSerializers.get("properties"); + artifactMap.put(entry.getName(), factory.create(zip)); + isSearchingForManifest = false; + } - zip.closeEntry(); - } + zip.closeEntry(); + } - initializeFactory(); + initializeFactory(); - loadArtifactSerializers(); + loadArtifactSerializers(); - // The Input Stream should always be reset-able because if markSupport returns - // false it is wrapped before hand into an Buffered InputStream - in.reset(); + // The Input Stream should always be reset-able because if markSupport returns + // false it is wrapped before hand into an Buffered InputStream + in.reset(); - finishLoadingArtifacts(in); + finishLoadingArtifacts(in); - checkArtifactMap(); + checkArtifactMap(); + } } private void initializeFactory() throws InvalidFormatException {
