use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class CharacterInputOutputBufferTest method writeCharacters.
@Test(description = "Write characters to file")
public void writeCharacters() throws IOException {
// 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;
int numberOfBytesWritten = characterChannel.write(text, 0);
Assert.assertEquals(numberOfBytesWritten, numberOfBytes);
characterChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class CharacterInputOutputBufferTest method readCharacters.
@Test(description = "Read characters into multiple iterations")
public void readCharacters() throws IOException, URISyntaxException {
int numberOfCharactersToRead = 2;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/text/6charfile.txt");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
String readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals(readCharacters.length(), numberOfCharactersToRead);
numberOfCharactersToRead = 1;
readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals(readCharacters.length(), numberOfCharactersToRead);
numberOfCharactersToRead = 4;
readCharacters = characterChannel.read(numberOfCharactersToRead);
Assert.assertEquals(readCharacters.length(), 3);
characterChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class AsyncReadWriteTest method readTextRecords.
@Test(description = "Test which will read text records from a given channel using async io framework")
public void readTextRecords() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
int expectedFieldCount = 3;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
DelimitedRecordReadEvent event = new DelimitedRecordReadEvent(recordChannel);
Future<EventResult> future = eventManager.publish(event);
EventResult eventResult = future.get();
String[] readRecord = (String[]) eventResult.getResponse();
Assert.assertEquals(readRecord.length, expectedFieldCount);
event = new DelimitedRecordReadEvent(recordChannel);
future = eventManager.publish(event);
eventResult = future.get();
readRecord = (String[]) eventResult.getResponse();
Assert.assertEquals(readRecord.length, expectedFieldCount);
event = new DelimitedRecordReadEvent(recordChannel);
future = eventManager.publish(event);
eventResult = future.get();
readRecord = (String[]) eventResult.getResponse();
Assert.assertEquals(readRecord.length, expectedFieldCount);
event = new DelimitedRecordReadEvent(recordChannel);
future = eventManager.publish(event);
eventResult = future.get();
readRecord = (String[]) eventResult.getResponse();
Assert.assertEquals(readRecord.length, 0);
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class AsyncReadWriteTest method writeRecords.
@Test(description = "Test which will write records to a channel using async io framework")
public void writeRecords() throws IOException, ExecutionException, InterruptedException {
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "records.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
String[] recordOne = { "Foo", "Bar", "911" };
BStringArray recordOneArr = new BStringArray(recordOne);
DelimitedRecordWriteEvent recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordOneArr);
Future<EventResult> future = eventManager.publish(recordWriteEvent);
future.get();
String[] recordTwo = { "Jim", "Com", "119" };
BStringArray recordTwoArr = new BStringArray(recordTwo);
recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordTwoArr);
future = eventManager.publish(recordWriteEvent);
future.get();
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method processRecordSequence.
@Test(description = "Processors records in sequence with hasNext()")
public void processRecordSequence() throws IOException, URISyntaxException {
int expectedFieldCount = 3;
boolean hasNext = false;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
hasNext = recordChannel.hasNext();
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
Assert.assertTrue(hasNext);
hasNext = recordChannel.hasNext();
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
Assert.assertTrue(hasNext);
hasNext = recordChannel.hasNext();
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
Assert.assertTrue(hasNext);
hasNext = recordChannel.hasNext();
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 0);
Assert.assertFalse(hasNext);
recordChannel.close();
}
Aggregations