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 io.netty.handler.codec.serialization;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufInputStream;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
22  
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.StreamCorruptedException;
26  
27  /**
28   * A decoder which deserializes the received {@link ByteBuf}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  public class ObjectDecoder extends LengthFieldBasedFrameDecoder {
37  
38      private final ClassResolver classResolver;
39  
40      /**
41       * Creates a new decoder whose maximum object size is {@code 1048576}
42       * bytes.  If the size of the received object is greater than
43       * {@code 1048576} bytes, a {@link StreamCorruptedException} will be
44       * raised.
45       *
46       * @param classResolver  the {@link ClassResolver} to use for this decoder
47       */
48      public ObjectDecoder(ClassResolver classResolver) {
49          this(1048576, classResolver);
50      }
51  
52      /**
53       * Creates a new decoder with the specified maximum object size.
54       *
55       * @param maxObjectSize  the maximum byte length of the serialized object.
56       *                       if the length of the received object is greater
57       *                       than this value, {@link StreamCorruptedException}
58       *                       will be raised.
59       * @param classResolver    the {@link ClassResolver} which will load the class
60       *                       of the serialized object
61       */
62      public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) {
63          super(maxObjectSize, 0, 4, 0, 4);
64          this.classResolver = classResolver;
65      }
66  
67      @Override
68      protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
69          ByteBuf frame = (ByteBuf) super.decode(ctx, in);
70          if (frame == null) {
71              return null;
72          }
73  
74          ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
75          try {
76              return ois.readObject();
77          } finally {
78              ois.close();
79          }
80      }
81  }