Search in sources :

Example 1 with WriteCharactersEvent

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

the class IOUtils method write.

/**
 * Writes characters to a channel.
 *
 * @param characterChannel the channel the characters will be written
 * @param content          the content which will be written.
 * @param offset           if an offset should be specified while writing.
 * @param context          context of the event.
 * @param function         callback function which should be triggered
 */
public static void write(CharacterChannel characterChannel, String content, int offset, EventContext context, Function<EventResult, EventResult> function) {
    WriteCharactersEvent event = new WriteCharactersEvent(characterChannel, content, offset, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : WriteCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Example 2 with WriteCharactersEvent

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

the class AsyncReadWriteTest method writeCharacters.

@Test(description = "Test writing characters through async io framework")
public void writeCharacters() throws IOException, ExecutionException, InterruptedException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    String text = "HelloNJ";
    int numberOfBytes = text.getBytes().length;
    WriteCharactersEvent event = new WriteCharactersEvent(characterChannel, text, 0);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    int numberOfCharactersWritten = (int) eventResult.getResponse();
    Assert.assertEquals(numberOfCharactersWritten, numberOfBytes);
    characterChannel.close();
}
Also used : WriteCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)2 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)2 ByteChannel (java.nio.channels.ByteChannel)1 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)1 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)1 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)1 Test (org.testng.annotations.Test)1