Search in sources :

Example 11 with Channel

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

the class BytesInputOutputBufferTest method multiReadFile.

@Test(description = "Reads files into multiple iterations")
public void multiReadFile() throws IOException, URISyntaxException {
    int initialReadLimit = 3;
    int secondLapReadLimit = 3;
    int thirdLapReadLimit = 3;
    // During the 3rd lap all the bytes were get
    int thirdLapReadLimitExpected = 0;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] readBytes = read(initialReadLimit, channel).getContent();
    // This should hold the number of bytes get
    Assert.assertEquals(readBytes.length, initialReadLimit);
    readBytes = read(secondLapReadLimit, channel).getContent();
    // This should hold the number of bytes get
    Assert.assertEquals(readBytes.length, secondLapReadLimit);
    ReadByteResult result = read(thirdLapReadLimit, channel);
    int numberOfReadBytes = result.getNumberOfBytesRead();
    channel.close();
    // This should hold the number of bytes get
    Assert.assertEquals(numberOfReadBytes, thirdLapReadLimitExpected);
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 12 with Channel

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

the class CharacterInputOutputBufferTest method readCorruptedCharacters.

@Test(description = "Read corrupted characters from text")
public void readCorruptedCharacters() throws IOException, URISyntaxException {
    int numberOfCharactersToRead;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/corruptedText");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    numberOfCharactersToRead = 2;
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("�T", readCharacters);
    numberOfCharactersToRead = 11;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("est Curr ß", readCharacters);
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(" �", readCharacters);
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "");
    characterChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) 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 13 with Channel

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

the class CharacterInputOutputBufferTest method readCorruptedCharactersIntoMultipleChannelReads.

@Test(description = "Read from file which has all corrupted chars")
public void readCorruptedCharactersIntoMultipleChannelReads() throws IOException, URISyntaxException {
    int numberOfCharactersToRead;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/corruptedText2");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    numberOfCharactersToRead = 3;
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("���", readCharacters);
    numberOfCharactersToRead = 3;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("��a", readCharacters);
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "");
    characterChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) 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 14 with Channel

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

the class RecordInputOutputTest method writeRecords.

@Test(description = "Writes records to channel")
public void writeRecords() throws IOException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "records.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    String[] recordOne = { "Foo", "Bar", "911" };
    BStringArray recordOneArr = new BStringArray(recordOne);
    recordChannel.write(recordOneArr);
    String[] recordTwo = { "Jim", "Com", "119" };
    BStringArray recordTwoArr = new BStringArray(recordTwo);
    recordChannel.write(recordTwoArr);
    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) BStringArray(org.ballerinalang.model.values.BStringArray) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 15 with Channel

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

the class RecordInputOutputTest method readNonIndentedRecords.

@Test(description = "Read records which are not indented properly")
public void readNonIndentedRecords() throws IOException, URISyntaxException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample2.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, 9);
    Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
    // This will be a blank record
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 1);
    Assert.assertTrue(recordChannel.hasNext(), "Expecting more records but received as EOL.");
    readRecord = recordChannel.read();
    Assert.assertEquals(readRecord.length, 9);
    Assert.assertFalse(recordChannel.hasNext(), "Last record received, but indicate as more records available.");
    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)

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