Search in sources :

Example 1 with StandardOpenOption

use of java.nio.file.StandardOpenOption in project druid by druid-io.

the class LimitedTemporaryStorage method createFile.

/**
   * Create a new temporary file. All methods of the returned output stream may throw
   * {@link TemporaryStorageFullException} if the temporary storage area fills up.
   *
   * @return output stream to the file
   *
   * @throws TemporaryStorageFullException if the temporary storage area is full
   * @throws IOException                   if something goes wrong while creating the file
   */
public LimitedOutputStream createFile() throws IOException {
    if (bytesUsed.get() >= maxBytesUsed) {
        throw new TemporaryStorageFullException(maxBytesUsed);
    }
    synchronized (files) {
        if (closed) {
            throw new ISE("Closed");
        }
        FileUtils.forceMkdir(storageDirectory);
        final File theFile = new File(storageDirectory, String.format("%08d.tmp", files.size()));
        final EnumSet<StandardOpenOption> openOptions = EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
        final FileChannel channel = FileChannel.open(theFile.toPath(), openOptions);
        files.add(theFile);
        return new LimitedOutputStream(theFile, Channels.newOutputStream(channel));
    }
}
Also used : StandardOpenOption(java.nio.file.StandardOpenOption) FileChannel(java.nio.channels.FileChannel) ISE(io.druid.java.util.common.ISE) File(java.io.File)

Example 2 with StandardOpenOption

use of java.nio.file.StandardOpenOption in project helios by spotify.

the class X509CertificateFactory method saveToCache.

private static void saveToCache(final Path cacheDirectory, final Path cacheCertPath, final Path cacheKeyPath, final CertificateAndPrivateKey certificateAndPrivateKey) {
    try {
        Files.createDirectories(cacheDirectory);
        final String certPem = asPemString(certificateAndPrivateKey.getCertificate());
        final String keyPem = asPemString(certificateAndPrivateKey.getPrivateKey());
        // overwrite any existing file, and make sure it's only readable by the current user
        final Set<StandardOpenOption> options = ImmutableSet.of(CREATE, WRITE);
        final Set<PosixFilePermission> perms = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE);
        final FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms);
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheCertPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(certPem.getBytes()));
        }
        try (final SeekableByteChannel sbc = Files.newByteChannel(cacheKeyPath, options, attrs)) {
            sbc.write(ByteBuffer.wrap(keyPem.getBytes()));
        }
        log.debug("cached generated certificate to {}", cacheCertPath);
    } catch (IOException e) {
        // couldn't save to the cache, oh well
        log.warn("error caching generated certificate", e);
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 3 with StandardOpenOption

use of java.nio.file.StandardOpenOption 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 4 with StandardOpenOption

use of java.nio.file.StandardOpenOption 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

StandardOpenOption (java.nio.file.StandardOpenOption)4 OpenOption (java.nio.file.OpenOption)2 Acl (com.google.cloud.storage.Acl)1 BlobId (com.google.cloud.storage.BlobId)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 Storage (com.google.cloud.storage.Storage)1 StorageException (com.google.cloud.storage.StorageException)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ISE (io.druid.java.util.common.ISE)1 File (java.io.File)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 SeekableByteChannel (java.nio.channels.SeekableByteChannel)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1