Changeset 14480 in josm
- Timestamp:
- 2018-12-01T21:27:53+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/AboutAction.java
r14153 r14480 163 163 */ 164 164 private void setTextFromResourceFile(JTextArea ta, String filePath) { 165 InputStream is = getClass().getResourceAsStream(filePath);165 InputStream is = Utils.getResourceAsStream(getClass(), filePath); 166 166 if (is == null) { 167 167 displayErrorMessage(ta, tr("Failed to locate resource ''{0}''.", filePath)); -
trunk/src/org/openstreetmap/josm/data/Version.java
r14406 r14480 6 6 import java.io.IOException; 7 7 import java.io.InputStream; 8 import java.net.URL;9 import java.nio.file.InvalidPathException;10 8 import java.util.Map.Entry; 11 9 import java.util.Optional; … … 107 105 */ 108 106 public void init() { 109 try (InputStream stream = openRevisionStream("/REVISION")) {107 try (InputStream stream = Utils.getResourceAsStream(getClass(), "/REVISION")) { 110 108 if (stream == null) { 111 109 Logging.warn(tr("The revision file ''/REVISION'' is missing.")); … … 120 118 } 121 119 122 private static InputStream openRevisionStream(String path) throws IOException {123 try {124 return Version.class.getResourceAsStream(path);125 } catch (InvalidPathException e) {126 Logging.error("Cannot open {0}: {1}", path, e.getMessage());127 URL betterUrl = Utils.betterJarUrl(Version.class.getResource(path));128 if (betterUrl != null) {129 return betterUrl.openStream();130 }131 return null;132 }133 }134 135 120 /** 136 121 * Replies the version string. Either the SVN revision "1234" (as string) or the -
trunk/src/org/openstreetmap/josm/gui/GettingStarted.java
r14273 r14480 176 176 URL u = GettingStarted.class.getResource(im); 177 177 if (u != null) { 178 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + u + '\"')); 178 try { 179 m.appendReplacement(sb, Matcher.quoteReplacement("src=\"" + Utils.betterJarUrl(u, u) + '\"')); 180 } catch (IOException e) { 181 Logging.error(e); 182 } 179 183 } 180 184 } -
trunk/src/org/openstreetmap/josm/gui/MainInitialization.java
r14140 r14480 33 33 import org.openstreetmap.josm.spi.preferences.Config; 34 34 import org.openstreetmap.josm.tools.I18n; 35 import org.openstreetmap.josm.tools.ImageProvider; 35 36 import org.openstreetmap.josm.tools.Logging; 36 37 import org.openstreetmap.josm.tools.OpenBrowser; … … 140 141 // hooks for the jmapviewer component 141 142 FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl); 143 FeatureAdapter.registerImageAdapter(ImageProvider::read); 142 144 FeatureAdapter.registerTranslationAdapter(I18n::tr); 143 145 FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger()); -
trunk/src/org/openstreetmap/josm/io/CachedFile.java
r14406 r14480 227 227 if (name != null && name.startsWith("resource://")) { 228 228 String resourceName = name.substring("resource:/".length()); 229 InputStream is = null; 230 try { 231 is = getClass().getResourceAsStream(resourceName); 232 } catch (InvalidPathException e) { 233 Logging.error("Cannot open {0}: {1}", resourceName, e.getMessage()); 234 Logging.trace(e); 235 } 229 InputStream is = Utils.getResourceAsStream(getClass(), resourceName); 236 230 if (is == null) { 237 URL resource = getClass().getResource(resourceName); 238 if (resource != null) { 239 // More robust way to open stream 240 is = Utils.openStream(resource); 241 } 242 if (is == null) { 243 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name)); 244 } 231 throw new IOException(tr("Failed to open input stream for resource ''{0}''", name)); 245 232 } 246 233 return is; -
trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java
r14384 r14480 422 422 String name = pluginName; 423 423 name = name.replaceAll("[-. ]", ""); 424 try (InputStream manifestStream = PluginInformation.class.getResourceAsStream("/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF")) { 424 try (InputStream manifestStream = Utils.getResourceAsStream( 425 PluginInformation.class, "/org/openstreetmap/josm/plugins/"+name+"/MANIFEST.MF")) { 425 426 if (manifestStream != null) { 426 427 return new PluginInformation(manifestStream, pluginName, null); -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r14404 r14480 1871 1871 */ 1872 1872 public static URL betterJarUrl(URL jarUrl) throws IOException { 1873 return betterJarUrl(jarUrl, null); 1874 } 1875 1876 /** 1877 * Tries to build a better JAR URL if we find it concerned by a JDK bug. 1878 * @param jarUrl jar URL to test 1879 * @param defaultUrl default URL to return 1880 * @return potentially a better URL that won't provoke a JDK bug, or {@code defaultUrl} 1881 * @throws IOException if an I/O error occurs 1882 * @since 14480 1883 */ 1884 public static URL betterJarUrl(URL jarUrl, URL defaultUrl) throws IOException { 1873 1885 // Workaround to https://bugs.openjdk.java.net/browse/JDK-4523159 1874 1886 String urlPath = jarUrl.getPath().replace("%20", " "); … … 1888 1900 return new URL(jarUrl.getProtocol() + ':' + jarCopy.toUri().toURL().toExternalForm() + urlPath.substring(index)); 1889 1901 } 1890 return null; 1902 return defaultUrl; 1903 } 1904 1905 /** 1906 * Finds a resource with a given name, with robustness to known JDK bugs. 1907 * @param klass class on which {@link Class#getResourceAsStream} will be called 1908 * @param path name of the desired resource 1909 * @return A {@link java.io.InputStream} object or {@code null} if no resource with this name is found 1910 * @since 14480 1911 */ 1912 public static InputStream getResourceAsStream(Class<?> klass, String path) { 1913 try { 1914 return klass.getResourceAsStream(path); 1915 } catch (InvalidPathException e) { 1916 Logging.error("Cannot open {0}: {1}", path, e.getMessage()); 1917 try { 1918 URL betterUrl = betterJarUrl(klass.getResource(path)); 1919 if (betterUrl != null) { 1920 return betterUrl.openStream(); 1921 } 1922 } catch (IOException ex) { 1923 Logging.error(ex); 1924 } 1925 return null; 1926 } 1891 1927 } 1892 1928 }
Note:
See TracChangeset
for help on using the changeset viewer.