View Javadoc

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       * @param classResolver  the {@link ClassResolver} to use for this decoder
50       */
51      public ObjectDecoder(ClassResolver classResolver) {
52          this(1048576, classResolver);
53      }
54  
55      /**
56       * Creates a new decoder with the specified maximum object size.
57       *
58       * @param maxObjectSize  the maximum byte length of the serialized object.
59       *                       if the length of the received object is greater
60       *                       than this value, {@link StreamCorruptedException}
61       *                       will be raised.
62       * @param classResolver    the {@link ClassResolver} which will load the class
63       *                       of the serialized object
64       */
65      public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
66          super(maxObjectSize, 0, 4, 0, 4);
67          if (classResolver == null) {
68              throw new NullPointerException("classResolver");
69          }
70          this.classResolver = classResolver;
71      }
72  
73      @Override
74      protected Object decode(
75              ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
76  
77          ChannelBuffer frame = (ChannelBuffer) super.decode(ctx, channel, buffer);
78          if (frame == null) {
79              return null;
80          }
81  
82          return new CompactObjectInputStream(
83                  new ChannelBufferInputStream(frame), classResolver).readObject();
84      }
85  
86      @Override
87      protected ChannelBuffer extractFrame(ChannelBuffer buffer, int index, int length) {
88          return buffer.slice(index, length);
89      }
90  }