Search in sources :

Example 26 with Channel

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

the class AsyncReadWriteTest method readContentValidationTest.

@Test(description = "Reads bytes and validate the byte content")
public void readContentValidationTest() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    byte[] content = new byte[3];
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] expected = "123".getBytes();
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = "456".getBytes();
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    content = new byte[3];
    expected = new byte[3];
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) 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) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 27 with Channel

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

the class BytesInputOutputBufferTest method getContentViaInputStream.

@Test(description = "Get content via InputStream")
public void getContentViaInputStream() 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();
}
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 28 with Channel

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

the class BytesInputOutputBufferTest method requestForExcessBytes.

@Test(description = "Request for bytes more exceeding the available limit")
public void requestForExcessBytes() throws IOException, URISyntaxException {
    int requestedLimit = 10;
    int expectedLimit = 6;
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
    Channel channel = new MockByteChannel(byteChannel);
    int numberOfBytes = read(requestedLimit, channel).getNumberOfBytesRead();
    channel.close();
    Assert.assertEquals(numberOfBytes, expectedLimit);
}
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 29 with Channel

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

the class BytesInputOutputBufferTest method writeBytesToFile.

@Test(description = "Write bytes to file")
public void writeBytesToFile() throws IOException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] bytes = "hello".getBytes();
    int numberOfBytesWritten = writeFull(bytes, bytes.length, channel);
    Assert.assertEquals(numberOfBytesWritten, bytes.length);
}
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 30 with Channel

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

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