/* Generate a filename unique to this run */ privatestatic String gensym() { return"x." + ++gensymCounter;
}
/** *Createfilesandfoldersinthetestworkingdirectory. *Thepurposeistomakesurethetestwillnotgooutof *itsuserdirwhenwalkingthefiletree. * *@paramdepthThenumberofdirectorylevelstobecreatedunder *theuserdirectory.Itshouldbethemaximumvalue *ofthedepthspassedtocheckNamesmethod(including *directorindirectcalling)inawholetest.
*/ protectedstaticvoid initTestData(int depth) throws IOException {
File parent = new File(userDir + File.separator + workSubDir); if (!parent.mkdir()) { thrownew IOException("Fail to create directory: " + parent);
} for (int i = 0; i < depth; i++) {
File tmp = new File(parent, gensym());
tmp.createNewFile();
tmp = new File(parent, gensym()); if (tmp.mkdir())
parent = tmp; else thrownew IOException("Fail to create directory, " + tmp);
}
baseDir = parent.getAbsolutePath();
relative = baseDir.substring(userDir.length() + 1);
}
/** *Findafileinthegivensubdirectory,ordescendintofurther *subdirectories,ifany,ifnofileisfoundhere.Returnnullifno *filecanbefoundanywherebeneaththegivensubdirectory. *@paramdirDirectoryatwhichwestarted *@paramsubdirSubdirectorythatwe'reexploring *@paramdlListingofsubdirectory
*/ privatestatic String findSomeFile(String dir, String subdir, String[] dl) { for (int i = 0; i < dl.length; i++) {
File f = new File(subdir, dl[i]);
File df = new File(dir, f.getPath()); if (Files.isRegularFile(df.toPath(), LinkOption.NOFOLLOW_LINKS)) { return f.getPath();
}
} for (int i = 0; i < dl.length; i++) {
File f = (subdir.length() == 0) ? new File(dl[i])
: new File(subdir, dl[i]);
File df = new File(dir, f.getPath()); if (Files.isDirectory(df.toPath(), LinkOption.NOFOLLOW_LINKS)) {
String[] dl2 = df.list(); if (dl2 != null) {
String ff = findSomeFile(dir, f.getPath(), dl2); if (ff != null) return ff;
}
}
} returnnull;
}
/** *Constructastringthatnamesafileinthegivendirectory.Ifcreate *istrue,thencreateafileifnoneisfound,andthrowanexceptionif *thatisnotpossible;otherwise,returnnullifnofilecanbefound.
*/ privatestatic String findSomeFile(String dir, boolean create) {
File d = new File(dir);
String[] dl = d.list(); if (dl == null) { thrownew RuntimeException("Can't list " + dir);
} for (int i = 0; i < dl.length; i++) {
File f = new File(dir, dl[i]); if (Files.isRegularFile(f.toPath(), LinkOption.NOFOLLOW_LINKS)) { return dl[i];
}
}
String f = findSomeFile(dir, "", dl); if (f != null) { return f;
} if (create) {
File nf = new File(d, gensym());
OutputStream os; try {
os = new FileOutputStream(nf);
os.close();
} catch (IOException x) { thrownew RuntimeException("Can't create a file in " + dir);
} return nf.getName();
} returnnull;
}
/** *Constructastringthatnamesasubdirectoryofthegivendirectory. *Ifcreateistrue,thencreateasubdirectoryifnoneisfound,and *throwanexceptionifthatisnotpossible;otherwise,returnnullif *nosubdirectorycanbefound.
*/ privatestatic String findSomeDir(String dir, boolean create) {
File d = new File(dir);
String[] dl = d.list(); if (dl == null) { thrownew RuntimeException("Can't list " + dir);
} for (int i = 0; i < dl.length; i++) {
File f = new File(d, dl[i]); if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS)) {
String[] dl2 = f.list(); if (dl2 == null || dl2.length >= 250) { /* Heuristic to avoid scanning huge directories */ continue;
} return dl[i];
}
} if (create) {
File sd = new File(d, gensym()); if (sd.mkdir()) return sd.getName();
} returnnull;
}
/** Construct a string that does not name a file in the given directory */ privatestatic String findNon(String dir) {
File d = new File(dir);
String[] x = new String[] { "foo", "bar", "baz" }; for (int i = 0; i < x.length; i++) {
File f = new File(d, x[i]); if (!f.exists()) { return x[i];
}
} for (int i = 0; i < 1024; i++) {
String n = "xx" + Integer.toString(i);
File f = new File(d, n); if (!f.exists()) { return n;
}
} thrownew RuntimeException("Can't find a non-existent file in " + dir);
}
/** Ensure that the named file does not exist */ publicstaticvoid ensureNon(String fn) { if ((new File(fn)).exists()) { thrownew RuntimeException("Test path " + fn + " exists");
}
}
/** Tell whether the given character is a "slash" on this platform */ privatestaticboolean isSlash(char x) { if (x == File.separatorChar) returntrue; if (win32 && (x == '/')) returntrue; returnfalse;
}
/** *Trimtrailingslashesfromthegivenstring,butleavesingletonslashes *alone(theydenoterootdirectories)
*/ privatestatic String trimTrailingSlashes(String s) { int n = s.length(); if (n == 0) return s;
n--; while ((n > 0) && isSlash(s.charAt(n))) { if ((n >= 1) && s.charAt(n - 1) == ':') break;
n--;
} return s.substring(0, n + 1);
}
/** Concatenate two paths, trimming slashes as needed */ privatestatic String pathConcat(String a, String b) { if (a.length() == 0) return b; if (b.length() == 0) return a; if (isSlash(a.charAt(a.length() - 1))
|| isSlash(b.charAt(0))
|| (win32 && (a.charAt(a.length() - 1) == ':'))) { return a + b;
} else { return a + File.separatorChar + b;
}
}
/** Hash table of input pathnames, used to detect duplicates */ privatestatic Hashtable<String, String> checked = new Hashtable<>();
/** Check a single slash case, plus its children */ privatestaticvoid checkSlash(int depth, boolean create,
String ans, String ask, String slash) throws Exception
{
check(ans, ask + slash);
checkNames(depth, create,
ans.endsWith(File.separator) ? ans : ans + File.separator,
ask + slash);
}
/** Check slash cases for the given ask string */ publicstaticvoid checkSlashes(int depth, boolean create,
String ans, String ask) throws Exception
{
check(ans, ask); if (depth == 0) return;
checkSlash(depth, create, ans, ask, "/");
checkSlash(depth, create, ans, ask, "//");
checkSlash(depth, create, ans, ask, "///"); if (win32) {
checkSlash(depth, create, ans, ask, "\\");
checkSlash(depth, create, ans, ask, "\\\\");
checkSlash(depth, create, ans, ask, "\\/");
checkSlash(depth, create, ans, ask, "/\\");
checkSlash(depth, create, ans, ask, "\\\\\\");
}
}
/** Check name cases for the given ask string */ publicstaticvoid checkNames(int depth, boolean create,
String ans, String ask) throws Exception
{ int d = depth - 1;
File f = new File(ans);
String n;
/* Normal name */ if (f.exists()) { if (Files.isDirectory(f.toPath(), LinkOption.NOFOLLOW_LINKS) && f.list() != null) { if ((n = findSomeFile(ans, create)) != null)
checkSlashes(d, create, ans + n, ask + n); if ((n = findSomeDir(ans, create)) != null)
checkSlashes(d, create, ans + n, ask + n);
}
n = findNon(ans);
checkSlashes(d, create, ans + n, ask + n);
} else {
n = "foo" + depth;
checkSlashes(d, create, ans + n, ask + n);
}
/* ".." */ if ((n = f.getParent()) != null) {
String n2; if (win32
&& ((n2 = f.getParentFile().getParent()) != null)
&& n2.equals("\\\\")) { /* Win32 resolves \\foo\bar\.. to \\foo\bar */
checkSlashes(d, create, ans, ask + "..");
} else {
checkSlashes(d, create, n, ask + "..");
}
} else { if (win32)
checkSlashes(d, create, ans, ask + ".."); else { // Fix for 4237875. We must ensure that we are sufficiently // deep in the path hierarchy to test parents this high up
File thisPath = new File(ask);
File nextPath = new File(ask + ".."); if (!thisPath.getCanonicalPath().equals(nextPath.getCanonicalPath()))
checkSlashes(d, create, ans + "..", ask + "..");
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-10-11)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.