Hi,
I’m trying to write a Custom Java Component that will read a file, decrypt it, then write the data to the output port which would then be connected to a standard FlatFileReader.
I’ve managed to get the component to read the file and decrypt it, which works as I can print the decrypted data onto the console. However I cannot work out how to write the data to the output port.
I have a byte array, but how do I write this to the output port? All I can find is write methods that take a DataRecord, but I don’t want to parse the data as the CSV will be better handled by the standard Clover components.
Can anyone help?
Thanks
John
OK, I see now how to do it:
DataRecord record = DataRecordFactory.newRecord(metadata);
record.getField(0).setValue(decrypted.toByteArray());
writeRecordToPort(0, record);
Where decrypted is a ByteArrayOutputStream where the decrypted data was written.
Hi,
The data you want to write to an output port has to have the form of a DataRecord. Every data flowing through a CloverETL graph has to be mapped to some metadata. This is the way you do it in a custom Java transformation. You don’t have to parse the data yourself, you can create a metadata containing only one field of byte type and map your whole byte array to it. This way you can get the array to an edge and parse it by other components. The code may look like this:
public void execute() {
DataRecord record = outRecords[0];
try (InputStream in = getInputStream("data-in/data2.txt")) {
record.getField("field1").setValue(IOUtils.toByteArray(in));
writeRecordToPort(0, record);
} catch (IOException e) {
throw new JetelRuntimeException("Problem with reading input file", e);
}
}
The code reads data from a file and the “IOUtils.toByteArray(in)” is just a substitution of a byte array.
Hope this helps.