Search in sources :

Example 16 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext 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 17 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method readBytes.

@Test(description = "Read into fixed byte[] using async io framework")
public void readBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    byte[] content = new byte[2];
    // 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 = { 49, 50 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 51, 52 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    expected = new byte[] { 53, 54 };
    IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(expected, content);
    int expectedNumberOfBytes = 0;
    content = new byte[2];
    expected = new byte[] { 0, 0 };
    int numberOfBytesRead = IOUtils.readFull(channel, content, new EventContext());
    Assert.assertEquals(numberOfBytesRead, expectedNumberOfBytes);
    Assert.assertEquals(expected, content);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 18 with EventContext

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

Example 19 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class Close method execute.

/**
 * Closes the byte channel.
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
    Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.close(byteChannel, eventContext, Close::closeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel)

Example 20 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class Close method closeResponse.

private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
    BStruct errorStruct = null;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    CallableUnitCallback callback = eventContext.getCallback();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
    }
    context.setReturnValues(errorStruct);
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)27 BStruct (org.ballerinalang.model.values.BStruct)20 Context (org.ballerinalang.bre.Context)11 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)11 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)6 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 ByteChannel (java.nio.channels.ByteChannel)3 BInteger (org.ballerinalang.model.values.BInteger)3 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)3 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)3 Test (org.testng.annotations.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 List (java.util.List)1 BBlob (org.ballerinalang.model.values.BBlob)1