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
45 public CompatibleMarshallingEncoder(MarshallerProvider provider) {
46 this.provider = provider;
47 }
48
49
50 @Override
51 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
52 Marshaller marshaller = provider.getMarshaller(ctx);
53 ChannelBufferByteOutput output =
54 new ChannelBufferByteOutput(ctx.getChannel().getConfig().getBufferFactory(), 256);
55 marshaller.start(output);
56 marshaller.writeObject(msg);
57 marshaller.finish();
58 marshaller.close();
59
60 return output.getBuffer();
61 }
62 }