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();
}
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();
}
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);
}
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;
}
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();
}
Aggregations