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));
}
}
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);
}
}
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);
}
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);
}
}
Aggregations