test/java/nio/file/Files/InterruptCopy.java
Print this page
@@ -27,11 +27,10 @@
* @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 {
@@ -41,14 +40,13 @@
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();
+ FileStore store = Files.getFileStore(dir);
System.out.format("Checking space (%s)\n", store);
- long usableSpace = Attributes
- .readFileStoreSpaceAttributes(store).usableSpace();
+ long usableSpace = store.getUsableSpace();
if (usableSpace < 2*FILE_SIZE_TO_COPY) {
System.out.println("Insufficient disk space to run test.");
return;
}
doTest(dir);
@@ -64,18 +62,15 @@
// 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 {
+ try (OutputStream out = Files.newOutputStream(source)) {
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();
@@ -87,11 +82,11 @@
me.interrupt();
}}, DELAY_IN_MS, TimeUnit.MILLISECONDS);
System.out.println("Copying file...");
try {
long start = System.currentTimeMillis();
- source.copyTo(target, ExtendedCopyOption.INTERRUPTIBLE);
+ 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,11 +102,11 @@
// 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,
+ Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,
StandardCopyOption.REPLACE_EXISTING);
return null;
}
});
Thread.sleep(DELAY_IN_MS);