Search in sources :

Example 6 with CharacterChannel

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

the class CreateDelimitedRecordChannel method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context) {
    try {
        // File which holds access to the channel information
        BStruct textRecordChannelInfo = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
        String recordSeparator = context.getStringArgument(RECORD_SEPARATOR_INDEX);
        String fieldSeparator = context.getStringArgument(FIELD_SEPARATOR_INDEX);
        BStruct textRecordChannel = BLangConnectorSPIUtil.createBStruct(context, RECORD_CHANNEL_PACKAGE, STRUCT_TYPE);
        // Will get the relevant byte channel and will create a character channel
        CharacterChannel characterChannel = (CharacterChannel) textRecordChannelInfo.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
        DelimitedRecordChannel bCharacterChannel = new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
        textRecordChannel.addNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME, bCharacterChannel);
        context.setReturnValues(textRecordChannel);
    } catch (Throwable e) {
        String message = "Error occurred while converting character channel to textRecord channel:" + e.getMessage();
        log.error(message, e);
        context.setReturnValues(IOUtils.createError(context, message));
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 7 with CharacterChannel

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

the class ReadCharacters method execute.

/**
 * <p>
 * Reads characters from the channel.
 * </p>
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
    long numberOfCharacters = context.getIntArgument(NUMBER_OF_CHARS_INDEX);
    CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.read(characterChannel, (int) numberOfCharacters, eventContext, ReadCharacters::readCharactersResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 8 with CharacterChannel

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

the class WriteCharacters method execute.

/**
 * Writes characters to a given file.
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
    String content = context.getStringArgument(CONTENT_INDEX);
    long startOffset = context.getIntArgument(START_OFFSET_INDEX);
    CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.write(characterChannel, content, (int) startOffset, eventContext, WriteCharacters::writeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)

Example 9 with CharacterChannel

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

the class AsyncReadWriteTest method writeCharacters.

@Test(description = "Test writing characters through async io framework")
public void writeCharacters() throws IOException, ExecutionException, InterruptedException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    String text = "HelloNJ";
    int numberOfBytes = text.getBytes().length;
    WriteCharactersEvent event = new WriteCharactersEvent(characterChannel, text, 0);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    int numberOfCharactersWritten = (int) eventResult.getResponse();
    Assert.assertEquals(numberOfCharactersWritten, numberOfBytes);
    characterChannel.close();
}
Also used : WriteCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) 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) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 10 with CharacterChannel

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

the class AsyncReadWriteTest method readCharacters.

@Test(description = "Tests reading characters through the async io framework")
public void readCharacters() throws Exception {
    int numberOfCharactersToRead = 2;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/longChars.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    ReadCharactersEvent event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    Future<EventResult> future = eventManager.publish(event);
    EventResult eventResult = future.get();
    String content = (String) eventResult.getResponse();
    Assert.assertEquals("NJa", content);
    numberOfCharactersToRead = 3;
    event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    future = eventManager.publish(event);
    eventResult = future.get();
    content = (String) eventResult.getResponse();
    Assert.assertEquals("bcNJ", content);
    numberOfCharactersToRead = 4;
    event = new ReadCharactersEvent(characterChannel, numberOfCharactersToRead);
    future = eventManager.publish(event);
    eventResult = future.get();
    content = (String) eventResult.getResponse();
    Assert.assertEquals("ff", content);
    characterChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) ReadCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent) 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) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

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