Changeset 13356 in josm for trunk


Ignore:
Timestamp:
2018-01-26T21:37:00+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #15662 - allow JOSM to open JAR URLs containing exclamation marks (workaround to https://bugs.openjdk.java.net/browse/JDK-4523159)

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r13294 r13356  
    156156import org.openstreetmap.josm.tools.FontsManager;
    157157import org.openstreetmap.josm.tools.GBC;
    158 import org.openstreetmap.josm.tools.HttpClient;
    159158import org.openstreetmap.josm.tools.I18n;
    160159import org.openstreetmap.josm.tools.ImageProvider;
     
    955954            for (String i : args.get(Option.LOAD_PREFERENCES)) {
    956955                Logging.info("Reading preferences from " + i);
    957                 try (InputStream is = openStream(new URL(i))) {
     956                try (InputStream is = Utils.openStream(new URL(i))) {
    958957                    config.openAndReadXML(is);
    959958                } catch (IOException ex) {
     
    12061205                }
    12071206            }
    1208         }
    1209     }
    1210 
    1211     private static InputStream openStream(URL url) throws IOException {
    1212         if ("file".equals(url.getProtocol())) {
    1213             return url.openStream();
    1214         } else {
    1215             return HttpClient.create(url).connect().getContent();
    12161207        }
    12171208    }
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r13352 r13356  
    164164            }
    165165
    166             if ("file".equals(url.getProtocol())) {
     166            String protocol = url.getProtocol();
     167            if ("file".equals(protocol) || "jar".equals(protocol)) {
    167168                try {
    168                     return url.openStream();
     169                    return Utils.openStream(url);
    169170                } catch (IOException e) {
    170171                    throw new OsmTransferException(e);
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r13204 r13356  
    419419        }
    420420        try (
    421             InputStream enStream = en.openStream();
    422             InputStream trStream = tr.openStream()
     421            InputStream enStream = Utils.openStream(en);
     422            InputStream trStream = Utils.openStream(tr)
    423423        ) {
    424424            if (load(enStream, trStream, false)) {
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r13311 r13356  
    17181718        CheckParameterUtil.ensureParameterNotNull(input, "input");
    17191719
    1720         InputStream istream = null;
    1721         try {
    1722             istream = input.openStream();
     1720        try (InputStream istream = Utils.openStream(input)) {
     1721            ImageInputStream stream = ImageIO.createImageInputStream(istream);
     1722            BufferedImage bi = read(stream, readMetadata, enforceTransparency);
     1723            if (bi == null) {
     1724                stream.close();
     1725            }
     1726            return bi;
    17231727        } catch (IOException e) {
    17241728            throw new IIOException("Can't get input stream from URL!", e);
    17251729        }
    1726         ImageInputStream stream = ImageIO.createImageInputStream(istream);
    1727         BufferedImage bi;
    1728         try {
    1729             bi = read(stream, readMetadata, enforceTransparency);
    1730             if (bi == null) {
    1731                 stream.close();
    1732             }
    1733         } finally {
    1734             istream.close();
    1735         }
    1736         return bi;
    17371730    }
    17381731
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r13331 r13356  
    1414import java.io.Closeable;
    1515import java.io.File;
     16import java.io.FileNotFoundException;
    1617import java.io.IOException;
    1718import java.io.InputStream;
     
    2627import java.nio.file.Files;
    2728import java.nio.file.Path;
     29import java.nio.file.Paths;
    2830import java.nio.file.StandardCopyOption;
     31import java.nio.file.attribute.BasicFileAttributes;
     32import java.nio.file.attribute.FileTime;
    2933import java.security.AccessController;
    3034import java.security.MessageDigest;
     
    17361740        }
    17371741    }
     1742
     1743    /**
     1744     * Convenient method to open an URL stream, using JOSM HTTP client if neeeded.
     1745     * @param url URL for reading from
     1746     * @return an input stream for reading from the URL
     1747     * @throws IOException if any I/O error occurs
     1748     * @since 13356
     1749     */
     1750    public static InputStream openStream(URL url) throws IOException {
     1751        switch (url.getProtocol()) {
     1752            case "http":
     1753            case "https":
     1754                return HttpClient.create(url).connect().getContent();
     1755            case "jar":
     1756                try {
     1757                    return url.openStream();
     1758                } catch (FileNotFoundException e) {
     1759                    // Workaround to https://bugs.openjdk.java.net/browse/JDK-4523159
     1760                    String urlPath = url.getPath();
     1761                    if (urlPath.startsWith("file:/") && urlPath.split("!").length > 2) {
     1762                        try {
     1763                            // Locate jar file
     1764                            int index = urlPath.lastIndexOf("!/");
     1765                            Path jarFile = Paths.get(urlPath.substring("file:/".length(), index));
     1766                            Path filename = jarFile.getFileName();
     1767                            FileTime jarTime = Files.readAttributes(jarFile, BasicFileAttributes.class).lastModifiedTime();
     1768                            // Copy it to temp directory (hopefully free of exclamation mark) if needed (missing or older jar)
     1769                            Path jarCopy = Paths.get(System.getProperty("java.io.tmpdir")).resolve(filename);
     1770                            if (!jarCopy.toFile().exists() ||
     1771                                    Files.readAttributes(jarCopy, BasicFileAttributes.class).lastModifiedTime().compareTo(jarTime) < 0) {
     1772                                Files.copy(jarFile, jarCopy, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
     1773                            }
     1774                            // Open the stream using the copy
     1775                            return new URL(url.getProtocol() + ':' + jarCopy.toUri().toURL().toExternalForm() + urlPath.substring(index))
     1776                                    .openStream();
     1777                        } catch (RuntimeException | IOException ex) {
     1778                            Logging.warn(ex);
     1779                        }
     1780                    }
     1781                    throw e;
     1782                }
     1783            case "file":
     1784            default:
     1785                return url.openStream();
     1786        }
     1787    }
    17381788}
Note: See TracChangeset for help on using the changeset viewer.