use of org.ballerinalang.nativeimpl.io.channels.FileIOChannel in project ballerina by ballerina-lang.
the class OpenFile method inFlow.
/**
* {@inheritDoc}
*/
@Override
public Channel inFlow(Context context) throws BallerinaException {
String pathUrl = context.getStringArgument(PATH_FIELD_INDEX);
String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
Path path = null;
Channel channel;
try {
String accessLC = accessMode.toLowerCase(Locale.getDefault());
path = Paths.get(pathUrl);
Set<OpenOption> opts = new HashSet<>();
if (accessLC.contains("r")) {
if (!Files.exists(path)) {
throw new BallerinaException("file not found: " + path);
}
if (!Files.isReadable(path)) {
throw new BallerinaException("file is not readable: " + path);
}
opts.add(StandardOpenOption.READ);
}
boolean write = accessLC.contains("w");
boolean append = accessLC.contains("a");
if (write || append) {
if (Files.exists(path) && !Files.isWritable(path)) {
throw new BallerinaException("file is not writable: " + path);
}
createDirs(path);
opts.add(StandardOpenOption.CREATE);
if (append) {
opts.add(StandardOpenOption.APPEND);
} else {
opts.add(StandardOpenOption.WRITE);
}
}
FileChannel byteChannel = FileChannel.open(path, opts);
// channel = new FileIOChannel(byteChannel, IOConstants.CHANNEL_BUFFER_SIZE);
channel = new FileIOChannel(byteChannel);
} catch (AccessDeniedException e) {
throw new BallerinaException("Do not have access to write file: " + path, e);
} catch (Throwable e) {
throw new BallerinaException("failed to open file: " + e.getMessage(), e);
}
return channel;
}
use of org.ballerinalang.nativeimpl.io.channels.FileIOChannel in project ballerina by ballerina-lang.
the class OpenChannel method inFlow.
/**
* {@inheritDoc}
*/
@Override
public Channel inFlow(Context context) throws BallerinaException {
BStruct fileStruct = (BStruct) context.getRefArgument(FILE_CHANNEL_INDEX);
String accessMode = context.getStringArgument(FILE_ACCESS_MODE_INDEX);
Path path = null;
Channel channel;
try {
String accessLC = accessMode.toLowerCase(Locale.getDefault());
path = Paths.get(fileStruct.getStringField(PATH_FIELD_INDEX));
Set<OpenOption> opts = new HashSet<>();
if (accessLC.contains("r")) {
if (!Files.exists(path)) {
throw new BallerinaException("file not found: " + path);
}
if (!Files.isReadable(path)) {
throw new BallerinaException("file is not readable: " + path);
}
opts.add(StandardOpenOption.READ);
}
boolean write = accessLC.contains("w");
boolean append = accessLC.contains("a");
if (write || append) {
if (Files.exists(path) && !Files.isWritable(path)) {
throw new BallerinaException("file is not writable: " + path);
}
createDirs(path);
opts.add(StandardOpenOption.CREATE);
if (append) {
opts.add(StandardOpenOption.APPEND);
} else {
opts.add(StandardOpenOption.WRITE);
}
}
FileChannel byteChannel = FileChannel.open(path, opts);
channel = new FileIOChannel(byteChannel);
} catch (AccessDeniedException e) {
throw new BallerinaException("Do not have access to write file: " + path, e);
} catch (Throwable e) {
throw new BallerinaException("failed to open file: " + e.getMessage(), e);
}
return channel;
}
use of org.ballerinalang.nativeimpl.io.channels.FileIOChannel in project ballerina by ballerina-lang.
the class LoadToTable method getDelimitedRecordChannel.
private DelimitedRecordChannel getDelimitedRecordChannel(Context context, FileChannel sourceChannel) {
final String recordSeparator = context.getStringArgument(1);
final String fieldSeparator = context.getStringArgument(2);
final String encoding = context.getStringArgument(3);
FileIOChannel fileIOChannel = new FileIOChannel(sourceChannel);
CharacterChannel characterChannel = new CharacterChannel(fileIOChannel, Charset.forName(encoding).name());
return new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
}
Aggregations