Search in sources :

Example 11 with DelimitedRecordChannel

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);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) 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) 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)

Example 12 with DelimitedRecordChannel

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) 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) 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)

Example 13 with DelimitedRecordChannel

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);
    }
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) HasNextDelimitedRecordEvent(org.ballerinalang.nativeimpl.io.events.records.HasNextDelimitedRecordEvent) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 14 with DelimitedRecordChannel

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);
}
Also used : FileIOChannel(org.ballerinalang.nativeimpl.io.channels.FileIOChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)14 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)9 ByteChannel (java.nio.channels.ByteChannel)7 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)7 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)7 Test (org.testng.annotations.Test)7 BStruct (org.ballerinalang.model.values.BStruct)5 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 BStringArray (org.ballerinalang.model.values.BStringArray)3 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 FileIOChannel (org.ballerinalang.nativeimpl.io.channels.FileIOChannel)1 DelimitedRecordReadAllEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent)1 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)1 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)1 HasNextDelimitedRecordEvent (org.ballerinalang.nativeimpl.io.events.records.HasNextDelimitedRecordEvent)1