jdk/src/solaris/classes/sun/nio/fs/UnixUriUtils.java
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.net.URI;
       
    29 import java.net.URISyntaxException;
       
    30 
       
    31 /**
       
    32  * Unix specific Path <--> URI conversion
       
    33  */
       
    34 
       
    35 class UnixUriUtils {
       
    36     private UnixUriUtils() { }
       
    37 
       
    38     /**
       
    39      * Converts URI to Path
       
    40      */
       
    41     static UnixPath fromUri(UnixFileSystem fs, URI uri) {
       
    42         if (!uri.isAbsolute())
       
    43             throw new IllegalArgumentException("URI is not absolute");
       
    44         if (uri.isOpaque())
       
    45             throw new IllegalArgumentException("URI is not hierarchical");
       
    46         String scheme = uri.getScheme();
       
    47         if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
       
    48             throw new IllegalArgumentException("URI scheme is not \"file\"");
       
    49         if (uri.getAuthority() != null)
       
    50             throw new IllegalArgumentException("URI has an authority component");
       
    51         if (uri.getFragment() != null)
       
    52             throw new IllegalArgumentException("URI has a fragment component");
       
    53         if (uri.getQuery() != null)
       
    54             throw new IllegalArgumentException("URI has a query component");
       
    55 
       
    56         String path = uri.getPath();
       
    57         if (path.equals(""))
       
    58             throw new IllegalArgumentException("URI path component is empty");
       
    59         if (path.endsWith("/") && (path.length() > 1)) {
       
    60             // "/foo/" --> "/foo", but "/" --> "/"
       
    61             path = path.substring(0, path.length() - 1);
       
    62         }
       
    63 
       
    64         // preserve bytes
       
    65         byte[] result = new byte[path.length()];
       
    66         for (int i=0; i<path.length(); i++) {
       
    67             byte v = (byte)(path.charAt(i));
       
    68             if (v == 0)
       
    69                 throw new IllegalArgumentException("Nul character not allowed");
       
    70             result[i] = v;
       
    71         }
       
    72         return new UnixPath(fs, result);
       
    73     }
       
    74 
       
    75     /**
       
    76      * Converts Path to URI
       
    77      */
       
    78     static URI toUri(UnixPath up) {
       
    79         byte[] path = up.toAbsolutePath().asByteArray();
       
    80         StringBuilder sb = new StringBuilder("file:///");
       
    81         assert path[0] == '/';
       
    82         for (int i=1; i<path.length; i++) {
       
    83             char c = (char)(path[i] & 0xff);
       
    84             if (match(c, L_PATH, H_PATH)) {
       
    85                 sb.append(c);
       
    86             } else {
       
    87                sb.append('%');
       
    88                sb.append(hexDigits[(c >> 4) & 0x0f]);
       
    89                sb.append(hexDigits[(c >> 0) & 0x0f]);
       
    90             }
       
    91         }
       
    92 
       
    93         // trailing slash if directory
       
    94         if (sb.charAt(sb.length()-1) != '/') {
       
    95             try {
       
    96                  if (UnixFileAttributes.get(up, true).isDirectory())
       
    97                      sb.append('/');
       
    98             } catch (UnixException x) {
       
    99                 // ignore
       
   100             }
       
   101         }
       
   102 
       
   103         try {
       
   104             return new URI(sb.toString());
       
   105         } catch (URISyntaxException x) {
       
   106             throw new AssertionError(x);  // should not happen
       
   107         }
       
   108     }
       
   109 
       
   110     // The following is copied from java.net.URI
       
   111 
       
   112     // Compute the low-order mask for the characters in the given string
       
   113     private static long lowMask(String chars) {
       
   114         int n = chars.length();
       
   115         long m = 0;
       
   116         for (int i = 0; i < n; i++) {
       
   117             char c = chars.charAt(i);
       
   118             if (c < 64)
       
   119                 m |= (1L << c);
       
   120         }
       
   121         return m;
       
   122     }
       
   123 
       
   124     // Compute the high-order mask for the characters in the given string
       
   125     private static long highMask(String chars) {
       
   126         int n = chars.length();
       
   127         long m = 0;
       
   128         for (int i = 0; i < n; i++) {
       
   129             char c = chars.charAt(i);
       
   130             if ((c >= 64) && (c < 128))
       
   131                 m |= (1L << (c - 64));
       
   132         }
       
   133         return m;
       
   134     }
       
   135 
       
   136     // Compute a low-order mask for the characters
       
   137     // between first and last, inclusive
       
   138     private static long lowMask(char first, char last) {
       
   139         long m = 0;
       
   140         int f = Math.max(Math.min(first, 63), 0);
       
   141         int l = Math.max(Math.min(last, 63), 0);
       
   142         for (int i = f; i <= l; i++)
       
   143             m |= 1L << i;
       
   144         return m;
       
   145     }
       
   146 
       
   147     // Compute a high-order mask for the characters
       
   148     // between first and last, inclusive
       
   149     private static long highMask(char first, char last) {
       
   150         long m = 0;
       
   151         int f = Math.max(Math.min(first, 127), 64) - 64;
       
   152         int l = Math.max(Math.min(last, 127), 64) - 64;
       
   153         for (int i = f; i <= l; i++)
       
   154             m |= 1L << i;
       
   155         return m;
       
   156     }
       
   157 
       
   158     // Tell whether the given character is permitted by the given mask pair
       
   159     private static boolean match(char c, long lowMask, long highMask) {
       
   160         if (c < 64)
       
   161             return ((1L << c) & lowMask) != 0;
       
   162         if (c < 128)
       
   163             return ((1L << (c - 64)) & highMask) != 0;
       
   164         return false;
       
   165     }
       
   166 
       
   167     // digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
       
   168     //            "8" | "9"
       
   169     private static final long L_DIGIT = lowMask('0', '9');
       
   170     private static final long H_DIGIT = 0L;
       
   171 
       
   172     // upalpha  = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
       
   173     //            "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
       
   174     //            "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
       
   175     private static final long L_UPALPHA = 0L;
       
   176     private static final long H_UPALPHA = highMask('A', 'Z');
       
   177 
       
   178     // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
       
   179     //            "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
       
   180     //            "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
       
   181     private static final long L_LOWALPHA = 0L;
       
   182     private static final long H_LOWALPHA = highMask('a', 'z');
       
   183 
       
   184     // alpha         = lowalpha | upalpha
       
   185     private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
       
   186     private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
       
   187 
       
   188     // alphanum      = alpha | digit
       
   189     private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
       
   190     private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
       
   191 
       
   192     // mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
       
   193     //                 "(" | ")"
       
   194     private static final long L_MARK = lowMask("-_.!~*'()");
       
   195     private static final long H_MARK = highMask("-_.!~*'()");
       
   196 
       
   197     // unreserved    = alphanum | mark
       
   198     private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
       
   199     private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
       
   200 
       
   201     // pchar         = unreserved | escaped |
       
   202     //                 ":" | "@" | "&" | "=" | "+" | "$" | ","
       
   203     private static final long L_PCHAR
       
   204         = L_UNRESERVED | lowMask(":@&=+$,");
       
   205     private static final long H_PCHAR
       
   206         = H_UNRESERVED | highMask(":@&=+$,");
       
   207 
       
   208    // All valid path characters
       
   209    private static final long L_PATH = L_PCHAR | lowMask(";/");
       
   210    private static final long H_PATH = H_PCHAR | highMask(";/");
       
   211 
       
   212    private final static char[] hexDigits = {
       
   213         '0', '1', '2', '3', '4', '5', '6', '7',
       
   214         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
       
   215     };
       
   216 }