893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
3497N/Aimport java.nio.file.Path;
3497N/Aimport java.io.File;
893N/Aimport java.net.URI;
893N/Aimport java.net.URISyntaxException;
3497N/Aimport java.util.Arrays;
893N/A
893N/A/**
893N/A * Unix specific Path <--> URI conversion
893N/A */
893N/A
893N/Aclass UnixUriUtils {
893N/A private UnixUriUtils() { }
893N/A
893N/A /**
893N/A * Converts URI to Path
893N/A */
3497N/A static Path fromUri(UnixFileSystem fs, URI uri) {
893N/A if (!uri.isAbsolute())
893N/A throw new IllegalArgumentException("URI is not absolute");
893N/A if (uri.isOpaque())
893N/A throw new IllegalArgumentException("URI is not hierarchical");
893N/A String scheme = uri.getScheme();
893N/A if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
893N/A throw new IllegalArgumentException("URI scheme is not \"file\"");
893N/A if (uri.getAuthority() != null)
893N/A throw new IllegalArgumentException("URI has an authority component");
893N/A if (uri.getFragment() != null)
893N/A throw new IllegalArgumentException("URI has a fragment component");
893N/A if (uri.getQuery() != null)
893N/A throw new IllegalArgumentException("URI has a query component");
893N/A
3497N/A // compatability with java.io.File
3497N/A if (!uri.toString().startsWith("file:///"))
3497N/A return new File(uri).toPath();
3497N/A
3497N/A // transformation use raw path
3497N/A String p = uri.getRawPath();
3497N/A int len = p.length();
3497N/A if (len == 0)
893N/A throw new IllegalArgumentException("URI path component is empty");
893N/A
3497N/A // transform escaped octets and unescaped characters to bytes
3497N/A if (p.endsWith("/") && len > 1)
3497N/A len--;
3497N/A byte[] result = new byte[len];
3497N/A int rlen = 0;
3497N/A int pos = 0;
3497N/A while (pos < len) {
3497N/A char c = p.charAt(pos++);
3497N/A byte b;
3497N/A if (c == '%') {
3497N/A assert (pos+2) <= len;
3497N/A char c1 = p.charAt(pos++);
3497N/A char c2 = p.charAt(pos++);
3497N/A b = (byte)((decode(c1) << 4) | decode(c2));
3497N/A if (b == 0)
3497N/A throw new IllegalArgumentException("Nul character not allowed");
3497N/A } else {
3497N/A assert c < 0x80;
3497N/A b = (byte)c;
3497N/A }
3497N/A result[rlen++] = b;
893N/A }
3497N/A if (rlen != result.length)
3497N/A result = Arrays.copyOf(result, rlen);
3497N/A
893N/A return new UnixPath(fs, result);
893N/A }
893N/A
893N/A /**
893N/A * Converts Path to URI
893N/A */
893N/A static URI toUri(UnixPath up) {
893N/A byte[] path = up.toAbsolutePath().asByteArray();
893N/A StringBuilder sb = new StringBuilder("file:///");
893N/A assert path[0] == '/';
893N/A for (int i=1; i<path.length; i++) {
893N/A char c = (char)(path[i] & 0xff);
893N/A if (match(c, L_PATH, H_PATH)) {
893N/A sb.append(c);
893N/A } else {
893N/A sb.append('%');
893N/A sb.append(hexDigits[(c >> 4) & 0x0f]);
3497N/A sb.append(hexDigits[(c) & 0x0f]);
893N/A }
893N/A }
893N/A
893N/A // trailing slash if directory
893N/A if (sb.charAt(sb.length()-1) != '/') {
893N/A try {
893N/A if (UnixFileAttributes.get(up, true).isDirectory())
893N/A sb.append('/');
893N/A } catch (UnixException x) {
893N/A // ignore
893N/A }
893N/A }
893N/A
893N/A try {
893N/A return new URI(sb.toString());
893N/A } catch (URISyntaxException x) {
893N/A throw new AssertionError(x); // should not happen
893N/A }
893N/A }
893N/A
893N/A // The following is copied from java.net.URI
893N/A
893N/A // Compute the low-order mask for the characters in the given string
893N/A private static long lowMask(String chars) {
893N/A int n = chars.length();
893N/A long m = 0;
893N/A for (int i = 0; i < n; i++) {
893N/A char c = chars.charAt(i);
893N/A if (c < 64)
893N/A m |= (1L << c);
893N/A }
893N/A return m;
893N/A }
893N/A
893N/A // Compute the high-order mask for the characters in the given string
893N/A private static long highMask(String chars) {
893N/A int n = chars.length();
893N/A long m = 0;
893N/A for (int i = 0; i < n; i++) {
893N/A char c = chars.charAt(i);
893N/A if ((c >= 64) && (c < 128))
893N/A m |= (1L << (c - 64));
893N/A }
893N/A return m;
893N/A }
893N/A
893N/A // Compute a low-order mask for the characters
893N/A // between first and last, inclusive
893N/A private static long lowMask(char first, char last) {
893N/A long m = 0;
893N/A int f = Math.max(Math.min(first, 63), 0);
893N/A int l = Math.max(Math.min(last, 63), 0);
893N/A for (int i = f; i <= l; i++)
893N/A m |= 1L << i;
893N/A return m;
893N/A }
893N/A
893N/A // Compute a high-order mask for the characters
893N/A // between first and last, inclusive
893N/A private static long highMask(char first, char last) {
893N/A long m = 0;
893N/A int f = Math.max(Math.min(first, 127), 64) - 64;
893N/A int l = Math.max(Math.min(last, 127), 64) - 64;
893N/A for (int i = f; i <= l; i++)
893N/A m |= 1L << i;
893N/A return m;
893N/A }
893N/A
893N/A // Tell whether the given character is permitted by the given mask pair
893N/A private static boolean match(char c, long lowMask, long highMask) {
893N/A if (c < 64)
893N/A return ((1L << c) & lowMask) != 0;
893N/A if (c < 128)
893N/A return ((1L << (c - 64)) & highMask) != 0;
893N/A return false;
893N/A }
893N/A
3497N/A // decode
3497N/A private static int decode(char c) {
3497N/A if ((c >= '0') && (c <= '9'))
3497N/A return c - '0';
3497N/A if ((c >= 'a') && (c <= 'f'))
3497N/A return c - 'a' + 10;
3497N/A if ((c >= 'A') && (c <= 'F'))
3497N/A return c - 'A' + 10;
3497N/A throw new AssertionError();
3497N/A }
3497N/A
893N/A // digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
893N/A // "8" | "9"
893N/A private static final long L_DIGIT = lowMask('0', '9');
893N/A private static final long H_DIGIT = 0L;
893N/A
893N/A // upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
893N/A // "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
893N/A // "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
893N/A private static final long L_UPALPHA = 0L;
893N/A private static final long H_UPALPHA = highMask('A', 'Z');
893N/A
893N/A // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
893N/A // "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
893N/A // "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
893N/A private static final long L_LOWALPHA = 0L;
893N/A private static final long H_LOWALPHA = highMask('a', 'z');
893N/A
893N/A // alpha = lowalpha | upalpha
893N/A private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
893N/A private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
893N/A
893N/A // alphanum = alpha | digit
893N/A private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
893N/A private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
893N/A
893N/A // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
893N/A // "(" | ")"
893N/A private static final long L_MARK = lowMask("-_.!~*'()");
893N/A private static final long H_MARK = highMask("-_.!~*'()");
893N/A
893N/A // unreserved = alphanum | mark
893N/A private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
893N/A private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
893N/A
893N/A // pchar = unreserved | escaped |
893N/A // ":" | "@" | "&" | "=" | "+" | "$" | ","
893N/A private static final long L_PCHAR
893N/A = L_UNRESERVED | lowMask(":@&=+$,");
893N/A private static final long H_PCHAR
893N/A = H_UNRESERVED | highMask(":@&=+$,");
893N/A
893N/A // All valid path characters
893N/A private static final long L_PATH = L_PCHAR | lowMask(";/");
893N/A private static final long H_PATH = H_PCHAR | highMask(";/");
893N/A
893N/A private final static char[] hexDigits = {
893N/A '0', '1', '2', '3', '4', '5', '6', '7',
893N/A '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
893N/A };
893N/A}