use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class Read method execute.
/**
* <p>
* Reads bytes from a given channel.
* </p>
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
int nBytes = (int) context.getIntArgument(NUMBER_OF_BYTES_INDEX);
int arraySize = nBytes <= 0 ? IOConstants.CHANNEL_BUFFER_SIZE : nBytes;
Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
byte[] content = new byte[arraySize];
EventContext eventContext = new EventContext(context, callback);
IOUtils.read(byteChannel, content, eventContext, Read::readResponse);
}
use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.
the class ReadCharacters method readCharactersResponse.
/*
* Callback method of the read characters response.
*
* @param result the result returned as the response.
* @return the processed event result.
*/
private static EventResult readCharactersResponse(EventResult<String, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
String readChars = result.getResponse();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
context.setReturnValues(new BString(readChars));
}
callback.notifySuccess();
return result;
}
Aggregations