Procedure
- In the Repository tree view, expand Code to display the Routines folder.
- Right-click Routines and select Create routine.
- The New routine dialog box opens. Enter its name, in this example MyRoutineDemo, a purpose and a description.
-
Click Finish to close the dialog box.
Note: The newly created routine appears in the Repository tree view, directly below the Routines node.
The routine editor opens to reveal a model routine which contains a simple example, by default, comprising descriptive text in blue, followed by the corresponding code.
-
Replace the model with the following code and press
Ctrl+S to save the routine.
It has a ByteArrayFromFile function, that requires a file path as input parameter and is used to read a file and convert it to a byte array.
package routines;public class MyRoutineDemo { public static byte[] ByteArrayFromFile(String filepath) { try{ java.io.File file=new java.io.File(filepath); java.io.FileInputStream fis = new java.io.FileInputStream(file); int fileLength = (int) file.length(); byte[] incoming_file_data = new byte[fileLength]; // allocate byte array of right size fis.read(incoming_file_data, 0, fileLength ); // read into byte array fis.close(); return incoming_file_data; }catch(Exception err){ err.printStackTrace();return null; } } }
For more information on how to create user routines, see the Talend User Guide.