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 | 0 | { |
38 | |
try |
39 | |
{ |
40 | 0 | if (message instanceof OutputStream) |
41 | |
{ |
42 | 0 | out = (OutputStream) message; |
43 | |
} |
44 | 0 | else if (message instanceof String) |
45 | |
{ |
46 | 0 | out = new ByteArrayOutputStream(message.toString().length()); |
47 | 0 | out.write(StringMessageUtils.getBytes(message.toString())); |
48 | |
} |
49 | 0 | else if (message instanceof byte[]) |
50 | |
{ |
51 | 0 | out = new ByteArrayOutputStream(((byte[]) message).length); |
52 | 0 | out.write((byte[]) message); |
53 | |
|
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | throw new MessageTypeNotSupportedException(message, getClass()); |
58 | |
} |
59 | |
} |
60 | 0 | catch (IOException e) |
61 | |
{ |
62 | 0 | throw new MessageTypeNotSupportedException(message, getClass(), e); |
63 | 0 | } |
64 | 0 | } |
65 | |
|
66 | |
protected OutStreamMessageAdapter(OutStreamMessageAdapter template) |
67 | |
{ |
68 | 0 | super(template); |
69 | 0 | out = template.out; |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public String getPayloadAsString(String encoding) throws Exception |
81 | |
{ |
82 | 0 | if (out instanceof ByteArrayOutputStream) |
83 | |
{ |
84 | 0 | return StringMessageUtils.getString(((ByteArrayOutputStream) out).toByteArray(), encoding); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 0 | logger.warn("Attempting to get the String contents of a non-ByteArray output stream"); |
89 | 0 | return out.toString(); |
90 | |
} |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public byte[] getPayloadAsBytes() throws Exception |
100 | |
{ |
101 | 0 | if (out instanceof ByteArrayOutputStream) |
102 | |
{ |
103 | 0 | return ((ByteArrayOutputStream) out).toByteArray(); |
104 | |
} |
105 | |
else |
106 | |
{ |
107 | 0 | logger.warn("Attempting to get the bytes of a non-ByteArray output stream"); |
108 | 0 | return StringMessageUtils.getBytes(out.toString()); |
109 | |
} |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public Object getPayload() |
116 | |
{ |
117 | 0 | return out; |
118 | |
} |
119 | |
|
120 | |
public void write(String string) throws IOException |
121 | |
{ |
122 | 0 | out.write(StringMessageUtils.getBytes(string)); |
123 | 0 | } |
124 | |
|
125 | |
public void write(String string, int offset, int len) throws IOException |
126 | |
{ |
127 | 0 | out.write(StringMessageUtils.getBytes(string), offset, len); |
128 | 0 | } |
129 | |
|
130 | |
public void write(byte[] bytes) throws IOException |
131 | |
{ |
132 | 0 | out.write(bytes); |
133 | 0 | } |
134 | |
|
135 | |
public void write(byte[] bytes, int offset, int len) throws IOException |
136 | |
{ |
137 | 0 | out.write(bytes, offset, len); |
138 | 0 | } |
139 | |
|
140 | |
public OutputStream getStream() |
141 | |
{ |
142 | 0 | return out; |
143 | |
} |
144 | |
|
145 | |
public void flush() throws IOException |
146 | |
{ |
147 | 0 | out.flush(); |
148 | 0 | } |
149 | |
|
150 | |
public void close() throws IOException |
151 | |
{ |
152 | 0 | out.close(); |
153 | 0 | } |
154 | |
|
155 | |
public ThreadSafeAccess newThreadCopy() |
156 | |
{ |
157 | 0 | return new OutStreamMessageAdapter(this); |
158 | |
} |
159 | |
|
160 | |
} |