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);
}
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;
}
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);
}
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);
}
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);
}
Aggregations