Changeset 13356 in josm for trunk/src/org
- Timestamp:
- 2018-01-26T21:37:00+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r13294 r13356 156 156 import org.openstreetmap.josm.tools.FontsManager; 157 157 import org.openstreetmap.josm.tools.GBC; 158 import org.openstreetmap.josm.tools.HttpClient;159 158 import org.openstreetmap.josm.tools.I18n; 160 159 import org.openstreetmap.josm.tools.ImageProvider; … … 955 954 for (String i : args.get(Option.LOAD_PREFERENCES)) { 956 955 Logging.info("Reading preferences from " + i); 957 try (InputStream is = openStream(new URL(i))) {956 try (InputStream is = Utils.openStream(new URL(i))) { 958 957 config.openAndReadXML(is); 959 958 } catch (IOException ex) { … … 1206 1205 } 1207 1206 } 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();1216 1207 } 1217 1208 } -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r13352 r13356 164 164 } 165 165 166 if ("file".equals(url.getProtocol())) { 166 String protocol = url.getProtocol(); 167 if ("file".equals(protocol) || "jar".equals(protocol)) { 167 168 try { 168 return url.openStream();169 return Utils.openStream(url); 169 170 } catch (IOException e) { 170 171 throw new OsmTransferException(e); -
trunk/src/org/openstreetmap/josm/tools/I18n.java
r13204 r13356 419 419 } 420 420 try ( 421 InputStream enStream = en.openStream();422 InputStream trStream = tr.openStream()421 InputStream enStream = Utils.openStream(en); 422 InputStream trStream = Utils.openStream(tr) 423 423 ) { 424 424 if (load(enStream, trStream, false)) { -
trunk/src/org/openstreetmap/josm/tools/ImageProvider.java
r13311 r13356 1718 1718 CheckParameterUtil.ensureParameterNotNull(input, "input"); 1719 1719 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; 1723 1727 } catch (IOException e) { 1724 1728 throw new IIOException("Can't get input stream from URL!", e); 1725 1729 } 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;1737 1730 } 1738 1731 -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r13331 r13356 14 14 import java.io.Closeable; 15 15 import java.io.File; 16 import java.io.FileNotFoundException; 16 17 import java.io.IOException; 17 18 import java.io.InputStream; … … 26 27 import java.nio.file.Files; 27 28 import java.nio.file.Path; 29 import java.nio.file.Paths; 28 30 import java.nio.file.StandardCopyOption; 31 import java.nio.file.attribute.BasicFileAttributes; 32 import java.nio.file.attribute.FileTime; 29 33 import java.security.AccessController; 30 34 import java.security.MessageDigest; … … 1736 1740 } 1737 1741 } 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 } 1738 1788 }
Note:
See TracChangeset
for help on using the changeset viewer.