test/java/nio/file/Files/InterruptCopy.java
Print this page
*** 27,37 ****
* @library ..
* @run main/othervm -XX:-UseVMInterruptibleIO InterruptCopy
*/
import java.nio.file.*;
- import java.nio.file.attribute.Attributes;
import java.io.*;
import java.util.concurrent.*;
import com.sun.nio.file.ExtendedCopyOption;
public class InterruptCopy {
--- 27,36 ----
*** 41,54 ****
private static final int DURATION_MAX_IN_MS = 5000;
public static void main(String[] args) throws Exception {
Path dir = TestUtil.createTemporaryDirectory();
try {
! FileStore store = dir.getFileStore();
System.out.format("Checking space (%s)\n", store);
! long usableSpace = Attributes
! .readFileStoreSpaceAttributes(store).usableSpace();
if (usableSpace < 2*FILE_SIZE_TO_COPY) {
System.out.println("Insufficient disk space to run test.");
return;
}
doTest(dir);
--- 40,52 ----
private static final int DURATION_MAX_IN_MS = 5000;
public static void main(String[] args) throws Exception {
Path dir = TestUtil.createTemporaryDirectory();
try {
! FileStore store = Files.getFileStore(dir);
System.out.format("Checking space (%s)\n", store);
! long usableSpace = store.getUsableSpace();
if (usableSpace < 2*FILE_SIZE_TO_COPY) {
System.out.println("Insufficient disk space to run test.");
return;
}
doTest(dir);
*** 64,81 ****
// create source file (don't create it as sparse file because we
// require the copy to take a long time)
System.out.println("Creating source file...");
byte[] buf = new byte[32*1024];
long total = 0;
! OutputStream out = source.newOutputStream();
! try {
do {
out.write(buf);
total += buf.length;
} while (total < FILE_SIZE_TO_COPY);
- } finally {
- out.close();
}
System.out.println("Source file created.");
ScheduledExecutorService pool =
Executors.newSingleThreadScheduledExecutor();
--- 62,76 ----
// create source file (don't create it as sparse file because we
// require the copy to take a long time)
System.out.println("Creating source file...");
byte[] buf = new byte[32*1024];
long total = 0;
! try (OutputStream out = Files.newOutputStream(source)) {
do {
out.write(buf);
total += buf.length;
} while (total < FILE_SIZE_TO_COPY);
}
System.out.println("Source file created.");
ScheduledExecutorService pool =
Executors.newSingleThreadScheduledExecutor();
*** 87,97 ****
me.interrupt();
}}, DELAY_IN_MS, TimeUnit.MILLISECONDS);
System.out.println("Copying file...");
try {
long start = System.currentTimeMillis();
! source.copyTo(target, ExtendedCopyOption.INTERRUPTIBLE);
long duration = System.currentTimeMillis() - start;
if (duration > DURATION_MAX_IN_MS)
throw new RuntimeException("Copy was not interrupted");
} catch (IOException e) {
boolean interrupted = Thread.interrupted();
--- 82,92 ----
me.interrupt();
}}, DELAY_IN_MS, TimeUnit.MILLISECONDS);
System.out.println("Copying file...");
try {
long start = System.currentTimeMillis();
! Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE);
long duration = System.currentTimeMillis() - start;
if (duration > DURATION_MAX_IN_MS)
throw new RuntimeException("Copy was not interrupted");
} catch (IOException e) {
boolean interrupted = Thread.interrupted();
*** 107,117 ****
// copy source to target via task in thread pool, interrupting it after
// a delay using cancel(true)
Future<Void> result = pool.submit(new Callable<Void>() {
public Void call() throws IOException {
System.out.println("Copying file...");
! source.copyTo(target, ExtendedCopyOption.INTERRUPTIBLE,
StandardCopyOption.REPLACE_EXISTING);
return null;
}
});
Thread.sleep(DELAY_IN_MS);
--- 102,112 ----
// copy source to target via task in thread pool, interrupting it after
// a delay using cancel(true)
Future<Void> result = pool.submit(new Callable<Void>() {
public Void call() throws IOException {
System.out.println("Copying file...");
! Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,
StandardCopyOption.REPLACE_EXISTING);
return null;
}
});
Thread.sleep(DELAY_IN_MS);