Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsUriSupport.java
32288 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.net.URI;28import java.net.URISyntaxException;2930/**31* Utility methods to convert between Path and URIs.32*/3334class WindowsUriSupport {35private WindowsUriSupport() {36}3738// suffix for IPv6 literal address39private static final String IPV6_LITERAL_SUFFIX = ".ipv6-literal.net";4041/**42* Returns URI to represent the given (absolute) path43*/44private static URI toUri(String path, boolean isUnc, boolean addSlash) {45String uriHost;46String uriPath;4748if (isUnc) {49int slash = path.indexOf('\\', 2);50uriHost = path.substring(2, slash);51uriPath = path.substring(slash).replace('\\', '/');5253// handle IPv6 literal addresses54// 1. drop .ivp6-literal.net55// 2. replace "-" with ":"56// 3. replace "s" with "%" (zone/scopeID delimiter)57if (uriHost.endsWith(IPV6_LITERAL_SUFFIX)) {58uriHost = uriHost59.substring(0, uriHost.length() - IPV6_LITERAL_SUFFIX.length())60.replace('-', ':')61.replace('s', '%');62}63} else {64uriHost = "";65uriPath = "/" + path.replace('\\', '/');66}6768// append slash if known to be directory69if (addSlash)70uriPath += "/";7172// return file:///C:/My%20Documents or file://server/share/foo73try {74return new URI("file", uriHost, uriPath, null);75} catch (URISyntaxException x) {76if (!isUnc)77throw new AssertionError(x);78}7980// if we get here it means we've got a UNC with reserved characters81// in the server name. The authority component cannot contain escaped82// octets so fallback to encoding the server name into the URI path83// component.84uriPath = "//" + path.replace('\\', '/');85if (addSlash)86uriPath += "/";87try {88return new URI("file", null, uriPath, null);89} catch (URISyntaxException x) {90throw new AssertionError(x);91}92}9394/**95* Converts given Path to a URI96*/97static URI toUri(WindowsPath path) {98path = path.toAbsolutePath();99String s = path.toString();100101// trailing slash will be added if file is a directory. Skip check if102// already have trailing space103boolean addSlash = false;104if (!s.endsWith("\\")) {105try {106path.checkRead();107addSlash = WindowsFileAttributes.get(path, true).isDirectory();108} catch (SecurityException | WindowsException x) {109}110}111112return toUri(s, path.isUnc(), addSlash);113}114115/**116* Converts given URI to a Path117*/118static WindowsPath fromUri(WindowsFileSystem fs, URI uri) {119if (!uri.isAbsolute())120throw new IllegalArgumentException("URI is not absolute");121if (uri.isOpaque())122throw new IllegalArgumentException("URI is not hierarchical");123String scheme = uri.getScheme();124if ((scheme == null) || !scheme.equalsIgnoreCase("file"))125throw new IllegalArgumentException("URI scheme is not \"file\"");126if (uri.getFragment() != null)127throw new IllegalArgumentException("URI has a fragment component");128if (uri.getQuery() != null)129throw new IllegalArgumentException("URI has a query component");130String path = uri.getPath();131if (path.equals(""))132throw new IllegalArgumentException("URI path component is empty");133134// UNC135String auth = uri.getAuthority();136if (auth != null && !auth.equals("")) {137String host = uri.getHost();138if (host == null)139throw new IllegalArgumentException("URI authority component has undefined host");140if (uri.getUserInfo() != null)141throw new IllegalArgumentException("URI authority component has user-info");142if (uri.getPort() != -1)143throw new IllegalArgumentException("URI authority component has port number");144145// IPv6 literal146// 1. drop enclosing brackets147// 2. replace ":" with "-"148// 3. replace "%" with "s" (zone/scopeID delimiter)149// 4. Append .ivp6-literal.net150if (host.startsWith("[")) {151host = host.substring(1, host.length()-1)152.replace(':', '-')153.replace('%', 's');154host += IPV6_LITERAL_SUFFIX;155}156157// reconstitute the UNC158path = "\\\\" + host + path;159} else {160if ((path.length() > 2) && (path.charAt(2) == ':')) {161// "/c:/foo" --> "c:/foo"162path = path.substring(1);163}164}165return WindowsPath.parse(fs, path);166}167}168169170