use of java.nio.file.OpenOption in project neo4j by neo4j.
the class MuninnPageCache method map.
@Override
public synchronized PagedFile map(File file, int filePageSize, OpenOption... openOptions) throws IOException {
assertHealthy();
ensureThreadsInitialised();
if (filePageSize > cachePageSize) {
throw new IllegalArgumentException("Cannot map files with a filePageSize (" + filePageSize + ") that is greater than the " + "cachePageSize (" + cachePageSize + ")");
}
file = file.getCanonicalFile();
boolean createIfNotExists = false;
boolean truncateExisting = false;
boolean deleteOnClose = false;
boolean anyPageSize = false;
for (OpenOption option : openOptions) {
if (option.equals(StandardOpenOption.CREATE)) {
createIfNotExists = true;
} else if (option.equals(StandardOpenOption.TRUNCATE_EXISTING)) {
truncateExisting = true;
} else if (option.equals(StandardOpenOption.DELETE_ON_CLOSE)) {
deleteOnClose = true;
} else if (option.equals(PageCacheOpenOptions.ANY_PAGE_SIZE)) {
anyPageSize = true;
} else if (!ignoredOpenOptions.contains(option)) {
throw new UnsupportedOperationException("Unsupported OpenOption: " + option);
}
}
FileMapping current = mappedFiles;
// find an existing mapping
while (current != null) {
if (current.file.equals(file)) {
MuninnPagedFile pagedFile = current.pagedFile;
if (pagedFile.pageSize() != filePageSize && !anyPageSize) {
String msg = "Cannot map file " + file + " with " + "filePageSize " + filePageSize + " bytes, " + "because it has already been mapped with a " + "filePageSize of " + pagedFile.pageSize() + " bytes.";
throw new IllegalArgumentException(msg);
}
if (truncateExisting) {
throw new UnsupportedOperationException("Cannot truncate a file that is already mapped");
}
pagedFile.incrementRefCount();
pagedFile.markDeleteOnClose(deleteOnClose);
return pagedFile;
}
current = current.next;
}
if (filePageSize < Long.BYTES) {
throw new IllegalArgumentException("Cannot map files with a filePageSize (" + filePageSize + ") that is less than " + Long.BYTES + " bytes");
}
// there was no existing mapping
MuninnPagedFile pagedFile = new MuninnPagedFile(file, this, filePageSize, swapperFactory, pageCacheTracer, pageCursorTracerSupplier, createIfNotExists, truncateExisting);
pagedFile.incrementRefCount();
pagedFile.markDeleteOnClose(deleteOnClose);
current = new FileMapping(file, pagedFile);
current.next = mappedFiles;
mappedFiles = current;
pageCacheTracer.mappedFile(file);
return pagedFile;
}
use of java.nio.file.OpenOption in project java-chassis by ServiceComb.
the class FortifyUtils method writeFile.
public static void writeFile(String file, byte[] content) throws IOException {
Set<OpenOption> options = new HashSet<OpenOption>();
//options.add(StandardOpenOption.CREATE_NEW);
//options.add(StandardOpenOption.APPEND);
//覆写文件
options.add(StandardOpenOption.CREATE);
options.add(StandardOpenOption.WRITE);
SeekableByteChannel sbc = null;
try {
FileAttribute<?> fa = getDefaultFileAttributes(file);
ByteBuffer buffer = ByteBuffer.wrap(content);
sbc = Files.newByteChannel(new File(file).toPath(), options, fa);
// write data
sbc.write(buffer);
} finally {
IOUtils.closeQuietly(sbc);
}
}
use of java.nio.file.OpenOption in project jdk8u_jdk by JetBrains.
the class BytesAndLines method testNulls.
/**
* Exercise NullPointerException
*/
public void testNulls() {
Path file = Paths.get("foo");
byte[] bytes = new byte[100];
List<String> lines = Collections.emptyList();
checkNullPointerException(() -> Files.readAllBytes(null));
checkNullPointerException(() -> Files.write(null, bytes));
checkNullPointerException(() -> Files.write(file, (byte[]) null));
checkNullPointerException(() -> Files.write(file, bytes, (OpenOption[]) null));
checkNullPointerException(() -> Files.write(file, bytes, new OpenOption[] { null }));
checkNullPointerException(() -> Files.readAllLines(null));
checkNullPointerException(() -> Files.readAllLines(file, (Charset) null));
checkNullPointerException(() -> Files.readAllLines(null, Charset.defaultCharset()));
checkNullPointerException(() -> Files.write(null, lines));
checkNullPointerException(() -> Files.write(file, (List<String>) null));
checkNullPointerException(() -> Files.write(file, lines, (OpenOption[]) null));
checkNullPointerException(() -> Files.write(file, lines, new OpenOption[] { null }));
checkNullPointerException(() -> Files.write(null, lines, Charset.defaultCharset()));
checkNullPointerException(() -> Files.write(file, null, Charset.defaultCharset()));
checkNullPointerException(() -> Files.write(file, lines, (Charset) null));
checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), (OpenOption[]) null));
checkNullPointerException(() -> Files.write(file, lines, Charset.defaultCharset(), new OpenOption[] { null }));
}
use of java.nio.file.OpenOption in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProvider method newReadChannel.
private SeekableByteChannel newReadChannel(Path path, Set<? extends OpenOption> options) throws IOException {
initStorage();
int maxChannelReopens = ((CloudStorageFileSystem) path.getFileSystem()).config().maxChannelReopens();
for (OpenOption option : options) {
if (option instanceof StandardOpenOption) {
switch((StandardOpenOption) option) {
case READ:
// Default behavior.
break;
case SPARSE:
case TRUNCATE_EXISTING:
// Ignored by specification.
break;
case WRITE:
throw new IllegalArgumentException("READ+WRITE not supported yet");
case APPEND:
case CREATE:
case CREATE_NEW:
case DELETE_ON_CLOSE:
case DSYNC:
case SYNC:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof OptionMaxChannelReopens) {
maxChannelReopens = ((OptionMaxChannelReopens) option).maxChannelReopens();
} else {
throw new UnsupportedOperationException(option.toString());
}
}
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
return CloudStorageReadChannel.create(storage, cloudPath.getBlobId(), 0, maxChannelReopens);
}
use of java.nio.file.OpenOption in project google-cloud-java by GoogleCloudPlatform.
the class CloudStorageFileSystemProvider method newWriteChannel.
private SeekableByteChannel newWriteChannel(Path path, Set<? extends OpenOption> options) throws IOException {
initStorage();
CloudStoragePath cloudPath = CloudStorageUtil.checkPath(path);
if (cloudPath.seemsLikeADirectoryAndUsePseudoDirectories()) {
throw new CloudStoragePseudoDirectoryException(cloudPath);
}
BlobId file = cloudPath.getBlobId();
BlobInfo.Builder infoBuilder = BlobInfo.newBuilder(file);
List<Storage.BlobWriteOption> writeOptions = new ArrayList<>();
List<Acl> acls = new ArrayList<>();
HashMap<String, String> metas = new HashMap<>();
for (OpenOption option : options) {
if (option instanceof OptionMimeType) {
infoBuilder.setContentType(((OptionMimeType) option).mimeType());
} else if (option instanceof OptionCacheControl) {
infoBuilder.setCacheControl(((OptionCacheControl) option).cacheControl());
} else if (option instanceof OptionContentDisposition) {
infoBuilder.setContentDisposition(((OptionContentDisposition) option).contentDisposition());
} else if (option instanceof OptionContentEncoding) {
infoBuilder.setContentEncoding(((OptionContentEncoding) option).contentEncoding());
} else if (option instanceof OptionUserMetadata) {
OptionUserMetadata opMeta = (OptionUserMetadata) option;
metas.put(opMeta.key(), opMeta.value());
} else if (option instanceof OptionAcl) {
acls.add(((OptionAcl) option).acl());
} else if (option instanceof OptionBlockSize) {
// TODO: figure out how to plumb in block size.
} else if (option instanceof StandardOpenOption) {
switch((StandardOpenOption) option) {
case CREATE:
case TRUNCATE_EXISTING:
case WRITE:
// Default behavior.
break;
case SPARSE:
// Ignored by specification.
break;
case CREATE_NEW:
writeOptions.add(Storage.BlobWriteOption.doesNotExist());
break;
case READ:
throw new IllegalArgumentException("READ+WRITE not supported yet");
case APPEND:
case DELETE_ON_CLOSE:
case DSYNC:
case SYNC:
default:
throw new UnsupportedOperationException(option.toString());
}
} else if (option instanceof CloudStorageOption) {
// XXX: We need to interpret these later
} else {
throw new UnsupportedOperationException(option.toString());
}
}
if (!metas.isEmpty()) {
infoBuilder.setMetadata(metas);
}
if (!acls.isEmpty()) {
infoBuilder.setAcl(acls);
}
try {
return new CloudStorageWriteChannel(storage.writer(infoBuilder.build(), writeOptions.toArray(new Storage.BlobWriteOption[writeOptions.size()])));
} catch (StorageException oops) {
throw asIoException(oops);
}
}
Aggregations