Ignore:
Timestamp:
2025-04-02T17:09:56+02:00 (5 weeks ago)
Author:
stoecker
Message:

fix resource leaks for cached Zip files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r19211 r19372  
    316316     */
    317317    public String findZipEntryPath(String extension, String namepart) {
    318         Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
     318        Pair<String, Pair<ZipFile, InputStream>> ze = findZipEntryImpl(extension, namepart);
    319319        if (ze == null) return null;
     320        Utils.close(ze.b.b);
     321        Utils.close(ze.b.a);
    320322        return ze.a;
    321323    }
     
    328330     * doesn't represent a zip file or if there was no matching
    329331     * file in the ZIP file.
    330      * @since 6148
    331      */
    332     public InputStream findZipEntryInputStream(String extension, String namepart) {
    333         Pair<String, InputStream> ze = findZipEntryImpl(extension, namepart);
     332     * The returned ZipFile must be closed after use.
     333     * @since 19372
     334     */
     335    public Pair<ZipFile, InputStream> findZipEntryInputStream(String extension, String namepart) {
     336        Pair<String, Pair<ZipFile, InputStream>> ze = findZipEntryImpl(extension, namepart);
    334337        if (ze == null) return null;
    335338        return ze.b;
    336339    }
    337340
    338     private Pair<String, InputStream> findZipEntryImpl(String extension, String namepart) {
     341    private Pair<String, Pair<ZipFile, InputStream>> findZipEntryImpl(String extension, String namepart) {
    339342        File file = null;
    340343        try {
     
    345348        if (file == null)
    346349            return null;
    347         Pair<String, InputStream> res = null;
     350        Pair<String, Pair <ZipFile, InputStream>> res = null;
    348351        try {
    349             ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8); // NOPMD
     352            ZipFile zipFile = new ZipFile(file, StandardCharsets.UTF_8);
    350353            ZipEntry resentry = null;
    351354            Enumeration<? extends ZipEntry> entries = zipFile.entries();
     
    358361            }
    359362            if (resentry != null) {
    360                 InputStream is = zipFile.getInputStream(resentry); // NOPMD
    361                 res = Pair.create(resentry.getName(), is);
     363                InputStream is = zipFile.getInputStream(resentry);
     364                res = Pair.create(resentry.getName(), Pair.create(zipFile, is));
    362365            } else {
    363366                Utils.close(zipFile);
Note: See TracChangeset for help on using the changeset viewer.