Search in sources :

Example 21 with CharacterChannel

use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel in project ballerina by ballerina-lang.

the class CreateCharacterChannel method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context) {
    BStruct byteChannelInfo;
    BStruct characterChannel;
    String encoding;
    try {
        // File which holds access to the channel information
        byteChannelInfo = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
        encoding = context.getStringArgument(ENCODING_INDEX);
        characterChannel = BLangConnectorSPIUtil.createBStruct(context, CHAR_CHANNEL_PACKAGE, STRUCT_TYPE);
        Channel byteChannel = (Channel) byteChannelInfo.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
        CharacterChannel bCharacterChannel = new CharacterChannel(byteChannel, encoding);
        characterChannel.addNativeData(IOConstants.CHARACTER_CHANNEL_NAME, bCharacterChannel);
        context.setReturnValues(characterChannel);
    } catch (Throwable e) {
        String message = "Error occurred while converting byte channel to character channel:" + e.getMessage();
        log.error(message, e);
        context.setReturnValues(IOUtils.createError(context, message));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 22 with CharacterChannel

use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel 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

CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)22 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)17 ByteChannel (java.nio.channels.ByteChannel)16 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)16 Test (org.testng.annotations.Test)16 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)9 BStruct (org.ballerinalang.model.values.BStruct)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)3 BStringArray (org.ballerinalang.model.values.BStringArray)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 BString (org.ballerinalang.model.values.BString)1 FileIOChannel (org.ballerinalang.nativeimpl.io.channels.FileIOChannel)1 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)1 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)1 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)1 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)1