1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.marshalling;
17
18 import org.jboss.marshalling.Marshaller;
19 import org.jboss.netty.channel.Channel;
20 import org.jboss.netty.channel.ChannelHandler.Sharable;
21 import org.jboss.netty.channel.ChannelHandlerContext;
22 import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
23
24
25
26
27
28
29
30
31
32
33
34 @Sharable
35 public class CompatibleMarshallingEncoder extends OneToOneEncoder {
36
37 private final MarshallerProvider provider;
38
39
40
41
42
43
44 public CompatibleMarshallingEncoder(MarshallerProvider provider) {
45 this.provider = provider;
46 }
47
48 @Override
49 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
50 Marshaller marshaller = provider.getMarshaller(ctx);
51 ChannelBufferByteOutput output =
52 new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), 256);
53 marshaller.start(output);
54 marshaller.writeObject(msg);
55 marshaller.finish();
56 marshaller.close();
57
58 return output.getBuffer();
59 }
60 }