com.sun.nio.file.ExtendedCopyOption Java Examples
The following examples show how to use
com.sun.nio.file.ExtendedCopyOption.
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 File: UnixCopyFile.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From hottub with GNU General Public License v2.0 | 5 votes |
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
Source File: UnixCopyFile.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
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
Source File: UnixCopyFile.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
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 File: UnixCopyFile.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
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
Source File: InterruptCopy.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
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
Source File: InterruptCopy.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From hottub with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
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
Source File: InterruptCopy.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From openjdk-jdk8u with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
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 File: InterruptCopy.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
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(); } }