Search in sources :

Example 1 with ReadCharactersEvent

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

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

the class AsyncReadWriteTest method readCharacters.

@Test(description = "Tests reading characters through the async io framework")
public void readCharacters() throws Exception {
    int numberOfCharactersToRead = 2;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/longChars.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    ReadCharactersEvent event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    String content = (String) eventResult.getResponse();
    Assert.assertEquals("NJa", content);
    numberOfCharactersToRead = 3;
    event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    future = eventManager.publish(event);
    eventResult = future.get();
    content = (String) eventResult.getResponse();
    Assert.assertEquals("bcNJ", content);
    numberOfCharactersToRead = 4;
    event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    future = eventManager.publish(event);
    eventResult = future.get();
    content = (String) eventResult.getResponse();
    Assert.assertEquals("ff", content);
    characterChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) ReadCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent) 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 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)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