use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method readLongRecord.
@Test(description = "Read lengthy records")
public void readLongRecord() throws IOException, URISyntaxException {
int expectedFieldCount = 18;
// Number of characters in this file would be 6
ByteChannel byteChannel = TestUtil.openForReading("datafiles/io/records/sample4.csv");
Channel channel = new MockByteChannel(byteChannel);
CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class RecordInputOutputTest method readRecords.
@Test(description = "Reads records from file")
public void readRecords() throws IOException, URISyntaxException {
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", ",");
String[] readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, expectedFieldCount);
readRecord = recordChannel.read();
Assert.assertEquals(readRecord.length, 0);
recordChannel.close();
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel 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.channels.base.Channel in project ballerina by ballerina-lang.
the class CreateCharacterChannel method execute.
/**
* {@inheritDoc}
*/
@Override
public void execute(Context context) {
BStruct byteChannelInfo;
BStruct characterChannel;
String encoding;
try {
// File which holds access to the channel information
byteChannelInfo = (BStruct) context.getRefArgument(CHAR_CHANNEL_INDEX);
encoding = context.getStringArgument(ENCODING_INDEX);
characterChannel = BLangConnectorSPIUtil.createBStruct(context, CHAR_CHANNEL_PACKAGE, STRUCT_TYPE);
Channel byteChannel = (Channel) byteChannelInfo.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
CharacterChannel bCharacterChannel = new CharacterChannel(byteChannel, encoding);
characterChannel.addNativeData(IOConstants.CHARACTER_CHANNEL_NAME, bCharacterChannel);
context.setReturnValues(characterChannel);
} catch (Throwable e) {
String message = "Error occurred while converting byte channel to character channel:" + e.getMessage();
log.error(message, e);
context.setReturnValues(IOUtils.createError(context, message));
}
}
use of org.ballerinalang.nativeimpl.io.channels.base.Channel in project ballerina by ballerina-lang.
the class Read method execute.
/**
* <p>
* Reads bytes from a given channel.
* </p>
* <p>
* {@inheritDoc}
*/
@Override
public void execute(Context context, CallableUnitCallback callback) {
BStruct channel = (BStruct) context.getRefArgument(BYTE_CHANNEL_INDEX);
int nBytes = (int) context.getIntArgument(NUMBER_OF_BYTES_INDEX);
int arraySize = nBytes <= 0 ? IOConstants.CHANNEL_BUFFER_SIZE : nBytes;
Channel byteChannel = (Channel) channel.getNativeData(IOConstants.BYTE_CHANNEL_NAME);
byte[] content = new byte[arraySize];
EventContext eventContext = new EventContext(context, callback);
IOUtils.read(byteChannel, content, eventContext, Read::readResponse);
}
Aggregations