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