1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.streaming;
12
13 import org.mule.impl.ThreadSafeAccess;
14 import org.mule.providers.AbstractMessageAdapter;
15 import org.mule.umo.provider.MessageTypeNotSupportedException;
16 import org.mule.util.StringMessageUtils;
17
18 import java.io.IOException;
19 import java.io.OutputStream;
20
21 import org.apache.commons.io.output.ByteArrayOutputStream;
22
23
24
25
26
27 public class OutStreamMessageAdapter extends AbstractMessageAdapter
28 {
29
30
31
32 private static final long serialVersionUID = -299373598028203772L;
33
34 private final OutputStream out;
35
36 public OutStreamMessageAdapter(Object message) throws MessageTypeNotSupportedException
37 {
38 try
39 {
40 if (message instanceof OutputStream)
41 {
42 out = (OutputStream) message;
43 }
44 else if (message instanceof String)
45 {
46 out = new ByteArrayOutputStream(message.toString().length());
47 out.write(StringMessageUtils.getBytes(message.toString()));
48 }
49 else if (message instanceof byte[])
50 {
51 out = new ByteArrayOutputStream(((byte[]) message).length);
52 out.write((byte[]) message);
53
54 }
55 else
56 {
57 throw new MessageTypeNotSupportedException(message, getClass());
58 }
59 }
60 catch (IOException e)
61 {
62 throw new MessageTypeNotSupportedException(message, getClass(), e);
63 }
64 }
65
66 protected OutStreamMessageAdapter(OutStreamMessageAdapter template)
67 {
68 super(template);
69 out = template.out;
70 }
71
72
73
74
75
76
77
78
79
80 public String getPayloadAsString(String encoding) throws Exception
81 {
82 if (out instanceof ByteArrayOutputStream)
83 {
84 return StringMessageUtils.getString(((ByteArrayOutputStream) out).toByteArray(), encoding);
85 }
86 else
87 {
88 logger.warn("Attempting to get the String contents of a non-ByteArray output stream");
89 return out.toString();
90 }
91 }
92
93
94
95
96
97
98
99 public byte[] getPayloadAsBytes() throws Exception
100 {
101 if (out instanceof ByteArrayOutputStream)
102 {
103 return ((ByteArrayOutputStream) out).toByteArray();
104 }
105 else
106 {
107 logger.warn("Attempting to get the bytes of a non-ByteArray output stream");
108 return StringMessageUtils.getBytes(out.toString());
109 }
110 }
111
112
113
114
115 public Object getPayload()
116 {
117 return out;
118 }
119
120 public void write(String string) throws IOException
121 {
122 out.write(StringMessageUtils.getBytes(string));
123 }
124
125 public void write(String string, int offset, int len) throws IOException
126 {
127 out.write(StringMessageUtils.getBytes(string), offset, len);
128 }
129
130 public void write(byte[] bytes) throws IOException
131 {
132 out.write(bytes);
133 }
134
135 public void write(byte[] bytes, int offset, int len) throws IOException
136 {
137 out.write(bytes, offset, len);
138 }
139
140 public OutputStream getStream()
141 {
142 return out;
143 }
144
145 public void flush() throws IOException
146 {
147 out.flush();
148 }
149
150 public void close() throws IOException
151 {
152 out.close();
153 }
154
155 public ThreadSafeAccess newThreadCopy()
156 {
157 return new OutStreamMessageAdapter(this);
158 }
159
160 }