下面列出了java.nio.file.StandardOpenOption#READ 实例代码,或者点击链接到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);
}
/**
* @throws Exception If failed.
*/
@Test
public void dumpRawPageHistoryToDefaultDir() throws Exception {
diagnosticProcessor.dumpPageHistory(new PageHistoryDiagnoster.DiagnosticPageBuilder()
.pageIds(expectedPageId)
.addAction(PRINT_TO_RAW_FILE)
);
Path path = Paths.get(U.defaultWorkDirectory(), DEFAULT_TARGET_FOLDER);
File dumpFile = path.toFile().listFiles((dir, name) -> name.endsWith(RAW_FILE_FORMAT))[0];
try (SegmentIO io = new SegmentIO(0L, new RandomAccessFileIO(dumpFile, StandardOpenOption.READ))) {
SegmentHeader hdr = RecordV1Serializer.readSegmentHeader(io, new SimpleSegmentFileInputFactory());
assertFalse(hdr.isCompacted());
assertEquals(RecordSerializerFactory.LATEST_SERIALIZER_VERSION, hdr.getSerializerVersion());
}
}
public void sendFile() {
if (this.selectedPeer == null) return;
FileDialog fd = new FileDialog(shlSimpleChatExample, SWT.OPEN);
fd.setText("Choose a file to send");
String selected = fd.open();
if (selected == null) return;
Path path = FileSystems.getDefault().getPath(selected);
OpenOption[] read = { StandardOpenOption.READ };
try {
FileChannel fileChannel = FileChannel.open(path, read);
this.selectedPeer.sendFile(fileChannel, path.getFileName().toString(), (int)fileChannel.size());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Convert Java open options to native flags.
*
* @param modes java options.
* @param log logger.
* @param enableDirect flag for enabling option {@link IgniteNativeIoLib#O_DIRECT} .
* @return native flags for open method.
*/
private static int setupOpenFlags(OpenOption[] modes, IgniteLogger log, boolean enableDirect) {
int flags = enableDirect ? IgniteNativeIoLib.O_DIRECT : 0;
List<OpenOption> openOptionList = Arrays.asList(modes);
for (OpenOption mode : openOptionList) {
if (mode == StandardOpenOption.READ) {
flags |= openOptionList.contains(StandardOpenOption.WRITE)
? IgniteNativeIoLib.O_RDWR
: IgniteNativeIoLib.O_RDONLY;
}
else if (mode == StandardOpenOption.WRITE) {
flags |= openOptionList.contains(StandardOpenOption.READ)
? IgniteNativeIoLib.O_RDWR
: IgniteNativeIoLib.O_WRONLY;
}
else if (mode == StandardOpenOption.CREATE)
flags |= IgniteNativeIoLib.O_CREAT;
else if (mode == StandardOpenOption.TRUNCATE_EXISTING)
flags |= IgniteNativeIoLib.O_TRUNC;
else if (mode == StandardOpenOption.SYNC)
flags |= IgniteNativeIoLib.O_SYNC;
else
log.error("Unsupported open option [" + mode + "]");
}
return flags;
}
/**
* Returns the reader if it is not closed, or thrown an exception otherwise.
*/
private Reader reader() throws DataStoreException {
final Reader r = reader;
if (r == null) {
throw new DataStoreClosedException(getLocale(), Constants.GEOTIFF, StandardOpenOption.READ);
}
return r;
}
/**
* Opens or creates a file, returning an output stream that may be used to
* write bytes to the file. This method works in exactly the manner
* specified by the {@link Files#newOutputStream} method.
*
* <p> The default implementation of this method opens a channel to the file
* as if by invoking the {@link #newByteChannel} method and constructs a
* stream that writes bytes to the channel. This method should be overridden
* where appropriate.
*
* @param path
* the path to the file to open or create
* @param options
* options specifying how the file is opened
*
* @return a new output stream
*
* @throws IllegalArgumentException
* if {@code options} contains an invalid combination of options
* @throws UnsupportedOperationException
* if an unsupported option is specified
* @throws IOException
* if an I/O error occurs
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
*/
public OutputStream newOutputStream(Path path, OpenOption... options)
throws IOException
{
int len = options.length;
Set<OpenOption> opts ;
if (len == 0) {
opts = DEFAULT_OPEN_OPTIONS;
} else {
opts = new HashSet<>();
for (OpenOption opt: options) {
if (opt == StandardOpenOption.READ)
throw new IllegalArgumentException("READ not allowed");
opts.add(opt);
}
opts.add(StandardOpenOption.WRITE);
}
WritableByteChannel wbc = newByteChannel(path, opts);
if (wbc instanceof FileChannelImpl) {
((FileChannelImpl) wbc).setUninterruptible();
}
return Channels.newOutputStream(wbc);
}
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);
}