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);
}
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();
}
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);
}
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);
}
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 = "HelloÇ";
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();
}
Aggregations