java.nio.file.StandardOpenOption#DELETE_ON_CLOSE源码实例Demo

下面列出了java.nio.file.StandardOpenOption#DELETE_ON_CLOSE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: sftp-fs   文件: OpenOptions.java
static OpenOptions forNewInputStream(Collection<? extends OpenOption> options) {
    if (options.isEmpty()) {
        return new OpenOptions(true, false, false, false, false, false, Collections.<OpenOption>emptySet());
    }

    boolean deleteOnClose = false;

    for (OpenOption option : options) {
        if (option == StandardOpenOption.DELETE_ON_CLOSE) {
            deleteOnClose = true;
        } else if (option != StandardOpenOption.READ && option != StandardOpenOption.TRUNCATE_EXISTING && !isIgnoredOpenOption(option)) {
            // TRUNCATE_EXISTING is ignored in combination with READ
            throw Messages.fileSystemProvider().unsupportedOpenOption(option);
        }
    }

    return new OpenOptions(true, false, false, false, false, deleteOnClose, options);
}
 
源代码2 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamExistingDeleteOnClose() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    OpenOption[] options = { StandardOpenOption.DELETE_ON_CLOSE };
    try (OutputStream output = fileSystem.newOutputStream(createPath("/foo/bar"), options)) {
        // don't do anything with the stream, there's a separate test for that
        assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    }
    // verify that the file system can be used after closing the stream
    fileSystem.checkAccess(createPath("/foo"));

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertFalse(Files.exists(getPath("/foo/bar")));
    assertEquals(0, getChildCount("/foo"));
}
 
源代码3 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamExistingCreateDeleteOnClose() throws IOException {
    addDirectory("/foo");
    addFile("/foo/bar");

    OpenOption[] options = { StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE };
    try (OutputStream output = fileSystem.newOutputStream(createPath("/foo/bar"), options)) {
        // don't do anything with the stream, there's a separate test for that
        assertTrue(Files.isRegularFile(getPath("/foo/bar")));
    }
    // verify that the file system can be used after closing the stream
    fileSystem.checkAccess(createPath("/foo"));

    assertTrue(Files.isDirectory(getPath("/foo")));
    assertFalse(Files.exists(getPath("/foo/bar")));
}
 
