Java Code Examples for java.nio.file.StandardOpenOption#DELETE_ON_CLOSE

The following examples show how to use java.nio.file.StandardOpenOption#DELETE_ON_CLOSE . 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: OpenOptions.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example 3
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example 4
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 6 votes vote down vote up
@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")));
}
 
Example 5
Source File: OpenOptions.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 7
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@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"));
    }
}
 
Example 8
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@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")));
    }
}
 
Example 9
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 10
Source File: OpenOptions.java    From sftp-fs with Apache License 2.0 4 votes vote down vote up
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);
    }