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 * Creates a new decoder whose maximum object size is {@code 1048576}
58 * bytes. If the size of the received object is greater than
59 * {@code 1048576} bytes, a {@link StreamCorruptedException} will be
60 * raised.
61 *
62 * @param classResolver the {@link ClassResolver} to use for this decoder
63 */
64 public ObjectDecoder(ClassResolver classResolver) {
65 this(1048576, classResolver);
66 }
67
68 /**
69 * Creates a new decoder with the specified maximum object size.
70 *
71 * @param maxObjectSize the maximum byte length of the serialized object.
72 * if the length of the received object is greater
73 * than this value, {@link StreamCorruptedException}
74 * will be raised.
75 * @deprecated use {@link #ObjectDecoder(int, ClassResolver)}
76 */
77 @Deprecated
78 public ObjectDecoder(int maxObjectSize) {
79 this(maxObjectSize, ClassResolvers.weakCachingResolver(null));
80 }
81
82 /**
83 * Creates a new decoder with the specified maximum object size.
84 *
85 * @param maxObjectSize the maximum byte length of the serialized object.
86 * if the length of the received object is greater
87 * than this value, {@link StreamCorruptedException}
88 * will be raised.
89 * @param classResolver the {@link ClassResolver} which will load the class
90 * of the serialized object
91 */
92 public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
93 super(maxObjectSize, 0, 4, 0, 4);
94 if (classResolver == null) {
95 throw new NullPointerException("classResolver");
96 }
97 this.classResolver = classResolver;
98 }
99
100 /**
101 * Create a new decoder with the specified maximum object size and the {@link ClassLoader}
102 * wrapped in {@link ClassResolvers#weakCachingResolver(ClassLoader)}.
103 *
104 * @param maxObjectSize the maximum byte length of the serialized object.
105 * if the length of the received object is greater
106 * than this value, {@link StreamCorruptedException}
107 * will be raised.
108 * @param classLoader the the classloader to use
109 * @deprecated use {@link #ObjectDecoder(int, ClassResolver)}
110 */
111 @Deprecated
112 public ObjectDecoder(int maxObjectSize, ClassLoader classLoader) {
113 this(maxObjectSize, ClassResolvers.weakCachingResolver(classLoader));
114 }
115
116 @Override
117 protected Object decode(
118 ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
119
120 ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer);
121 if (frame == null) {
122 return null;
123 }
124
125 return new CompactObjectInputStream(
126 new ChannelBufferInputStream(frame), classResolver).readObject();
127 }
128
129 @Override
130 protected ChannelBuffer extractFrame(ChannelBuffer buffer, int index, int length) {
131 return buffer.slice(index, length);
132 }
133 }