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 {
31 this.registerSourceType(String.class);
32 this.registerSourceType(OutputHandler.class);
33 setReturnClass(InputStream.class);
34 }
35
36
37 public Object doTransform(Object src, String encoding) throws TransformerException
38 {
39 try
40 {
41 if (src instanceof String)
42 {
43 return new ByteArrayInputStream(((String) src).getBytes(encoding));
44 }
45 else if (src instanceof OutputHandler)
46 {
47 OutputHandler oh = (OutputHandler) src;
48
49 ByteArrayOutputStream out = new ByteArrayOutputStream();
50 oh.write(RequestContext.getEvent(), out);
51
52 return new ByteArrayInputStream(out.toByteArray());
53 }
54 }
55 catch (Exception e)
56 {
57 throw new TransformerException(this, e);
58 }
59
60
61 byte[] bytes = (byte[]) super.doTransform(src, encoding);
62 return new ByteArrayInputStream(bytes);
63 }
64
65 }