1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transformers.simple; |
12 | |
|
13 | |
import org.mule.config.i18n.MessageFactory; |
14 | |
import org.mule.transformers.AbstractTransformer; |
15 | |
import org.mule.umo.transformer.TransformerException; |
16 | |
|
17 | |
import java.io.UnsupportedEncodingException; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
public class StringToByteArray extends AbstractTransformer |
24 | |
{ |
25 | |
|
26 | |
public StringToByteArray() |
27 | 16 | { |
28 | 20 | registerSourceType(String.class); |
29 | 16 | registerSourceType(byte[].class); |
30 | 16 | setReturnClass(byte[].class); |
31 | 16 | } |
32 | |
|
33 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
34 | |
{ |
35 | 10 | if (src instanceof byte[]) |
36 | |
{ |
37 | 0 | return src; |
38 | |
} |
39 | |
else |
40 | |
{ |
41 | |
try |
42 | |
{ |
43 | 10 | return ((String) src).getBytes(encoding); |
44 | |
} |
45 | 0 | catch (UnsupportedEncodingException e) |
46 | |
{ |
47 | 0 | throw new TransformerException(MessageFactory |
48 | |
.createStaticMessage("Unable to convert String to byte[]."), e); |
49 | |
} |
50 | |
} |
51 | |
} |
52 | |
|
53 | |
} |