use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel in project ballerina by ballerina-lang.
the class CreateDelimitedRecordChannel method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context) {
try {
// File which holds access to the channel information
BStruct textRecordChannelInfo = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
String recordSeparator = context.getStringArgument(RECORD_SEPARATOR_INDEX);
String fieldSeparator = context.getStringArgument(FIELD_SEPARATOR_INDEX);
BStruct textRecordChannel = BLangConnectorSPIUtil.createBStruct(context, RECORD_CHANNEL_PACKAGE, STRUCT_TYPE);
// Will get the relevant byte channel and will create a character channel
CharacterChannel characterChannel = (CharacterChannel) textRecordChannelInfo.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
DelimitedRecordChannel bCharacterChannel = new DelimitedRecordChannel(characterChannel, recordSeparator, fieldSeparator);
textRecordChannel.addNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME, bCharacterChannel);
context.setReturnValues(textRecordChannel);
} catch (Throwable e) {
String message = "Error occurred while converting character channel to textRecord channel:" + e.getMessage();
log.error(message, e);
context.setReturnValues(IOUtils.createError(context, message));
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel in project ballerina by ballerina-lang.
the class ReadCharacters method execute.
/**
* <p>
* Reads characters from the channel.
* </p>
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
long numberOfCharacters = context.getIntArgument(NUMBER_OF_CHARS_INDEX);
CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.read(characterChannel, (int) numberOfCharacters, eventContext, ReadCharacters::readCharactersResponse);
}
use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel in project ballerina by ballerina-lang.
the class WriteCharacters method execute.
/**
* Writes characters to a given file.
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
String content = context.getStringArgument(CONTENT_INDEX);
long startOffset = context.getIntArgument(START_OFFSET_INDEX);
CharacterChannel characterChannel = (CharacterChannel) channel.getNativeData(IOConstants.CHARACTER_CHANNEL_NAME);
EventContext eventContext = new EventContext(context, callback);
IOUtils.write(characterChannel, content, (int) startOffset, eventContext, WriteCharacters::writeResponse);
}
use of org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel 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.channels.base.CharacterChannel 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();
}
Aggregations