privatestaticvoid deleteFileWithRetry0(Path path) throws IOException, InterruptedException { int times = 0;
IOException ioe = null; while (true) { try {
Files.delete(path); // Checks for absence of the file. Semantics of Files.exists() is not the same. while (!Files.notExists(path)) {
times++; if (times > MAX_RETRY_DELETE_TIMES) { thrownew IOException("File still exists after " + times + " waits.");
} Thread.sleep(RETRY_DELETE_MILLIS);
} break;
} catch (NoSuchFileException | DirectoryNotEmptyException x) { throw x;
} catch (IOException x) { // Backoff/retry in case another process is accessing the file
times++; if (ioe == null) {
ioe = x;
} else {
ioe.addSuppressed(x);
}
final AtomicBoolean areMountPointsOK = new AtomicBoolean(true); Thread thr = newThread(() -> { try {
Process proc = new ProcessBuilder("df").start();
BufferedReader reader = new BufferedReader
(new InputStreamReader(proc.getInputStream())); // Skip the first line as it is the "df" output header. if (reader.readLine() != null ) {
Set mountPoints = new HashSet();
String mountPoint = null; while ((mountPoint = reader.readLine()) != null) { if (!mountPoints.add(mountPoint)) {
System.err.printf
("Config error: duplicate mount point %s%n",
mountPoint);
areMountPointsOK.set(false); break;
}
}
}
// Return the current process handle count publicstaticlong getProcessHandleCount() { if (IS_WINDOWS) { if (!nativeLibLoaded) {
System.loadLibrary("FileUtils");
nativeLibLoaded = true;
} return getWinProcessHandleCount();
} else { return ((UnixOperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean()).getOpenFileDescriptorCount();
}
}
/** *Patchesapartofafile. * *@parampaththefile *@paramfromLinethefirstlinetopatch.Thisisthenumberyouseeinaneditor,1-based,inclusive. *@paramtoLinethelastlinetopatch.Thisisthenumberyouseeinaneditor,inclusive. *Set{@codetoLine}to{@codefromLine-1}ifyouonlywanttoinsertlines. *@paramfromlinestoremove,usedtoensurethecorrectlinesareremoved.Canbemultiplelinesorempty. *It'scomparedtoexistinglineswithalllinestrimmedandnonewlinesatbothends.Ignoredifnull. *@paramtothenewlyaddedlines,canbemultiplelinesorempty.Newlineatendisoptional.Cannotbenull. *@throwsIOExceptionifthere'sanI/Oerroror{@codefrom}doesnotmatchtheexistinglines *@throwsIndexOutOfBoundsExceptionif{@codefromLine}or{@codetoLine}isinvalid
*/ publicstaticvoid patch(Path path, int fromLine, int toLine, String from, String to) throws IOException { var lines = Files.readAllLines(path); // The next line does a from/to as well var subList = lines.subList(fromLine - 1, toLine); if (from != null) { // Each line is trimmed so caller needs not care about indentation. // Caller also needs not care about new lines on both ends. // New lines inside are preserved.
String actuallyRemoved = subList.stream()
.map(String::trim)
.collect(Collectors.joining("\n")).trim();
String wantToRemove = from.lines()
.map(String::trim)
.collect(Collectors.joining("\n")).trim(); if (!actuallyRemoved.equals(wantToRemove)) { thrownew IOException("Removed not the same: ["
+ String.join("\\n", subList) + "] and ["
+ from.replaceAll("\\n", "\\\\n") + "]");
}
}
subList.clear();
lines.addAll(fromLine - 1, to.lines().toList());
Files.write(path, lines);
}
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.