use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.
the class IOUtils method close.
/**
* Closes the delimited record channel asynchronously.
*
* @param charChannel channel which should be closed.
* @param eventContext context of the event.
* @param function callback function which will be triggered.
*/
public static void close(DelimitedRecordChannel charChannel, EventContext eventContext, Function<EventResult, EventResult> function) {
CloseDelimitedRecordEvent closeEvent = new CloseDelimitedRecordEvent(charChannel, eventContext);
CompletableFuture<EventResult> future = EventManager.getInstance().publish(closeEvent);
future.thenApply(function);
}
use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.
the class IOUtils method write.
/**
* <p>
* Writes bytes to a channel.
* </p>
* <p>
* This will be a blocking call.
* </p>
*
* @param channel channel which should be used to write bytes.
* @param content content which should be written.
* @param offset offset which should be set when writing bytes.
* @param context context obtained from the native function call.
* @return the number of bytes written.
* @throws InterruptedException if the thread is interrupted
* @throws ExecutionException error while execution.
*/
private static int write(Channel channel, byte[] content, int offset, EventContext context) throws InterruptedException, ExecutionException {
WriteBytesEvent writeBytesEvent = new WriteBytesEvent(channel, content, offset, context);
CompletableFuture<EventResult> future = EventManager.getInstance().publish(writeBytesEvent);
EventResult eventResponse = future.get();
offset = offset + (Integer) eventResponse.getResponse();
Throwable error = ((EventContext) eventResponse.getContext()).getError();
if (null != error) {
throw new ExecutionException(error);
}
return offset;
}
use of org.ballerinalang.nativeimpl.io.events.EventResult 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 = "HelloNJ";
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();
}
use of org.ballerinalang.nativeimpl.io.events.EventResult 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.events.EventResult 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();
}
Aggregations