1 /* 2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.fs; 27 28 import java.nio.file.Path; 29 import java.io.File; 30 import java.net.URI; 31 import java.net.URISyntaxException; 32 import java.util.Arrays; 33 34 /** 35 * Unix specific Path <--> URI conversion 36 */ 37 38 class UnixUriUtils { UnixUriUtils()39 private UnixUriUtils() { } 40 41 /** 42 * Converts URI to Path 43 */ fromUri(UnixFileSystem fs, URI uri)44 static Path fromUri(UnixFileSystem fs, URI uri) { 45 if (!uri.isAbsolute()) 46 throw new IllegalArgumentException("URI is not absolute"); 47 if (uri.isOpaque()) 48 throw new IllegalArgumentException("URI is not hierarchical"); 49 String scheme = uri.getScheme(); 50 if ((scheme == null) || !scheme.equalsIgnoreCase("file")) 51 throw new IllegalArgumentException("URI scheme is not \"file\""); 52 if (uri.getAuthority() != null) 53 throw new IllegalArgumentException("URI has an authority component"); 54 if (uri.getFragment() != null) 55 throw new IllegalArgumentException("URI has a fragment component"); 56 if (uri.getQuery() != null) 57 throw new IllegalArgumentException("URI has a query component"); 58 59 // compatibility with java.io.File 60 if (!uri.toString().startsWith("file:///")) 61 return new File(uri).toPath(); 62 63 // transformation use raw path 64 String p = uri.getRawPath(); 65 int len = p.length(); 66 if (len == 0) 67 throw new IllegalArgumentException("URI path component is empty"); 68 69 // transform escaped octets and unescaped characters to bytes 70 if (p.endsWith("/") && len > 1) 71 len--; 72 byte[] result = new byte[len]; 73 int rlen = 0; 74 int pos = 0; 75 while (pos < len) { 76 char c = p.charAt(pos++); 77 byte b; 78 if (c == '%') { 79 assert (pos+2) <= len; 80 char c1 = p.charAt(pos++); 81 char c2 = p.charAt(pos++); 82 b = (byte)((decode(c1) << 4) | decode(c2)); 83 if (b == 0) 84 throw new IllegalArgumentException("Nul character not allowed"); 85 } else { 86 assert c < 0x80; 87 b = (byte)c; 88 } 89 result[rlen++] = b; 90 } 91 if (rlen != result.length) 92 result = Arrays.copyOf(result, rlen); 93 94 return new UnixPath(fs, result); 95 } 96 97 /** 98 * Converts Path to URI 99 */ 100 static URI toUri(UnixPath up) { 101 byte[] path = up.toAbsolutePath().asByteArray(); 102 StringBuilder sb = new StringBuilder("file:///"); 103 assert path[0] == '/'; 104 for (int i=1; i<path.length; i++) { 105 char c = (char)(path[i] & 0xff); 106 if (match(c, L_PATH, H_PATH)) { 107 sb.append(c); 108 } else { 109 sb.append('%'); 110 sb.append(hexDigits[(c >> 4) & 0x0f]); 111 sb.append(hexDigits[(c) & 0x0f]); 112 } 113 } 114 115 // trailing slash if directory 116 if (sb.charAt(sb.length()-1) != '/') { 117 try { 118 if (UnixFileAttributes.get(up, true).isDirectory()) 119 sb.append('/'); 120 } catch (UnixException x) { 121 // ignore 122 } 123 } 124 125 try { 126 return new URI(sb.toString()); 127 } catch (URISyntaxException x) { 128 throw new AssertionError(x); // should not happen 129 } 130 } 131 132 // The following is copied from java.net.URI 133 134 // Compute the low-order mask for the characters in the given string lowMask(String chars)135 private static long lowMask(String chars) { 136 int n = chars.length(); 137 long m = 0; 138 for (int i = 0; i < n; i++) { 139 char c = chars.charAt(i); 140 if (c < 64) 141 m |= (1L << c); 142 } 143 return m; 144 } 145 146 // Compute the high-order mask for the characters in the given string highMask(String chars)147 private static long highMask(String chars) { 148 int n = chars.length(); 149 long m = 0; 150 for (int i = 0; i < n; i++) { 151 char c = chars.charAt(i); 152 if ((c >= 64) && (c < 128)) 153 m |= (1L << (c - 64)); 154 } 155 return m; 156 } 157 158 // Compute a low-order mask for the characters 159 // between first and last, inclusive lowMask(char first, char last)160 private static long lowMask(char first, char last) { 161 long m = 0; 162 int f = Math.max(Math.min(first, 63), 0); 163 int l = Math.max(Math.min(last, 63), 0); 164 for (int i = f; i <= l; i++) 165 m |= 1L << i; 166 return m; 167 } 168 169 // Compute a high-order mask for the characters 170 // between first and last, inclusive highMask(char first, char last)171 private static long highMask(char first, char last) { 172 long m = 0; 173 int f = Math.max(Math.min(first, 127), 64) - 64; 174 int l = Math.max(Math.min(last, 127), 64) - 64; 175 for (int i = f; i <= l; i++) 176 m |= 1L << i; 177 return m; 178 } 179 180 // Tell whether the given character is permitted by the given mask pair match(char c, long lowMask, long highMask)181 private static boolean match(char c, long lowMask, long highMask) { 182 if (c < 64) 183 return ((1L << c) & lowMask) != 0; 184 if (c < 128) 185 return ((1L << (c - 64)) & highMask) != 0; 186 return false; 187 } 188 189 // decode decode(char c)190 private static int decode(char c) { 191 if ((c >= '0') && (c <= '9')) 192 return c - '0'; 193 if ((c >= 'a') && (c <= 'f')) 194 return c - 'a' + 10; 195 if ((c >= 'A') && (c <= 'F')) 196 return c - 'A' + 10; 197 throw new AssertionError(); 198 } 199 200 // digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | 201 // "8" | "9" 202 private static final long L_DIGIT = lowMask('0', '9'); 203 private static final long H_DIGIT = 0L; 204 205 // upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | 206 // "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | 207 // "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" 208 private static final long L_UPALPHA = 0L; 209 private static final long H_UPALPHA = highMask('A', 'Z'); 210 211 // lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | 212 // "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | 213 // "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" 214 private static final long L_LOWALPHA = 0L; 215 private static final long H_LOWALPHA = highMask('a', 'z'); 216 217 // alpha = lowalpha | upalpha 218 private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA; 219 private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA; 220 221 // alphanum = alpha | digit 222 private static final long L_ALPHANUM = L_DIGIT | L_ALPHA; 223 private static final long H_ALPHANUM = H_DIGIT | H_ALPHA; 224 225 // mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | 226 // "(" | ")" 227 private static final long L_MARK = lowMask("-_.!~*'()"); 228 private static final long H_MARK = highMask("-_.!~*'()"); 229 230 // unreserved = alphanum | mark 231 private static final long L_UNRESERVED = L_ALPHANUM | L_MARK; 232 private static final long H_UNRESERVED = H_ALPHANUM | H_MARK; 233 234 // pchar = unreserved | escaped | 235 // ":" | "@" | "&" | "=" | "+" | "$" | "," 236 private static final long L_PCHAR 237 = L_UNRESERVED | lowMask(":@&=+$,"); 238 private static final long H_PCHAR 239 = H_UNRESERVED | highMask(":@&=+$,"); 240 241 // All valid path characters 242 private static final long L_PATH = L_PCHAR | lowMask(";/"); 243 private static final long H_PATH = H_PCHAR | highMask(";/"); 244 245 private final static char[] hexDigits = { 246 '0', '1', '2', '3', '4', '5', '6', '7', 247 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 248 }; 249 } 250