use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method readLongRecord.
@Test(description = "Read lengthy records")
public void readLongRecord() throws IOException, URISyntaxException {
int expectedFieldCount = 18;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample4.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method readRecords.
@Test(description = "Reads records from file")
public void readRecords() throws IOException, URISyntaxException {
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", ",");
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 0);
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class HasNextTextRecord method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(TXT_RECORD_CHANNEL_INDEX);
if (channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME) != null) {
DelimitedRecordChannel textRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
HasNextDelimitedRecordEvent hasNextEvent = new HasNextDelimitedRecordEvent(textRecordChannel, eventContext);
CompletableFuture<EventResult> event = EventManager.getInstance().publish(hasNextEvent);
event.thenApply(HasNextTextRecord::response);
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel in project ballerina by ballerina-lang.
the class LoadToTable method getDelimitedRecordChannel.
private DelimitedRecordChannel getDelimitedRecordChannel(Context context, FileChannel sourceChannel) {
final String recordSeparator = context.getStringArgument(1);
final String fieldSeparator = context.getStringArgument(2);
final String encoding = context.getStringArgument(3);
FileIOChannel fileIOChannel = new FileIOChannel(sourceChannel);
CharacterChannel characterChannel = new CharacterChannel(fileIOChannel, Charset.forName(encoding).name());
return new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
}
Aggregations