Search in sources :

Example 1 with OpenOption

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;
}
Also used : OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption)

Example 2 with OpenOption

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);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) ByteBuffer(java.nio.ByteBuffer) File(java.io.File) HashSet(java.util.HashSet)

Example 3 with OpenOption

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 }));
}
Also used : Path(java.nio.file.Path) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) Charset(java.nio.charset.Charset) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with OpenOption

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);
}
Also used : StandardOpenOption(java.nio.file.StandardOpenOption) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption)

Example 5 with OpenOption

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);
    }
}
Also used : HashMap(java.util.HashMap) StandardOpenOption(java.nio.file.StandardOpenOption) ArrayList(java.util.ArrayList) BlobInfo(com.google.cloud.storage.BlobInfo) Acl(com.google.cloud.storage.Acl) StandardOpenOption(java.nio.file.StandardOpenOption) OpenOption(java.nio.file.OpenOption) Storage(com.google.cloud.storage.Storage) BlobId(com.google.cloud.storage.BlobId) StorageException(com.google.cloud.storage.StorageException)

Aggregations

OpenOption (java.nio.file.OpenOption)49 StandardOpenOption (java.nio.file.StandardOpenOption)30 IOException (java.io.IOException)19 HashSet (java.util.HashSet)17 Test (org.junit.Test)17 Path (java.nio.file.Path)15 File (java.io.File)13 FileChannel (java.nio.channels.FileChannel)9 FileIO (org.apache.ignite.internal.processors.cache.persistence.file.FileIO)8 FileIODecorator (org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator)8 ByteBuffer (java.nio.ByteBuffer)7 SeekableByteChannel (java.nio.channels.SeekableByteChannel)7 NoSuchFileException (java.nio.file.NoSuchFileException)7 FileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory)7 RandomAccessFileIOFactory (org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory)7 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InputStream (java.io.InputStream)5 RandomAccessFile (java.io.RandomAccessFile)5 WritableByteChannel (java.nio.channels.WritableByteChannel)5