diff options
Diffstat (limited to 'bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java')
-rw-r--r-- | bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java | 81 |
1 files changed, 76 insertions, 5 deletions
diff --git a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java index 75da9a911..73050d44f 100644 --- a/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java +++ b/bundles/org.eclipse.osgi/supplement/src/org/eclipse/osgi/internal/location/LocationHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2013 IBM Corporation and others. + * Copyright (c) 2006, 2016 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -10,9 +10,8 @@ *******************************************************************************/ package org.eclipse.osgi.internal.location; -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; +import java.io.*; +import java.net.*; import org.eclipse.osgi.internal.location.Locker.MockLocker; /** @@ -55,7 +54,7 @@ public class LocationHelper { } private static URL adjustTrailingSlash(URL url, boolean trailingSlash) throws MalformedURLException { - String file = url.getFile(); + String file = url.getPath(); if (trailingSlash == (file.endsWith("/"))) //$NON-NLS-1$ return url; file = trailingSlash ? file + "/" : file.substring(0, file.length() - 1); //$NON-NLS-1$ @@ -80,4 +79,76 @@ public class LocationHelper { // Backup case if an invalid value has been specified return new Locker_JavaNio(lock, debug); } + + public static InputStream getStream(URL location) throws IOException { + if ("file".equalsIgnoreCase(location.getProtocol())) { //$NON-NLS-1$ + // this is done to handle URLs with invalid syntax in the path + File f = new File(location.getPath()); + if (f.exists()) { + return new FileInputStream(f); + } + } + return location.openStream(); + } + + public static URLConnection getConnection(URL url) throws IOException { + if ("file".equalsIgnoreCase(url.getProtocol())) { //$NON-NLS-1$ + try { + return url.openConnection(); + } catch (IllegalArgumentException e) { + // this is done to handle URLs with invalid syntax in the path for URIs + File f = new File(url.getPath()); + if (f.exists()) { + return f.toURI().toURL().openConnection(); + } + } + } + return url.openConnection(); + } + + public static File decodePath(File file) { + // Pre-check if file exists, if not, and it contains escape characters, + // try decoding the absolute path generated by makeAbsolute + if (!file.exists() && (file.getPath().indexOf('%') >= 0 || file.getPath().indexOf('+') >= 0)) { + String absolute = file.getAbsolutePath(); + String decodePath = LocationHelper.decode(absolute, true); + File f = new File(decodePath); + if (f.exists()) { + return f; + } + decodePath = LocationHelper.decode(absolute, false); + f = new File(decodePath); + if (f.exists()) { + return f; + } + } + return file; + } + + public static String decode(String urlString, boolean plusEncoded) { + //first encode '+' characters, because URLDecoder incorrectly converts + //them to spaces on certain class library implementations. + if (plusEncoded && urlString.indexOf('+') >= 0) { + int len = urlString.length(); + StringBuffer buf = new StringBuffer(len); + for (int i = 0; i < len; i++) { + char c = urlString.charAt(i); + if (c == '+') + buf.append("%2B"); //$NON-NLS-1$ + else + buf.append(c); + } + urlString = buf.toString(); + } + try { + return URLDecoder.decode(urlString, "UTF-8"); //$NON-NLS-1$ + } catch (UnsupportedEncodingException e) { + // Tried but failed + // TODO should we throw runtime exception here? + return urlString; + } catch (RuntimeException e) { + // May have illegal characters for decoding + return urlString; + } + } } |