Search in sources :

Example 1 with WriteBytesEvent

use of org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent 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 2 with WriteBytesEvent

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

the class IOUtils method write.

/**
 * <p>
 * Writes bytes to a channel.
 * </p>
 * <p>
 * This will be a blocking call.
 * </p>
 *
 * @param channel channel which should be used to write bytes.
 * @param content content which should be written.
 * @param offset  offset which should be set when writing bytes.
 * @param context context obtained from the native function call.
 * @return the number of bytes written.
 * @throws InterruptedException if the thread is interrupted
 * @throws ExecutionException   error while execution.
 */
private static int write(Channel channel, byte[] content, int offset, EventContext context) throws InterruptedException, ExecutionException {
    WriteBytesEvent writeBytesEvent = new WriteBytesEvent(channel, content, offset, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(writeBytesEvent);
    EventResult eventResponse = future.get();
    offset = offset + (Integer) eventResponse.getResponse();
    Throwable error = ((EventContext) eventResponse.getContext()).getError();
    if (null != error) {
        throw new ExecutionException(error);
    }
    return offset;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) WriteBytesEvent(org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)2 WriteBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)2 ExecutionException (java.util.concurrent.ExecutionException)1 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)1