Java Code Examples for com.sun.nio.file.ExtendedCopyOption

The following examples show how to use com.sun.nio.file.ExtendedCopyOption. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: dragonwell8_jdk   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 2
Source Project: TencentKona-8   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 3
Source Project: jdk8u60   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 4
Source Project: openjdk-jdk8u   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 5
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 6
Source Project: jdk8u-jdk   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 7
Source Project: hottub   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 8
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 9
Source Project: openjdk-8   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 10
Source Project: jdk8u_jdk   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 11
Source Project: jdk8u-jdk   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 12
Source Project: jdk8u-dev-jdk   Source File: UnixCopyFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
static Flags fromCopyOptions(CopyOption... options) {
    Flags flags = new Flags();
    flags.followLinks = true;
    for (CopyOption option: options) {
        if (option == StandardCopyOption.REPLACE_EXISTING) {
            flags.replaceExisting = true;
            continue;
        }
        if (option == LinkOption.NOFOLLOW_LINKS) {
            flags.followLinks = false;
            continue;
        }
        if (option == StandardCopyOption.COPY_ATTRIBUTES) {
            // copy all attributes but only fail if basic attributes
            // cannot be copied
            flags.copyBasicAttributes = true;
            flags.copyPosixAttributes = true;
            flags.copyNonPosixAttributes = true;
            flags.failIfUnableToCopyBasic = true;
            continue;
        }
        if (option == ExtendedCopyOption.INTERRUPTIBLE) {
            flags.interruptible = true;
            continue;
        }
        if (option == null)
            throw new NullPointerException();
        throw new UnsupportedOperationException("Unsupported copy option");
    }
    return flags;
}
 
Example 13
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 14
Source Project: TencentKona-8   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 15
Source Project: jdk8u60   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 16
Source Project: openjdk-jdk8u   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 17
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 18
Source Project: openjdk-jdk9   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 19
Source Project: jdk8u-jdk   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 20
Source Project: hottub   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 21
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 22
Source Project: openjdk-8   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 23
Source Project: jdk8u_jdk   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 24
Source Project: jdk8u-jdk   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}
 
Example 25
Source Project: jdk8u-dev-jdk   Source File: InterruptCopy.java    License: GNU General Public License v2.0 4 votes vote down vote up
static void doTest(Path dir) throws Exception {
    final Path source = dir.resolve("foo");
    final Path target = dir.resolve("bar");

    // 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();
    try {
        // copy source to target in main thread, interrupting it after a delay
        final Thread me = Thread.currentThread();
        Future<?> wakeup = pool.schedule(new Runnable() {
            public void run() {
                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();
            if (!interrupted)
                throw new RuntimeException("Interrupt status was not set");
            System.out.println("Copy failed (this is expected)");
        }
        try {
            wakeup.get();
        } catch (InterruptedException ignore) { }
        Thread.interrupted();

        // 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);
        boolean cancelled = result.cancel(true);
        if (!cancelled)
            result.get();
        System.out.println("Copy cancelled.");
    } finally {
        pool.shutdown();
    }
}