1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport;
12
13 import org.mule.api.ThreadSafeAccess;
14 import org.mule.api.transport.MessageTypeNotSupportedException;
15
16 import java.io.IOException;
17 import java.io.StringWriter;
18 import java.io.Writer;
19
20
21
22
23
24 public class WriterMessageAdapter extends AbstractMessageAdapter
25 {
26
27
28
29 private static final long serialVersionUID = -1065602752454818625L;
30
31 private final StringWriter writer;
32
33 public WriterMessageAdapter(Object message) throws MessageTypeNotSupportedException
34 {
35 if (message instanceof String)
36 {
37 writer = new StringWriter();
38 writer.write((String) message);
39 }
40 else if (message instanceof StringWriter)
41 {
42 this.writer = (StringWriter) message;
43 }
44 else
45 {
46 throw new MessageTypeNotSupportedException(message, getClass());
47 }
48 }
49
50 protected WriterMessageAdapter(WriterMessageAdapter template)
51 {
52 super(template);
53 writer = template.writer;
54 }
55
56
57
58
59
60
61
62
63
64 public String getPayloadAsString(String encoding) throws Exception
65 {
66 return writer.toString();
67 }
68
69
70
71
72
73
74
75 public byte[] getPayloadAsBytes() throws Exception
76 {
77 return writer.toString().getBytes();
78 }
79
80
81
82
83 public Object getPayload()
84 {
85 return writer.toString();
86 }
87
88 public void write(String string)
89 {
90 writer.write(string);
91 }
92
93 public void write(String string, int offset, int len)
94 {
95 writer.write(string, offset, len);
96 }
97
98 public Writer getWriter()
99 {
100 return writer;
101 }
102
103 public void flush()
104 {
105 writer.flush();
106 }
107
108 public void close() throws IOException
109 {
110 writer.close();
111 }
112
113 public ThreadSafeAccess newThreadCopy()
114 {
115 return new WriterMessageAdapter(this);
116 }
117
118 }