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