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);
}
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 = "HelloÇ";
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();
}
Aggregations