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