01: /*
02: * $Id: ObjectToInputStream.java 10787 2008-02-12 18:51:50Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: * <code>ObjectToInputStream</code> converts serilaizable object to a input stream but
23: * treats <code>java.lang.String</code> differently by converting to bytes using
24: * the <code>String.getBytrs()</code> method.
25: */
26: public class ObjectToInputStream extends SerializableToByteArray {
27:
28: public ObjectToInputStream() {
29: this .registerSourceType(String.class);
30: this .registerSourceType(OutputHandler.class);
31: setReturnClass(InputStream.class);
32: }
33:
34: // @Override
35: public Object doTransform(Object src, String encoding)
36: throws TransformerException {
37: try {
38: if (src instanceof String) {
39: return new ByteArrayInputStream(((String) src)
40: .getBytes(encoding));
41: } else if (src instanceof OutputHandler) {
42: OutputHandler oh = (OutputHandler) src;
43:
44: ByteArrayOutputStream out = new ByteArrayOutputStream();
45: oh.write(RequestContext.getEvent(), out);
46:
47: return new ByteArrayInputStream(out.toByteArray());
48: }
49: } catch (Exception e) {
50: throw new TransformerException(this , e);
51: }
52:
53: byte[] bytes = (byte[]) super .doTransform(src, encoding);
54: return new ByteArrayInputStream(bytes);
55: }
56:
57: }
|