Changeset 14724 in josm for trunk/src/org


Ignore:
Timestamp:
2019-01-23T22:20:33+01:00 (6 years ago)
Author:
simon04
Message:

see #16497 - Use NIO.2 in SessionReader

File:
1 edited

Legend:

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

    r14201 r14724  
    1313import java.net.URI;
    1414import java.net.URISyntaxException;
    15 import java.nio.charset.StandardCharsets;
     15import java.nio.file.FileSystem;
     16import java.nio.file.FileSystems;
    1617import java.nio.file.Files;
     18import java.nio.file.Path;
    1719import java.util.ArrayList;
    1820import java.util.Collection;
    1921import java.util.Collections;
    20 import java.util.Enumeration;
    2122import java.util.HashMap;
    2223import java.util.List;
     
    2425import java.util.Map.Entry;
    2526import java.util.TreeMap;
    26 import java.util.zip.ZipEntry;
    27 import java.util.zip.ZipException;
    28 import java.util.zip.ZipFile;
     27import java.util.stream.Stream;
    2928
    3029import javax.swing.JOptionPane;
     
    151150    private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>();
    152151
    153     private URI sessionFileURI;
     152    private Path sessionFile;
    154153    private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
    155     private ZipFile zipFile;
     154    private FileSystem zipFile;
    156155    private List<Layer> layers = new ArrayList<>();
    157156    private int active = -1;
     
    292291         *     - file inside zip archive:
    293292         *         "layers/01/data.osm"
    294          *     - relativ to the .joz file:
     293         *     - relative to the .joz file:
    295294         *         "../save/data.osm"           ("../" steps out of the archive)
    296295         * @param uriStr URI as string
     
    308307                }
    309308            } else if (inZipPath != null) {
    310                 ZipEntry entry = zipFile.getEntry(inZipPath);
    311                 if (entry != null) {
    312                     return zipFile.getInputStream(entry);
    313                 }
     309                return Files.newInputStream(zipFile.getPath(inZipPath));
    314310            }
    315311            throw new IOException(tr("Unable to locate file  ''{0}''.", uriStr));
     
    345341                                // relative to session file - "../" step out of the archive
    346342                                String relPath = uri.getPath().substring(3);
    347                                 return new File(sessionFileURI.resolve(relPath));
     343                                return sessionFile.resolveSibling(relPath).toFile();
    348344                            } else {
    349345                                // file inside zip archive
     
    352348                            }
    353349                        } else
    354                             return new File(sessionFileURI.resolve(uri));
     350                            return sessionFile.resolveSibling(uriStr).toFile();
    355351                    }
    356352                } else
     
    704700    public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException {
    705701        try (InputStream josIS = createInputStream(sessionFile, zip)) {
    706             loadSession(josIS, sessionFile.toURI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);
     702            loadSession(josIS, sessionFile.toPath(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);
    707703        }
    708704    }
     
    711707        if (zip) {
    712708            try {
    713                 zipFile = new ZipFile(sessionFile, StandardCharsets.UTF_8);
    714                 return getZipInputStream(zipFile);
    715             } catch (ZipException ex) {
    716                 throw new IOException(ex);
     709                // read zip using ZipFileSystem/ZipFileSystemProvider
     710                zipFile = FileSystems.newFileSystem(new URI("jar:" + sessionFile.toURI()), Collections.emptyMap());
     711                try (Stream<Path> content = Files.list(zipFile.getPath("/"))) {
     712                    final Path jos = content
     713                            .filter(path -> Utils.hasExtension(path.getFileName().toString(), "jos"))
     714                            .findFirst()
     715                            .orElseThrow(() -> new IllegalDataException(tr("expected .jos file inside .joz archive")));
     716                    return Files.newInputStream(jos);
     717                }
     718            } catch (URISyntaxException e) {
     719                throw new IOException(e);
    717720            }
    718721        } else {
     
    721724    }
    722725
    723     private static InputStream getZipInputStream(ZipFile zipFile) throws IOException, IllegalDataException {
    724         ZipEntry josEntry = null;
    725         Enumeration<? extends ZipEntry> entries = zipFile.entries();
    726         while (entries.hasMoreElements()) {
    727             ZipEntry entry = entries.nextElement();
    728             if (Utils.hasExtension(entry.getName(), "jos")) {
    729                 josEntry = entry;
    730                 break;
    731             }
    732         }
    733         if (josEntry == null) {
    734             error(tr("expected .jos file inside .joz archive"));
    735         }
    736         return zipFile.getInputStream(josEntry);
    737     }
    738 
    739     private void loadSession(InputStream josIS, URI sessionFileURI, boolean zip, ProgressMonitor progressMonitor)
     726    private void loadSession(InputStream josIS, Path sessionFile, boolean zip, ProgressMonitor progressMonitor)
    740727            throws IOException, IllegalDataException {
    741728
    742         this.sessionFileURI = sessionFileURI;
     729        this.sessionFile = sessionFile;
    743730        this.zip = zip;
    744731
Note: See TracChangeset for help on using the changeset viewer.