Search in sources :

Example 31 with Channel

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

Example 32 with Channel

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

the class CharacterInputOutputBufferTest method readLongCharacters.

@Test(description = "Reads characters which are represented with long bytes")
public void readLongCharacters() throws IOException, URISyntaxException {
    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());
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("NJa", readCharacters);
    numberOfCharactersToRead = 3;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("bcNJ", readCharacters);
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals("ff", 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 33 with Channel

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

the class CloseSocket method execute.

@Override
public void execute(Context context) {
    BStruct socket;
    try {
        socket = (BStruct) context.getRefArgument(0);
        ByteChannel byteChannel = (ByteChannel) socket.getNativeData(IOConstants.CLIENT_SOCKET_NAME);
        BStruct byteChannelStruct = (BStruct) socket.getRefField(0);
        Channel channel = (Channel) byteChannelStruct.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
        byteChannel.close();
        channel.close();
    } catch (Throwable e) {
        String message = "Failed to close the socket:" + e.getMessage();
        log.error(message, e);
        context.setReturnValues(IOUtils.createError(context, message));
    }
    context.setReturnValues((BValue) null);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) ByteChannel(java.nio.channels.ByteChannel)

Example 34 with Channel

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

the class OpenSecureSocket method createReturnStruct.

private BStruct createReturnStruct(Context context, SSLSocket sslSocket, ByteChannel channel) throws IOException {
    PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
    // Create ByteChannel Struct
    StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
    Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
    BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
    channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
    // Create Socket Struct
    StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
    BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
    socketStruct.setRefField(0, channelStruct);
    socketStruct.setIntField(0, sslSocket.getPort());
    socketStruct.setIntField(1, sslSocket.getLocalPort());
    socketStruct.setStringField(0, sslSocket.getInetAddress().getHostAddress());
    socketStruct.setStringField(1, sslSocket.getLocalAddress().getHostAddress());
    socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
    return socketStruct;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) ByteChannel(java.nio.channels.ByteChannel) SocketIOChannel(org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)

Example 35 with Channel

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

the class CharacterInputOutputBufferTest method readFussyCharacters.

@Test(description = "Read characters which does not fit to the fixed buffer limit")
public void readFussyCharacters() throws IOException, URISyntaxException {
    int numberOfCharactersToRead = 2;
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/utf8file.txt");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    String readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "aa");
    numberOfCharactersToRead = 4;
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters, "abbNJ");
    readCharacters = characterChannel.read(numberOfCharactersToRead);
    Assert.assertEquals(readCharacters.length(), 0);
    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)

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