use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class WriteCharacters method execute.
/**
* Writes characters to a given file.
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
String content = context.getStringArgument(CONTENT_INDEX);
long startOffset = context.getIntArgument(START_OFFSET_INDEX);
CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.write(characterChannel, content, (int) startOffset, eventContext, WriteCharacters::writeResponse);
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class WriteTextRecord method execute.
/**
* Writes records to a given file.
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
BStringArray content = (BStringArray) context.getRefArgument(CONTENT_INDEX);
DelimitedRecordChannel delimitedRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.write(delimitedRecordChannel, content, eventContext, WriteTextRecord::writeResponse);
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class WriteTextRecord method writeResponse.
/**
* Callback response received after the bytes are written.
*
* @param result the response received.
* @return the result context.
*/
private static EventResult writeResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class CloseCharacterChannel method closeResponse.
private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class IOUtils method read.
/**
* <p>
* Reads bytes from a channel and will obtain the response.
* </p>
* <p>
* This operation will be blocking.
* </p>
*
* @param channel channel the bytes should be read from.
* @param content byte [] which will hold the content which is read.
* @param context context obtained from the native function.
* @return the number of bytes read.
* @throws InterruptedException errors which occur if the thread is interrupted.
* @throws ExecutionException errors which occur during execution.
*/
private static int read(Channel channel, byte[] content, EventContext context) throws InterruptedException, ExecutionException {
ReadBytesEvent event = new ReadBytesEvent(channel, content, context);
CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
EventResult eventResponse = future.get();
int numberOfBytesRead = (Integer) eventResponse.getResponse();
Throwable error = ((EventContext) eventResponse.getContext()).getError();
if (null != error) {
throw new ExecutionException(error);
}
return numberOfBytesRead;
}
Aggregations