package routines; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.sql.Blob; /* * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but * it must be before the "{talendTypes}" key. * * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character, * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short | * Short * * 3. {Category} define a category for the Function. it is required. its value is user-defined . * * 4. {param} 's format is: {param} [()] [ : ] * * 's value should be one of: string, int, list, double, object, boolean, long, char, date. 's value is the * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't * added. you can have many parameters for the Function. * * 5. {example} gives a example for the Function. it is optional. */ public class CustomFile { /** * fileToByteArray: construction d'un tableau de byte à partir d'un fichier, pour insertion dans un varbinary (SQL Server) * * * {talendTypes} byte[] * * {Category} CustomFile * * {param} string("filepath") input: Le chemin absolu du fichier dont on veut récupérer le contenu * * {example} helloExemple("C:\test.pdf"). */ public static byte[] fileToByteArray(String filepath) { byte[] b=null; try { FileInputStream fis = new FileInputStream(filepath); b=new byte[fis.available()]; fis.read(b); } catch (Exception e) { e.printStackTrace(); } return b; } /** * blobToFile: construction d'un fichier à partir de données binaires en base de données * (testé avec varbinary sur SQL Server) * * * {talendTypes} void * * {Category} CustomFile * * {param} string("input") input: les données binaires * {param} string("filepath") input: Le chemin absolu du fichier que l'on veut créer * * {example} blobToFile(blob, "C:\test.pdf"). */ public static void blobToFile(Object input, String filepath) { try { Blob blob = (Blob) input; File fp = new File(filepath); FileOutputStream fos = new FileOutputStream(fp); fos.write(blob.getBytes(1, (int) blob.length())); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }