Search in sources :

Example 6 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method write.

/**
 * <p>
 * Writes bytes to a channel asynchronously.
 * </p>
 *
 * @param channel  channel which will be used to write bytes.
 * @param content  content which will be written.
 * @param offset   the offset which will be set to write bytes.
 * @param context  context of the native function call.
 * @param function callback function which should be called upon completion.
 */
public static void write(Channel channel, byte[] content, int offset, EventContext context, Function<EventResult, EventResult> function) {
    WriteBytesEvent writeBytesEvent = new WriteBytesEvent(channel, content, offset, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(writeBytesEvent);
    future.thenApply(function);
}
Also used : EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) WriteBytesEvent(org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)

Example 7 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult 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)

Example 8 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method read.

/**
 * Reads characters from the channel.
 *
 * @param characterChannel   channel the characters should be read.
 * @param numberOfCharacters the number of characters to read.
 * @param context            context of the event.
 * @param function           the callback function which will be triggered after reading characters.
 */
public static void read(CharacterChannel characterChannel, int numberOfCharacters, EventContext context, Function<EventResult, EventResult> function) {
    ReadCharactersEvent event = new ReadCharactersEvent(characterChannel, numberOfCharacters, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) ReadCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)

Example 9 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method read.

/**
 * <p>
 * Reads bytes asynchronously and trigger the callback.
 * </p>
 *
 * @param channel  the channel which the bytes should be read from.
 * @param content  the byte[] which will holds the content which will be read.
 * @param context  context which will be obtained from the native function call.
 * @param function the callback function which will be triggered.
 */
public static void read(Channel channel, byte[] content, EventContext context, Function<EventResult, EventResult> function) {
    ReadBytesEvent event = new ReadBytesEvent(channel, content, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : ReadBytesEvent(org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Example 10 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class IOUtils method close.

/**
 * Closes the channel asynchronously.
 *
 * @param byteChannel  channel which should be closed.
 * @param eventContext context of the event.
 * @param function     callback function which will be triggered.
 */
public static void close(Channel byteChannel, EventContext eventContext, Function<EventResult, EventResult> function) {
    CloseByteChannelEvent closeEvent = new CloseByteChannelEvent(byteChannel, eventContext);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(closeEvent);
    future.thenApply(function);
}
Also used : CloseByteChannelEvent(org.ballerinalang.nativeimpl.io.events.bytes.CloseByteChannelEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)17 ByteChannel (java.nio.channels.ByteChannel)4 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)4 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)4 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)4 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)4 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)4 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)2 ReadBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent)2 WriteBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)2 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)2 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)2 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)2 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 BStringArray (org.ballerinalang.model.values.BStringArray)1