UnixUriUtils.java revision 2362
893N/A/*
2362N/A * Copyright (c) 2008, 2009, 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
893N/Aimport java.net.URI;
893N/Aimport java.net.URISyntaxException;
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 */
893N/A static UnixPath 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
893N/A String path = uri.getPath();
893N/A if (path.equals(""))
893N/A throw new IllegalArgumentException("URI path component is empty");
893N/A if (path.endsWith("/") && (path.length() > 1)) {
893N/A // "/foo/" --> "/foo", but "/" --> "/"
893N/A path = path.substring(0, path.length() - 1);
893N/A }
893N/A
893N/A // preserve bytes
893N/A byte[] result = new byte[path.length()];
893N/A for (int i=0; i<path.length(); i++) {
893N/A byte v = (byte)(path.charAt(i));
893N/A if (v == 0)
893N/A throw new IllegalArgumentException("Nul character not allowed");
893N/A result[i] = v;
893N/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]);
893N/A sb.append(hexDigits[(c >> 0) & 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
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}