Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Path/MacPath.java
41153 views
1
/*
2
* Copyright (c) 2008, 2017, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.nio.file.DirectoryStream;
25
import java.nio.file.Files;
26
import java.nio.file.Path;
27
import java.nio.file.Paths;
28
import java.nio.file.attribute.PosixFilePermission;
29
import java.text.Normalizer;
30
import java.util.Set;
31
import java.util.regex.Pattern;
32
33
public class MacPath {
34
35
public static void main(String args[]) throws Throwable {
36
System.out.printf("sun.jnu.encoding=%s, file.encoding=%s%n",
37
System.getProperty("file.encoding"),
38
System.getProperty("sun.jnu.encoding"));
39
// English
40
test("TestDir_apple", // test dir
41
"dir_macosx", // dir
42
"file_macosx"); // file
43
44
// Japanese composite character
45
test("TestDir_\u30c8\u30a4\u30e4\u30cb\u30ca\u30eb/",
46
"dir_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad",
47
"file_\u30a4\u30c1\u30b4\u306e\u30b1\u30fc\u30ad");
48
49
// latin-1 supplementory
50
test("TestDir_K\u00f6rperlich\u00e4\u00df/",
51
"dir_Entt\u00e4uschung",
52
"file_Entt\u00e4uschung");
53
54
test("TestDir_K\u00f6rperlich\u00e4\u00df/",
55
"dir_Entt\u00c4uschung",
56
"file_Entt\u00c4uschung");
57
58
// Korean syblla
59
test("TestDir_\uac00\uac01\uac02",
60
"dir_\uac20\uac21\uac22",
61
"file_\uacc0\uacc1\uacc2");
62
}
63
64
private static boolean equal(Object x, Object y) {
65
return x == null ? y == null : x.equals(y);
66
}
67
68
private static boolean match(Path target, Path src) {
69
String fname = target.toString();
70
System.out.printf(" --> Trying [%s], length=%d...", fname, fname.length());
71
if (target.equals(src)) {
72
System.out.println(" MATCHED!");
73
return true;
74
} else {
75
System.out.println(" NOT MATCHED!");
76
}
77
return false;
78
}
79
80
private static void test(String testdir, String dname, String fname_nfc)
81
throws Throwable
82
{
83
String fname = null;
84
String dname_nfd = Normalizer.normalize(dname, Normalizer.Form.NFD);
85
String fname_nfd = Normalizer.normalize(fname_nfc, Normalizer.Form.NFD);
86
87
System.out.printf("%n%n--------Testing...----------%n");
88
Path bpath = Paths.get(testdir);
89
Path dpath = Paths.get(testdir, dname);
90
Path dpath_nfd = Paths.get(testdir, dname_nfd);
91
Path fpath_nfc = Paths.get(testdir, fname_nfc);
92
Path fpath_nfd = Paths.get(testdir, fname_nfd);
93
94
if (Files.exists(bpath))
95
TestUtil.removeAll(bpath);
96
Files.createDirectories(dpath);
97
98
fname = dpath.toString();
99
System.out.printf(":Directory [%s][len=%d] created%n", fname, fname.length());
100
101
//////////////////////////////////////////////////////////////
102
if (!Files.isDirectory(dpath) || !Files.isDirectory(dpath_nfd)) {
103
throw new RuntimeException("Files.isDirectory(...) failed");
104
}
105
106
//////////////////////////////////////////////////////////////
107
// write out with nfd, read in with nfc + case
108
Files.write(fpath_nfd, new byte[] { 'n', 'f', 'd'});
109
System.out.println(" read in with nfc (from nfd):" + new String(Files.readAllBytes(fpath_nfc)));
110
111
// check attrs with nfc + case
112
Set<PosixFilePermission> pfp = Files.getPosixFilePermissions(fpath_nfd);
113
if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfc)) ) {
114
throw new RuntimeException("Files.getPosixfilePermission(...) failed");
115
}
116
Files.delete(fpath_nfd);
117
118
// write out with nfc, read in with nfd + case
119
Files.write(fpath_nfc, new byte[] { 'n', 'f', 'c'});
120
System.out.println(" read in with nfd (from nfc):" + new String(Files.readAllBytes(fpath_nfd)));
121
122
// check attrs with nfc + case
123
pfp = Files.getPosixFilePermissions(fpath_nfc);
124
if (!equal(pfp, Files.getPosixFilePermissions(fpath_nfd))) {
125
throw new RuntimeException("Files.getPosixfilePermission(...) failed");
126
}
127
//////////////////////////////////////////////////////////////
128
boolean found_dir = false;
129
boolean found_file_nfc = false;
130
boolean found_file_nfd = false;
131
try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath)) {
132
for (Path path: stream) {
133
fname = path.toString();
134
System.out.printf("Found : [%s], length=%d%n", fname, fname.length());
135
found_dir |= match(dpath, path);
136
found_file_nfc |= match(fpath_nfc, path);
137
found_file_nfd |= match(fpath_nfd, path);
138
}
139
}
140
if (!found_dir || !found_file_nfc || !found_file_nfd) {
141
throw new RuntimeException("File.equal() failed");
142
}
143
// glob
144
String glob = "*" + fname_nfd.substring(2); // remove leading "FI" from "FILE..."
145
System.out.println("glob=" + glob);
146
boolean globmatched = false;
147
try (DirectoryStream<Path> stream = Files.newDirectoryStream(bpath, glob)) {
148
for (Path path: stream) {
149
fname = path.toString();
150
System.out.printf("PathMatch : [%s], length=%d%n", fname, fname.length());
151
globmatched |= match(fpath_nfc, path);
152
}
153
}
154
if (!globmatched) {
155
//throw new RuntimeException("path matcher failed");
156
// it appears we have a regex.anon_eq bug in hangul syllable handling
157
System.out.printf("pathmatcher failed, glob=[%s]%n", glob);
158
System.out.printf(" -> fname_nfd.matches(fname_nfc)=%b%n",
159
Pattern.compile(fname_nfd, Pattern.CANON_EQ)
160
.matcher(fname_nfc)
161
.matches());
162
System.out.printf(" -> fname_nfc.matches(fname_nfd)=%b%n",
163
Pattern.compile(fname_nfc, Pattern.CANON_EQ)
164
.matcher(fname_nfd)
165
.matches());
166
}
167
TestUtil.removeAll(bpath);
168
}
169
}
170
171