Search in sources :

Example 1 with DelimitedRecordReadEvent

use of org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent in project ballerina by ballerina-lang.

the class IOUtils method read.

/**
 * Reads delimited records asynchronously.
 *
 * @param recordChannel channel the bytes should be read from.
 * @param context       event context.
 * @param function      callback function which will be triggered.
 */
public static void read(DelimitedRecordChannel recordChannel, EventContext context, Function<EventResult, EventResult> function) {
    DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel, context);
    CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
    future.thenApply(function);
}
Also used : EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)

Example 2 with DelimitedRecordReadEvent

use of org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method readTextRecords.

@Test(description = "Test which will read text records from a given channel using async io framework")
public void readTextRecords() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    int expectedFieldCount = 3;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    String[] readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, expectedFieldCount);
    event = new DelimitedRecordReadEvent(recordChannel);
    future = eventManager.publish(event);
    eventResult = future.get();
    readRecord = (String[]) eventResult.getResponse();
    Assert.assertEquals(readRecord.length, 0);
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) 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) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)2 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)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 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)1 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)1 Test (org.testng.annotations.Test)1