Search in sources :

Example 6 with Channel

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

the class BytesInputOutputBufferTest method fixedBufferIterativeRead.

@Test(description = "Reads bytes which has a fixed buffer for multiple read iterations")
public void fixedBufferIterativeRead() throws IOException, URISyntaxException {
    final int numberOfBytesInFile = 7;
    final int fixedBufferSize = 15;
    int readByteLength = -1;
    int totalNumberOfBytesRead = 0;
    ByteBuffer content = ByteBuffer.allocate(fixedBufferSize);
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/sequenceOfChars");
    Channel channel = new MockByteChannel(byteChannel);
    while (readByteLength != 0) {
        ReadByteResult readByteResult = read(3, channel);
        byte[] readBytes = readByteResult.getContent();
        content.put(readBytes);
        readByteLength = readByteResult.getNumberOfBytesRead();
        totalNumberOfBytesRead = totalNumberOfBytesRead + readByteLength;
    }
    content.flip();
    Assert.assertEquals(totalNumberOfBytesRead, numberOfBytesInFile);
}
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) ByteBuffer(java.nio.ByteBuffer) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 7 with Channel

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

the class BytesInputOutputBufferTest method varyingBufferSizeTest.

@Test(description = "Reads file which has varying buffer sizes")
public void varyingBufferSizeTest() throws IOException, URISyntaxException {
    final int numberOfBytesInFile = 7;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/sequenceOfChars");
    Channel channel = new MockByteChannel(byteChannel);
    int numberOfBytesRead = read(8, channel).getNumberOfBytesRead();
    Assert.assertEquals(numberOfBytesRead, numberOfBytesInFile);
}
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 8 with Channel

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

the class BytesInputOutputBufferTest method multiReadFromFixedBuffer.

@Test(description = "Read bytes from fix buffer into multiple reads")
public void multiReadFromFixedBuffer() throws IOException, URISyntaxException {
    int initialReadLimit = 3;
    int secondLapReadLimit = 3;
    int thirdLapReadLimit = 3;
    // During the 3rd lap all the bytes were read
    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);
    int numberOfBytesRead = read(initialReadLimit, channel).getNumberOfBytesRead();
    // This should hold the number of bytes read
    Assert.assertEquals(numberOfBytesRead, initialReadLimit);
    numberOfBytesRead = read(secondLapReadLimit, channel).getNumberOfBytesRead();
    // This should hold the number of bytes get
    Assert.assertEquals(numberOfBytesRead, secondLapReadLimit);
    numberOfBytesRead = read(thirdLapReadLimit, channel).getNumberOfBytesRead();
    channel.close();
    // This should hold the number of bytes get
    Assert.assertEquals(numberOfBytesRead, 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 9 with Channel

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

the class BytesInputOutputBufferTest method checkChannelCloseStatus.

@Test(description = "Validate getting InputStream from closed channel", expectedExceptions = BallerinaIOException.class, expectedExceptionsMessageRegExp = "Channel is already closed.")
public void checkChannelCloseStatus() throws IOException, URISyntaxException {
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel, 0);
    final InputStream inputStream = channel.getInputStream();
    try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
        final String result = buffer.lines().collect(Collectors.joining("\n"));
        Assert.assertEquals(result, "123456", "Did not get expected string output.");
    }
    inputStream.close();
    channel.close();
    channel.getInputStream();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) BufferedReader(java.io.BufferedReader) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 10 with Channel

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

the class BytesInputOutputBufferTest method fileStreamCopyTest.

@Test(description = "Copy I/O byte file as a stream")
public void fileStreamCopyTest() throws IOException, URISyntaxException {
    final int readLimit = 10000;
    int totalNumberOfBytesRead = 0;
    int totalNumberOfBytesWritten = 0;
    final int numberOfBytesInFile = 45613;
    int numberOfBytesRead = 38938;
    int numberOfBytesWritten;
    // Number of characters in this file would be 6
    ByteChannel readByteChannel = TestUtil.openForReading("datafiles/io/images/ballerina.png");
    ByteChannel writeByteChannel = TestUtil.openForWriting(currentDirectoryPath + "ballerinaCopy.png");
    Channel readChannel = new MockByteChannel(readByteChannel);
    Channel writeChannel = new MockByteChannel(writeByteChannel);
    while (numberOfBytesRead > 0) {
        ReadByteResult readByteResult = read(readLimit, readChannel);
        byte[] readBytes = readByteResult.getContent();
        numberOfBytesRead = readByteResult.getNumberOfBytesRead();
        numberOfBytesWritten = writeFull(readBytes, numberOfBytesRead, writeChannel);
        totalNumberOfBytesRead = totalNumberOfBytesRead + numberOfBytesRead;
        totalNumberOfBytesWritten = totalNumberOfBytesWritten + numberOfBytesWritten;
    }
    Assert.assertEquals(totalNumberOfBytesRead, numberOfBytesInFile);
    Assert.assertEquals(totalNumberOfBytesRead, totalNumberOfBytesWritten);
}
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)

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