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 | |
import org.mule.transformer.types.DataTypeFactory; |
17 | |
|
18 | |
import java.io.ByteArrayInputStream; |
19 | |
import java.io.ByteArrayOutputStream; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class ObjectToInputStream extends SerializableToByteArray |
28 | |
{ |
29 | |
|
30 | |
public ObjectToInputStream() |
31 | 0 | { |
32 | 0 | this.registerSourceType(DataTypeFactory.STRING); |
33 | 0 | this.registerSourceType(DataTypeFactory.create(OutputHandler.class)); |
34 | 0 | setReturnDataType(DataTypeFactory.INPUT_STREAM); |
35 | 0 | } |
36 | |
|
37 | |
@Override |
38 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
39 | |
{ |
40 | |
try |
41 | |
{ |
42 | 0 | if (src instanceof String) |
43 | |
{ |
44 | 0 | return new ByteArrayInputStream(((String) src).getBytes(encoding)); |
45 | |
} |
46 | 0 | else if (src instanceof byte[]) |
47 | |
{ |
48 | 0 | return new ByteArrayInputStream((byte[]) src); |
49 | |
} |
50 | 0 | else if (src instanceof OutputHandler) |
51 | |
{ |
52 | 0 | OutputHandler oh = (OutputHandler) src; |
53 | |
|
54 | 0 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
55 | 0 | oh.write(RequestContext.getEvent(), out); |
56 | |
|
57 | 0 | return new ByteArrayInputStream(out.toByteArray()); |
58 | |
} |
59 | |
else |
60 | |
{ |
61 | 0 | return new ByteArrayInputStream((byte[]) super.doTransform(src, encoding)); |
62 | |
} |
63 | |
} |
64 | 0 | catch (Exception e) |
65 | |
{ |
66 | 0 | throw new TransformerException(this, e); |
67 | |
} |
68 | |
} |
69 | |
|
70 | |
} |