Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/NameLimits.java
41153 views
1
/*
2
* Copyright (c) 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.
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
/* @test
25
* @bug 8011128
26
* @summary Test file and directory name limits. This test is primarily
27
* intended to test Files.createDirectory on resolved paths at or around
28
* the short path limit of 248 on Windows.
29
*/
30
31
import java.io.IOException;
32
import java.nio.file.Files;
33
import java.nio.file.Path;
34
import java.nio.file.Paths;
35
36
public class NameLimits {
37
38
static final int MAX_PATH = 255;
39
static final int MIN_PATH = 8; // arbitrarily chosen
40
41
static Path generatePath(int len) {
42
if (len < MIN_PATH)
43
throw new RuntimeException("Attempting to generate path less than MIN_PATH");
44
StringBuilder sb = new StringBuilder(len);
45
sb.append("name");
46
while (sb.length() < len) {
47
sb.append('X');
48
}
49
return Paths.get(sb.toString());
50
}
51
52
static boolean tryCreateFile(int len) throws IOException {
53
Path name = generatePath(len);
54
try {
55
Files.createFile(name);
56
} catch (IOException ioe) {
57
System.err.format("Unable to create file of length %d (full path %d), %s%n",
58
name.toString().length(), name.toAbsolutePath().toString().length(), ioe);
59
return false;
60
}
61
Files.delete(name);
62
return true;
63
}
64
65
static boolean tryCreateDirectory(int len) throws IOException {
66
Path name = generatePath(len);
67
try {
68
Files.createDirectory(name);
69
} catch (IOException ioe) {
70
System.err.format("Unable to create directory of length %d (full path %d), %s%n",
71
name.toString().length(), name.toAbsolutePath().toString().length(), ioe);
72
return false;
73
}
74
Files.delete(name);
75
return true;
76
}
77
78
public static void main(String[] args) throws Exception {
79
int len;
80
81
// find the maximum file name if MAX_PATH or less
82
len = MAX_PATH;
83
while (!tryCreateFile(len)) {
84
len--;
85
}
86
System.out.format("Testing createFile on paths %d .. %d%n", MIN_PATH, len);
87
while (len >= MIN_PATH) {
88
if (!tryCreateFile(len--))
89
throw new RuntimeException("Test failed");
90
}
91
92
// find the maximum directory name if MAX_PATH or less
93
len = MAX_PATH;
94
while (!tryCreateDirectory(len)) {
95
len--;
96
}
97
System.out.format("Testing createDirectory on paths %d .. %d%n", MIN_PATH, len);
98
while (len >= MIN_PATH) {
99
if (!tryCreateDirectory(len--))
100
throw new RuntimeException("Test failed");
101
}
102
}
103
}
104
105