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 | |
public class ByteArrayToString extends AbstractTransformer |
23 | |
{ |
24 | |
|
25 | |
public ByteArrayToString() |
26 | 0 | { |
27 | 0 | registerSourceType(byte[].class); |
28 | 0 | registerSourceType(String.class); |
29 | 0 | setReturnClass(String.class); |
30 | 0 | } |
31 | |
|
32 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
33 | |
{ |
34 | 0 | if (src instanceof String) |
35 | |
{ |
36 | 0 | return src; |
37 | |
} |
38 | |
else |
39 | |
{ |
40 | |
try |
41 | |
{ |
42 | 0 | return new String((byte[]) src, encoding); |
43 | |
} |
44 | 0 | catch (UnsupportedEncodingException e) |
45 | |
{ |
46 | 0 | throw new TransformerException(MessageFactory |
47 | |
.createStaticMessage("Unable to convert byte[] to String."), e); |
48 | |
} |
49 | |
} |
50 | |
} |
51 | |
} |