Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixUriUtils.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.nio.file.Path;28import java.io.File;29import java.net.URI;30import java.net.URISyntaxException;31import java.util.Arrays;3233/**34* Unix specific Path <--> URI conversion35*/3637class UnixUriUtils {38private UnixUriUtils() { }3940/**41* Converts URI to Path42*/43static Path fromUri(UnixFileSystem fs, URI uri) {44if (!uri.isAbsolute())45throw new IllegalArgumentException("URI is not absolute");46if (uri.isOpaque())47throw new IllegalArgumentException("URI is not hierarchical");48String scheme = uri.getScheme();49if ((scheme == null) || !scheme.equalsIgnoreCase("file"))50throw new IllegalArgumentException("URI scheme is not \"file\"");51if (uri.getAuthority() != null)52throw new IllegalArgumentException("URI has an authority component");53if (uri.getFragment() != null)54throw new IllegalArgumentException("URI has a fragment component");55if (uri.getQuery() != null)56throw new IllegalArgumentException("URI has a query component");5758// compatibility with java.io.File59if (!uri.toString().startsWith("file:///"))60return new File(uri).toPath();6162// transformation use raw path63String p = uri.getRawPath();64int len = p.length();65if (len == 0)66throw new IllegalArgumentException("URI path component is empty");6768// transform escaped octets and unescaped characters to bytes69if (p.endsWith("/") && len > 1)70len--;71byte[] result = new byte[len];72int rlen = 0;73int pos = 0;74while (pos < len) {75char c = p.charAt(pos++);76byte b;77if (c == '%') {78assert (pos+2) <= len;79char c1 = p.charAt(pos++);80char c2 = p.charAt(pos++);81b = (byte)((decode(c1) << 4) | decode(c2));82if (b == 0)83throw new IllegalArgumentException("Nul character not allowed");84} else {85if (c == 0 || c >= 0x80)86throw new IllegalArgumentException("Bad escape");87b = (byte)c;88}89result[rlen++] = b;90}91if (rlen != result.length)92result = Arrays.copyOf(result, rlen);9394return new UnixPath(fs, result);95}9697/**98* Converts Path to URI99*/100static URI toUri(UnixPath up) {101byte[] path = up.toAbsolutePath().asByteArray();102StringBuilder sb = new StringBuilder("file:///");103assert path[0] == '/';104for (int i=1; i<path.length; i++) {105char c = (char)(path[i] & 0xff);106if (match(c, L_PATH, H_PATH)) {107sb.append(c);108} else {109sb.append('%');110sb.append(hexDigits[(c >> 4) & 0x0f]);111sb.append(hexDigits[(c) & 0x0f]);112}113}114115// trailing slash if directory116if (sb.charAt(sb.length()-1) != '/') {117try {118up.checkRead();119if (UnixFileAttributes.get(up, true).isDirectory())120sb.append('/');121} catch (SecurityException | UnixException x) {122// ignore123}124}125126try {127return new URI(sb.toString());128} catch (URISyntaxException x) {129throw new AssertionError(x); // should not happen130}131}132133// The following is copied from java.net.URI134135// Compute the low-order mask for the characters in the given string136private static long lowMask(String chars) {137int n = chars.length();138long m = 0;139for (int i = 0; i < n; i++) {140char c = chars.charAt(i);141if (c < 64)142m |= (1L << c);143}144return m;145}146147// Compute the high-order mask for the characters in the given string148private static long highMask(String chars) {149int n = chars.length();150long m = 0;151for (int i = 0; i < n; i++) {152char c = chars.charAt(i);153if ((c >= 64) && (c < 128))154m |= (1L << (c - 64));155}156return m;157}158159// Compute a low-order mask for the characters160// between first and last, inclusive161private static long lowMask(char first, char last) {162long m = 0;163int f = Math.max(Math.min(first, 63), 0);164int l = Math.max(Math.min(last, 63), 0);165for (int i = f; i <= l; i++)166m |= 1L << i;167return m;168}169170// Compute a high-order mask for the characters171// between first and last, inclusive172private static long highMask(char first, char last) {173long m = 0;174int f = Math.max(Math.min(first, 127), 64) - 64;175int l = Math.max(Math.min(last, 127), 64) - 64;176for (int i = f; i <= l; i++)177m |= 1L << i;178return m;179}180181// Tell whether the given character is permitted by the given mask pair182private static boolean match(char c, long lowMask, long highMask) {183if (c < 64)184return ((1L << c) & lowMask) != 0;185if (c < 128)186return ((1L << (c - 64)) & highMask) != 0;187return false;188}189190// decode191private static int decode(char c) {192if ((c >= '0') && (c <= '9'))193return c - '0';194if ((c >= 'a') && (c <= 'f'))195return c - 'a' + 10;196if ((c >= 'A') && (c <= 'F'))197return c - 'A' + 10;198throw new AssertionError();199}200201// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |202// "8" | "9"203private static final long L_DIGIT = lowMask('0', '9');204private static final long H_DIGIT = 0L;205206// upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |207// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |208// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"209private static final long L_UPALPHA = 0L;210private static final long H_UPALPHA = highMask('A', 'Z');211212// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |213// "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |214// "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"215private static final long L_LOWALPHA = 0L;216private static final long H_LOWALPHA = highMask('a', 'z');217218// alpha = lowalpha | upalpha219private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;220private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;221222// alphanum = alpha | digit223private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;224private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;225226// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |227// "(" | ")"228private static final long L_MARK = lowMask("-_.!~*'()");229private static final long H_MARK = highMask("-_.!~*'()");230231// unreserved = alphanum | mark232private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;233private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;234235// pchar = unreserved | escaped |236// ":" | "@" | "&" | "=" | "+" | "$" | ","237private static final long L_PCHAR238= L_UNRESERVED | lowMask(":@&=+$,");239private static final long H_PCHAR240= H_UNRESERVED | highMask(":@&=+$,");241242// All valid path characters243private static final long L_PATH = L_PCHAR | lowMask(";/");244private static final long H_PATH = H_PCHAR | highMask(";/");245246private final static char[] hexDigits = {247'0', '1', '2', '3', '4', '5', '6', '7',248'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'249};250}251252253