Search in sources :

Example 11 with EventContext

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);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 12 with EventContext

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);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) BStringArray(org.ballerinalang.model.values.BStringArray) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 13 with EventContext

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 14 with EventContext

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 15 with EventContext

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) ReadBytesEvent(org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)27 BStruct (org.ballerinalang.model.values.BStruct)20 Context (org.ballerinalang.bre.Context)11 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)11 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)6 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 ByteChannel (java.nio.channels.ByteChannel)3 BInteger (org.ballerinalang.model.values.BInteger)3 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)3 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)3 Test (org.testng.annotations.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 List (java.util.List)1 BBlob (org.ballerinalang.model.values.BBlob)1