use of org.ballerinalang.nativeimpl.io.channels.base.Channel 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.base.Channel in project ballerina by ballerina-lang.
the class Write method execute.
/**
* Writes bytes to a given channel.
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
byte[] content = context.getBlobArgument(CONTENT_INDEX);
int offset = (int) context.getIntArgument(START_OFFSET_INDEX);
Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.write(byteChannel, content, offset, eventContext, Write::writeResponse);
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class AbstractNativeChannel method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context) {
Channel channel = inFlow(context);
BStruct channelStruct = BLangConnectorSPIUtil.createBStruct(context, BYTE_CHANNEL_PACKAGE, STRUCT_TYPE);
channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, channel);
context.setReturnValues(channelStruct);
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel 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.base.Channel in project ballerina by ballerina-lang.
the class EntityBodyHandler method constructJsonDataSource.
/**
* Construct JsonDataSource from the underneath byte channel which is associated with the entity struct.
*
* @param entityStruct Represent an entity struct
* @return BJSON data source which is kept in memory
*/
public static BJSON constructJsonDataSource(BStruct entityStruct) {
try {
Channel byteChannel = getByteChannel(entityStruct);
if (byteChannel == null) {
return null;
}
BJSON jsonData = new BJSON(byteChannel.getInputStream());
byteChannel.close();
return jsonData;
} catch (IOException e) {
throw new BallerinaIOException("Error occurred while closing connection", e);
}
}
Aggregations