Search in sources :

Example 11 with EventResult

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);
}
Also used : CloseDelimitedRecordEvent(org.ballerinalang.nativeimpl.io.events.records.CloseDelimitedRecordEvent) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult)

Example 12 with EventResult

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) WriteBytesEvent(org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with EventResult

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();
}
Also used : WriteCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 14 with EventResult

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) ReadCharactersEvent(org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 15 with EventResult

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();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordReadEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)17 ByteChannel (java.nio.channels.ByteChannel)4 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)4 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)4 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)4 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)4 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)4 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)2 ReadBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent)2 WriteBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)2 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)2 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)2 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)2 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 BStringArray (org.ballerinalang.model.values.BStringArray)1