Search in sources :

Example 41 with Channel

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

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

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

the class Close method execute.

/**
 * Closes the byte channel.
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
    Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.close(byteChannel, eventContext, Close::closeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 44 with Channel

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

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

the class Read method execute.

/**
 * <p>
 * Reads bytes from a given channel.
 * </p>
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
    int nBytes = (int) context.getIntArgument(NUMBER_OF_BYTES_INDEX);
    int arraySize = nBytes <= 0 ? IOConstants.CHANNEL_BUFFER_SIZE : nBytes;
    Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
    byte[] content = new byte[arraySize];
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.read(byteChannel, content, eventContext, Read::readResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Aggregations

Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)49 ByteChannel (java.nio.channels.ByteChannel)33 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)31 Test (org.testng.annotations.Test)31 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)19 BStruct (org.ballerinalang.model.values.BStruct)12 IOException (java.io.IOException)7 FileChannel (java.nio.channels.FileChannel)7 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)7 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)6 TempFileIOChannel (org.ballerinalang.nativeimpl.io.channels.TempFileIOChannel)5 InputStream (java.io.InputStream)4 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 BallerinaIOException (org.ballerinalang.nativeimpl.io.BallerinaIOException)3 BufferedReader (java.io.BufferedReader)2 BufferedWriter (java.io.BufferedWriter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 InputStreamReader (java.io.InputStreamReader)2