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