源代码4 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamExistingSFTPFailureDeleteOnClose() throws IOException {
    addDirectory("/foo");
    Path bar = addFile("/foo/bar");
    bar.toFile().setReadOnly();

    // failure: no permission to write

    OpenOption[] options = { StandardOpenOption.DELETE_ON_CLOSE };
    FileSystemException exception = assertThrows(FileSystemException.class, () -> fileSystem.newOutputStream(createPath("/foo/bar"), options));
    assertEquals("/foo/bar", exception.getFile());

    verify(getExceptionFactory()).createNewOutputStreamException(eq("/foo/bar"), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertTrue(Files.isRegularFile(getPath("/foo/bar")));
}
 
源代码5 项目: sftp-fs   文件: OpenOptions.java
static OpenOptions forNewOutputStream(Collection<? extends OpenOption> options) {
    if (options.isEmpty()) {
        // CREATE, TRUNCATE_EXISTING and WRITE, i.e. create, not createNew, and not append
        return new OpenOptions(false, true, false, true, false, false, Collections.<OpenOption>emptySet());
    }

    boolean append = false;
    boolean truncateExisting = false;
    boolean create = false;
    boolean createNew = false;
    boolean deleteOnClose = false;

    for (OpenOption option : options) {
        if (option == StandardOpenOption.APPEND) {
            append = true;
        } else if (option == StandardOpenOption.TRUNCATE_EXISTING) {
            truncateExisting = true;
        } else if (option == StandardOpenOption.CREATE) {
            create = true;
        } else if (option == StandardOpenOption.CREATE_NEW) {
            createNew = true;
        } else if (option == StandardOpenOption.DELETE_ON_CLOSE) {
            deleteOnClose = true;
        } else if (option != StandardOpenOption.WRITE && !isIgnoredOpenOption(option)) {
            throw Messages.fileSystemProvider().unsupportedOpenOption(option);
        }
    }

    // append and truncateExisting contradict each other
    if (append && truncateExisting) {
        throw Messages.fileSystemProvider().illegalOpenOptionCombination(options);
    }

    return new OpenOptions(false, true, append, create, createNew, deleteOnClose, options);
}
 
源代码6 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewInputStreamDeleteOnClose() throws IOException {
    addFile("/foo/bar");

    OpenOption[] options = { StandardOpenOption.DELETE_ON_CLOSE };
    try (InputStream input = fileSystem.newInputStream(createPath("/foo/bar"), options)) {
        // don't do anything with the stream, there's a separate test for that
    }
    assertFalse(Files.exists(getPath("/foo/bar")));
    assertEquals(0, getChildCount("/foo"));
}
 
源代码7 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamNonExistingCreateDeleteOnClose() throws IOException {
    addDirectory("/foo");

    OpenOption[] options = { StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE };
    try (OutputStream input = fileSystem.newOutputStream(createPath("/foo/bar"), options)) {
        // don't do anything with the stream, there's a separate test for that
        // we can't check here that /foo/bar exists, because it will only be stored in the file system once the stream is closed
    } finally {
        assertTrue(Files.isDirectory(getPath("/foo")));
        assertFalse(Files.exists(getPath("/foo/bar")));
        assertEquals(0, getChildCount("/foo"));
    }
}
 
源代码8 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamNonExistingCreateNewDeleteOnClose() throws IOException {
    addDirectory("/foo");

    OpenOption[] options = { StandardOpenOption.CREATE_NEW, StandardOpenOption.DELETE_ON_CLOSE };
    try (OutputStream input = fileSystem.newOutputStream(createPath("/foo/bar"), options)) {
        // don't do anything with the stream, there's a separate test for that
        // we can't check here that /foo/bar exists, because it will only be stored in the file system once the stream is closed
    } finally {
        assertTrue(Files.isDirectory(getPath("/foo")));
        assertFalse(Files.exists(getPath("/foo/bar")));
    }
}
 
源代码9 项目: sftp-fs   文件: SFTPFileSystemTest.java
@Test
public void testNewOutputStreamDirectoryDeleteOnClose() throws IOException {
    addDirectory("/foo");

    OpenOption[] options = { StandardOpenOption.DELETE_ON_CLOSE };
    FileSystemException exception = assertThrows(FileSystemException.class, () -> fileSystem.newOutputStream(createPath("/foo"), options));
    assertEquals("/foo", exception.getFile());
    assertEquals(Messages.fileSystemProvider().isDirectory("/foo").getReason(), exception.getReason());

    verify(getExceptionFactory(), never()).createNewOutputStreamException(anyString(), any(SftpException.class), anyCollection());
    assertTrue(Files.isDirectory(getPath("/foo")));
    assertEquals(0, getChildCount("/foo"));
}
 
源代码10 项目: sftp-fs   文件: OpenOptions.java
static OpenOptions forNewByteChannel(Set<? extends OpenOption> options) {

        boolean read = false;
        boolean write = false;
        boolean append = false;
        boolean truncateExisting = false;
        boolean create = false;
        boolean createNew = false;
        boolean deleteOnClose = false;

        for (OpenOption option : options) {
            if (option == StandardOpenOption.READ) {
                read = true;
            } else if (option == StandardOpenOption.WRITE) {
                write = true;
            } else if (option == StandardOpenOption.APPEND) {
                append = true;
            } else if (option == StandardOpenOption.TRUNCATE_EXISTING) {
                truncateExisting = true;
            } else if (option == StandardOpenOption.CREATE) {
                create = true;
            } else if (option == StandardOpenOption.CREATE_NEW) {
                createNew = true;
            } else if (option == StandardOpenOption.DELETE_ON_CLOSE) {
                deleteOnClose = true;
            } else if (!isIgnoredOpenOption(option)) {
                throw Messages.fileSystemProvider().unsupportedOpenOption(option);
            }
        }

        // as per Files.newByteChannel, if none of these options is given, default to read
        if (!read && !write && !append) {
            read = true;
        }

        // read contradicts with write, append, create and createNew; TRUNCATE_EXISTING is ignored in combination with READ
        if (read && (write || append || create || createNew)) {
            throw Messages.fileSystemProvider().illegalOpenOptionCombination(options);
        }

        // append and truncateExisting contract each other
        if (append && truncateExisting) {
            throw Messages.fileSystemProvider().illegalOpenOptionCombination(options);
        }

        // read and write contract each other; read and append contract each other; append and truncateExisting contract each other
        if ((read && write) || (read && append) || (append && truncateExisting)) {
            throw Messages.fileSystemProvider().illegalOpenOptionCombination(options);
        }

        return new OpenOptions(read, write, append, create, createNew, deleteOnClose, options);
    }