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