1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformer.simple; |
12 | |
|
13 | |
import org.mule.api.transformer.TransformerException; |
14 | |
import org.mule.util.IOUtils; |
15 | |
|
16 | |
import java.io.ByteArrayOutputStream; |
17 | |
import java.io.InputStream; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class ObjectToByteArray extends SerializableToByteArray |
25 | |
{ |
26 | |
|
27 | |
public ObjectToByteArray() |
28 | 1182 | { |
29 | 1182 | this.registerSourceType(InputStream.class); |
30 | 1182 | this.registerSourceType(String.class); |
31 | 1182 | setReturnClass(byte[].class); |
32 | 1182 | } |
33 | |
|
34 | |
|
35 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
36 | |
{ |
37 | |
try |
38 | |
{ |
39 | 28 | if (src instanceof String) |
40 | |
{ |
41 | 16 | return src.toString().getBytes(encoding); |
42 | |
} |
43 | 12 | else if (src instanceof InputStream) |
44 | |
{ |
45 | 0 | InputStream is = (InputStream) src; |
46 | 0 | ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); |
47 | |
try |
48 | |
{ |
49 | 0 | IOUtils.copyLarge(is, byteOut); |
50 | |
} |
51 | |
finally |
52 | |
{ |
53 | 0 | is.close(); |
54 | 0 | } |
55 | 0 | return byteOut.toByteArray(); |
56 | |
} |
57 | |
} |
58 | 0 | catch (Exception e) |
59 | |
{ |
60 | 0 | throw new TransformerException(this, e); |
61 | 12 | } |
62 | |
|
63 | 12 | return super.doTransform(src, encoding); |
64 | |
} |
65 | |
|
66 | |
} |