1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package org.jboss.netty.handler.codec.serialization;
17
18 import java.io.ObjectOutputStream;
19 import java.io.StreamCorruptedException;
20
21 import org.jboss.netty.buffer.ChannelBuffer;
22 import org.jboss.netty.buffer.ChannelBufferInputStream;
23 import org.jboss.netty.channel.Channel;
24 import org.jboss.netty.channel.ChannelHandlerContext;
25 import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
26
27 /**
28 * A decoder which deserializes the received {@link ChannelBuffer}s into Java
29 * objects.
30 * <p>
31 * Please note that the serialized form this decoder expects is not
32 * compatible with the standard {@link ObjectOutputStream}. Please use
33 * {@link ObjectEncoder} or {@link ObjectEncoderOutputStream} to ensure the
34 * interoperability with this decoder.
35 *
36 * @apiviz.landmark
37 * @apiviz.has org.jboss.netty.handler.codec.serialization.ObjectDecoderInputStream - - - compatible with
38 */
39 public class ObjectDecoder extends LengthFieldBasedFrameDecoder {
40
41 private final ClassResolver classResolver;
42
43 /**
44 * Creates a new decoder whose maximum object size is {@code 1048576}
45 * bytes. If the size of the received object is greater than
46 * {@code 1048576} bytes, a {@link StreamCorruptedException} will be
47 * raised.
48 *
49 * @deprecated use {@link #ObjectDecoder(ClassResolver)}
50 */
51 @Deprecated
52 public ObjectDecoder() {
53 this(1048576);
54 }
55
56
57 /**
58 * Creates a new decoder whose maximum object size is {@code 1048576}
59 * bytes. If the size of the received object is greater than
60 * {@code 1048576} bytes, a {@link StreamCorruptedException} will be
61 * raised.
62 *
63 * @param classResolver the {@link ClassResolver} to use for this decoder
64 */
65 public ObjectDecoder(ClassResolver classResolver) {
66 this(1048576, classResolver);
67 }
68
69 /**
70 * Creates a new decoder with the specified maximum object size.
71 *
72 * @param maxObjectSize the maximum byte length of the serialized object.
73 * if the length of the received object is greater
74 * than this value, {@link StreamCorruptedException}
75 * will be raised.
76 * @deprecated use {@link #ObjectDecoder(int, ClassResolver)}
77 */
78 @Deprecated
79 public ObjectDecoder(int maxObjectSize) {
80 this(maxObjectSize, ClassResolvers.weakCachingResolver(null));
81 }
82
83 /**
84 * Creates a new decoder with the specified maximum object size.
85 *
86 * @param maxObjectSize the maximum byte length of the serialized object.
87 * if the length of the received object is greater
88 * than this value, {@link StreamCorruptedException}
89 * will be raised.
90 * @param classResolver the {@link ClassResolver} which will load the class
91 * of the serialized object
92 */
93 public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
94 super(maxObjectSize, 0, 4, 0, 4);
95 if (classResolver == null) {
96 throw new NullPointerException("classResolver");
97 }
98 this.classResolver = classResolver;
99 }
100
101
102 /**
103 * Create a new decoder with the specified maximum object size and the {@link ClassLoader}
104 * wrapped in {@link ClassResolvers#weakCachingResolver(ClassLoader)}.
105 *
106 * @param maxObjectSize the maximum byte length of the serialized object.
107 * if the length of the received object is greater
108 * than this value, {@link StreamCorruptedException}
109 * will be raised.
110 * @param classLoader the the classloader to use
111 * @deprecated use {@link #ObjectDecoder(int, ClassResolver)}
112 */
113 @Deprecated
114 public ObjectDecoder(int maxObjectSize, ClassLoader classLoader) {
115 this(maxObjectSize, ClassResolvers.weakCachingResolver(classLoader));
116 }
117
118 @Override
119 protected Object decode(
120 ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
121
122 ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer);
123 if (frame == null) {
124 return null;
125 }
126
127 return new CompactObjectInputStream(
128 new ChannelBufferInputStream(frame), classResolver).readObject();
129 }
130
131 @Override
132 protected ChannelBuffer extractFrame(ChannelBuffer buffer, int index, int length) {
133 return buffer.slice(index, length);
134 }
135 }