当前位置: 首页>>代码示例>>Java>>正文


Java ExtendedCopyOption类代码示例

本文整理汇总了Java中com.sun.nio.file.ExtendedCopyOption的典型用法代码示例。如果您正苦于以下问题:Java ExtendedCopyOption类的具体用法?Java ExtendedCopyOption怎么用?Java ExtendedCopyOption使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ExtendedCopyOption类属于com.sun.nio.file包,在下文中一共展示了ExtendedCopyOption类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: fromCopyOptions

import com.sun.nio.file.ExtendedCopyOption; //导入依赖的package包/类
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;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:32,代码来源:UnixCopyFile.java

示例2: doTest

import com.sun.nio.file.ExtendedCopyOption; //导入依赖的package包/类
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();
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:64,代码来源:InterruptCopy.java


注:本文中的com.sun.nio.file.ExtendedCopyOption类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。