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