use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class AsyncReadWriteTest method writeBytes.
@Test(description = "Write into a channel using async io framework")
public void writeBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
Channel channel = new MockByteChannel(byteChannel);
byte[] bytes = "hello".getBytes();
int numberOfBytesWritten = IOUtils.writeFull(channel, bytes, 0, new EventContext());
Assert.assertEquals(numberOfBytesWritten, bytes.length);
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class CloseDelimitedRecordChannel method execute.
/**
* <p>
* Closes a text record channel.
* </p>
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
DelimitedRecordChannel recordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.close(recordChannel, eventContext, CloseDelimitedRecordChannel::closeResponse);
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class HasNextTextRecord method response.
/**
* Responds whether a next record exists.
*
* @param result the result processed.
* @return result context.
*/
private static EventResult response(EventResult<Boolean, EventContext> result) {
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Boolean response = result.getResponse();
context.setReturnValues(new BBoolean(response));
callback.notifySuccess();
return result;
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class LoadToTable method response.
private static EventResult response(EventResult<List, EventContext> result) {
BStruct errorStruct;
BTable table;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
try {
List records = result.getResponse();
table = getbTable(context, records);
context.setReturnValues(table);
} catch (Throwable e) {
errorStruct = IOUtils.createError(context, e.getMessage());
context.setReturnValues(errorStruct);
}
}
CallableUnitCallback callback = eventContext.getCallback();
callback.notifySuccess();
return result;
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class LoadToTable method execute.
@Override
public void execute(Context context, CallableUnitCallback callback) {
final String filePath = context.getStringArgument(0);
Path path;
try {
path = Paths.get(filePath);
} catch (InvalidPathException e) {
String msg = "Unable to resolve the file path[" + filePath + "]: " + e.getMessage();
context.setReturnValues(IOUtils.createError(context, msg));
callback.notifySuccess();
return;
}
if (Files.notExists(path)) {
String msg = "Unable to find a file in given path: " + filePath;
context.setReturnValues(IOUtils.createError(context, msg));
callback.notifySuccess();
return;
}
try {
FileChannel sourceChannel = FileChannel.open(path, StandardOpenOption.READ);
// FileChannel will close once we completely read the content.
// Close will happen in DelimitedRecordReadAllEvent.
DelimitedRecordChannel recordChannel = getDelimitedRecordChannel(context, sourceChannel);
EventContext eventContext = new EventContext(context, callback);
DelimitedRecordReadAllEvent event = new DelimitedRecordReadAllEvent(recordChannel, eventContext);
CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
future.thenApply(LoadToTable::response);
} catch (IOException e) {
String msg = "Failed to process the delimited file: " + e.getMessage();
log.error(msg, e);
context.setReturnValues(IOUtils.createError(context, msg));
}
}
Aggregations