下面列出了java.nio.file.StandardOpenOption#DELETE_ON_CLOSE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
@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"));
}
@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")));
}
@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")));
}
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);
}
@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"));
}
@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"));
}
}
@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")));
}
}
@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"));
}
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);
}