/* * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
// evaluate System.out of child process
String s; boolean hasOutput = false;
InputStreamReader isr;
isr = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr); while ((s = br.readLine()) != null) { // only throw on Unix as windows over NFS fails... if ((File.separatorChar == '/') && !s.equals("good")) { thrownew RuntimeException("Failed: " + s);
}
hasOutput = true;
}
// evaluate System.err in case of System.out of child process // was empty if (!hasOutput) {
isr = new InputStreamReader(p.getErrorStream());
br = new BufferedReader(isr); if ((s = br.readLine()) != null) {
System.err.println("Error output:");
System.err.println(s); while ((s = br.readLine()) != null) {
System.err.println(s);
}
} thrownew RuntimeException("Failed, no output");
}
/** * Test that overlapping file locking is not possible when using different * FileChannel objects to the same file path
*/ staticvoid test3(File blah) throws Exception { try (RandomAccessFile raf1 = new RandomAccessFile(blah, "rw");
RandomAccessFile raf2 = new RandomAccessFile(blah, "rw"))
{
FileChannel fc1 = raf1.getChannel();
FileChannel fc2 = raf2.getChannel();
// lock via one channel, and then attempt to lock the same file // using a second channel
FileLock fl1 = fc1.lock(); try {
fc2.tryLock(); thrownew RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {} try {
fc2.lock(); thrownew RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {}
// release lock and the attempt to lock with the second channel // should succeed.
fl1.release();
fc2.lock(); try {
fc1.lock(); thrownew RuntimeException("Overlapping locks allowed");
} catch (OverlappingFileLockException x) {}
}
}
/** * Test file locking when file is opened for append
*/ staticvoid test4(File blah) throws Exception { try (FileOutputStream fos = new FileOutputStream(blah, true)) {
FileChannel fc = fos.getChannel();
fc.tryLock().release();
fc.tryLock(0L, 1L, false).release();
fc.lock().release();
fc.lock(0L, 1L, false).release();
} try (FileChannel fc = FileChannel.open(blah.toPath(), APPEND)) {
fc.tryLock().release();
fc.tryLock(0L, 1L, false).release();
fc.lock().release();
fc.lock(0L, 1L, false).release();
}
}
/** * Utility method to be run in secondary process which tries to acquire a * lock on a FileChannel
*/ staticvoid attemptLock(String fileName, boolean expectsLock) throws Exception
{
File f = new File(fileName); try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
FileChannel fc = raf.getChannel(); if (fc.tryLock(10, 10, false) == null) {
System.out.println("bad: Failed to grab adjacent lock");
} if (fc.tryLock(0, 10, false) == null) { if (expectsLock)
System.out.println("bad"); else
System.out.println("good");
} else { if (expectsLock)
System.out.println("good"); else
System.out.println("bad");
}
}
}
}
¤ 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.0.11Bemerkung:
(vorverarbeitet)
¤
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 ist noch experimentell.