use of org.structr.files.ssh.filesystem.StructrFileChannel in project structr by structr.
the class StructrFilePath method newFileChannel.
@Override
public FileChannel newFileChannel(final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
AbstractFile actualFile = getActualFile();
FileChannel channel = null;
final boolean create = options.contains(StandardOpenOption.CREATE);
final boolean createNew = options.contains(StandardOpenOption.CREATE_NEW);
final boolean write = options.contains(StandardOpenOption.WRITE);
final boolean truncate = options.contains(StandardOpenOption.TRUNCATE_EXISTING);
final boolean append = options.contains(StandardOpenOption.APPEND);
if (write) {
try (final Tx tx = StructrApp.getInstance(fs.getSecurityContext()).tx()) {
// creation of a new file requested (=> create a new schema method)
if (create || createNew) {
// if CREATE_NEW, file must not exist, otherwise an error should be thrown
if (createNew && actualFile != null) {
throw new java.nio.file.FileAlreadyExistsException(toString());
}
// only create new file when it does not already exist
if (actualFile == null) {
try {
actualFile = createNewFile();
setParentFolder(actualFile);
} catch (FrameworkException fex) {
logger.warn("", fex);
}
}
}
if (actualFile != null && actualFile instanceof File) {
final File file = (File) actualFile;
channel = new StructrFileChannel(file.getOutputStream(true, !truncate || append));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("Unable to open file channel for writing of {}: {}", new Object[] { actualFile.getPath(), fex.getMessage() });
}
} else {
if (actualFile != null && actualFile instanceof File) {
try (final Tx tx = StructrApp.getInstance(fs.getSecurityContext()).tx()) {
channel = FileChannel.open(((File) actualFile).getFileOnDisk().toPath(), options);
tx.success();
} catch (FrameworkException fex) {
logger.warn("Unable to open file channel for reading of {}: {}", new Object[] { actualFile.getPath(), fex.getMessage() });
}
} else {
throw new FileNotFoundException("File " + actualFile.getPath() + " does not exist.");
}
}
return channel;
}
Aggregations