2
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28
import java.nio.file.Path;
31
import java.net.URISyntaxException;
32
import java.nio.file.Path;
33
import java.util.Arrays;
36
* Unix specific Path <--> URI conversion
40
private UnixUriUtils() { }
43
* Converts URI to Path
45
static Path fromUri(NetFileSystem fs, URI uri) {
46
if (!uri.isAbsolute())
47
throw new IllegalArgumentException("URI is not absolute");
49
throw new IllegalArgumentException("URI is not hierarchical");
50
String scheme = uri.getScheme();
51
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
52
throw new IllegalArgumentException("URI scheme is not \"file\"");
53
if (uri.getAuthority() != null)
54
throw new IllegalArgumentException("URI has an authority component");
55
if (uri.getFragment() != null)
56
throw new IllegalArgumentException("URI has a fragment component");
57
if (uri.getQuery() != null)
58
throw new IllegalArgumentException("URI has a query component");
60
// compatability with java.io.File
61
if (!uri.toString().startsWith("file:///"))
62
return new File(uri).toPath();
64
// transformation use raw path
65
String p = uri.getRawPath();
68
throw new IllegalArgumentException("URI path component is empty");
70
// transform escaped octets and unescaped characters to bytes
71
if (p.endsWith("/") && len > 1)
72
p = p.substring(0, len - 1);
74
return new NetPath(fs, p);
78
* Converts Path to URI
80
static URI toUri(NetPath up) {
81
byte[] path = up.toAbsolutePath().toString().getBytes();
82
StringBuilder sb = new StringBuilder("file:///");
83
assert path[0] == '/';
84
for (int i=1; i<path.length; i++) {
85
char c = (char)(path[i] & 0xff);
86
if (match(c, L_PATH, H_PATH)) {
90
sb.append(hexDigits[(c >> 4) & 0x0f]);
91
sb.append(hexDigits[(c) & 0x0f]);
95
// trailing slash if directory
96
if (sb.charAt(sb.length()-1) != '/') {
98
if (cli.System.IO.Directory.Exists(up.path))
100
} catch (Throwable x) {
106
return new URI(sb.toString());
107
} catch (URISyntaxException x) {
108
throw new AssertionError(x); // should not happen
112
// The following is copied from java.net.URI
114
// Compute the low-order mask for the characters in the given string
115
private static long lowMask(String chars) {
116
int n = chars.length();
118
for (int i = 0; i < n; i++) {
119
char c = chars.charAt(i);
126
// Compute the high-order mask for the characters in the given string
127
private static long highMask(String chars) {
128
int n = chars.length();
130
for (int i = 0; i < n; i++) {
131
char c = chars.charAt(i);
132
if ((c >= 64) && (c < 128))
133
m |= (1L << (c - 64));
138
// Compute a low-order mask for the characters
139
// between first and last, inclusive
140
private static long lowMask(char first, char last) {
142
int f = Math.max(Math.min(first, 63), 0);
143
int l = Math.max(Math.min(last, 63), 0);
144
for (int i = f; i <= l; i++)
149
// Compute a high-order mask for the characters
150
// between first and last, inclusive
151
private static long highMask(char first, char last) {
153
int f = Math.max(Math.min(first, 127), 64) - 64;
154
int l = Math.max(Math.min(last, 127), 64) - 64;
155
for (int i = f; i <= l; i++)
160
// Tell whether the given character is permitted by the given mask pair
161
private static boolean match(char c, long lowMask, long highMask) {
163
return ((1L << c) & lowMask) != 0;
165
return ((1L << (c - 64)) & highMask) != 0;
170
private static int decode(char c) {
171
if ((c >= '0') && (c <= '9'))
173
if ((c >= 'a') && (c <= 'f'))
175
if ((c >= 'A') && (c <= 'F'))
177
throw new AssertionError();
180
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
182
private static final long L_DIGIT = lowMask('0', '9');
183
private static final long H_DIGIT = 0L;
185
// upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
186
// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
187
// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
188
private static final long L_UPALPHA = 0L;
189
private static final long H_UPALPHA = highMask('A', 'Z');
191
// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
192
// "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
193
// "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
194
private static final long L_LOWALPHA = 0L;
195
private static final long H_LOWALPHA = highMask('a', 'z');
197
// alpha = lowalpha | upalpha
198
private static final long L_ALPHA = L_LOWALPHA | L_UPALPHA;
199
private static final long H_ALPHA = H_LOWALPHA | H_UPALPHA;
201
// alphanum = alpha | digit
202
private static final long L_ALPHANUM = L_DIGIT | L_ALPHA;
203
private static final long H_ALPHANUM = H_DIGIT | H_ALPHA;
205
// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
207
private static final long L_MARK = lowMask("-_.!~*'()");
208
private static final long H_MARK = highMask("-_.!~*'()");
210
// unreserved = alphanum | mark
211
private static final long L_UNRESERVED = L_ALPHANUM | L_MARK;
212
private static final long H_UNRESERVED = H_ALPHANUM | H_MARK;
214
// pchar = unreserved | escaped |
215
// ":" | "@" | "&" | "=" | "+" | "$" | ","
216
private static final long L_PCHAR
217
= L_UNRESERVED | lowMask(":@&=+$,");
218
private static final long H_PCHAR
219
= H_UNRESERVED | highMask(":@&=+$,");
221
// All valid path characters
222
private static final long L_PATH = L_PCHAR | lowMask(";/");
223
private static final long H_PATH = H_PCHAR | highMask(";/");
225
private final static char[] hexDigits = {
226
'0', '1', '2', '3', '4', '5', '6', '7',
227
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'