1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp.protocols; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.RequestContext; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.transformer.wire.WireFormat; |
13 | |
|
14 | |
import java.io.ByteArrayInputStream; |
15 | |
import java.io.ByteArrayOutputStream; |
16 | |
import java.io.IOException; |
17 | |
import java.io.InputStream; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
class MuleMessageWorker |
24 | |
{ |
25 | |
|
26 | |
private final WireFormat wireFormat; |
27 | |
|
28 | |
MuleMessageWorker(WireFormat wireFormat) |
29 | 0 | { |
30 | 0 | this.wireFormat = wireFormat; |
31 | 0 | } |
32 | |
|
33 | |
public byte[] doWrite() throws IOException |
34 | |
{ |
35 | |
|
36 | 0 | DefaultMuleMessage msg = (DefaultMuleMessage) RequestContext.getEvent().getMessage(); |
37 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
38 | |
try |
39 | |
{ |
40 | 0 | wireFormat.write(baos, msg, msg.getEncoding()); |
41 | |
} |
42 | 0 | catch (MuleException e) |
43 | |
{ |
44 | 0 | throw new IOException(e.getDetailedMessage()); |
45 | 0 | } |
46 | 0 | return baos.toByteArray(); |
47 | |
} |
48 | |
|
49 | |
public Object doRead(Object message) throws IOException |
50 | |
{ |
51 | 0 | if (message == null) |
52 | |
{ |
53 | 0 | return null; |
54 | |
} |
55 | |
|
56 | |
InputStream in; |
57 | 0 | if (message instanceof byte[]) |
58 | |
{ |
59 | 0 | in = new ByteArrayInputStream((byte[]) message); |
60 | |
} |
61 | |
else |
62 | |
{ |
63 | 0 | in = (InputStream) message; |
64 | |
} |
65 | |
|
66 | |
try |
67 | |
{ |
68 | 0 | return wireFormat.read(in); |
69 | |
} |
70 | 0 | catch (MuleException e) |
71 | |
{ |
72 | 0 | throw new IOException(e.getDetailedMessage()); |
73 | |
} |
74 | |
|
75 | |
} |
76 | |
|
77 | |
} |