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