1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.file.transformers;
12
13 import org.mule.api.transformer.TransformerException;
14 import org.mule.util.IOUtils;
15
16 import java.io.File;
17 import java.io.InputStream;
18 import java.io.UnsupportedEncodingException;
19
20
21
22
23 public class FileToString extends FileToByteArray
24 {
25
26 public FileToString()
27 {
28 registerSourceType(File.class);
29 registerSourceType(InputStream.class);
30 registerSourceType(byte[].class);
31 setReturnClass(String.class);
32 }
33
34
35
36
37
38
39
40
41
42
43 public Object doTransform(Object src, String encoding) throws TransformerException
44 {
45 byte[] bytes;
46
47 if (src instanceof InputStream)
48 {
49 bytes = IOUtils.toByteArray((InputStream) src);
50 }
51 else if (src instanceof byte[])
52 {
53 bytes = (byte[])src;
54 }
55 else
56 {
57 bytes = (byte[])super.doTransform(src, encoding);
58 }
59
60 try
61 {
62 return new String(bytes, encoding);
63 }
64 catch (UnsupportedEncodingException uee)
65 {
66 throw new TransformerException(this, uee);
67 }
68 }
69
70 }