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