查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.marshalling;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.handler.codec.ReplayingDecoder;
22  import io.netty.handler.codec.TooLongFrameException;
23  import org.jboss.marshalling.ByteInput;
24  import org.jboss.marshalling.Unmarshaller;
25  
26  import java.io.ObjectStreamConstants;
27  import java.util.List;
28  
29  /**
30   * {@link ReplayingDecoder} which use an {@link Unmarshaller} to read the Object out of the {@link ByteBuf}.
31   *
32   * If you can you should use {@link MarshallingDecoder}.
33   */
34  public class CompatibleMarshallingDecoder extends ReplayingDecoder<Void> {
35      protected final UnmarshallerProvider provider;
36      protected final int maxObjectSize;
37      private boolean discardingTooLongFrame;
38  
39      /**
40       * Create a new instance of {@link CompatibleMarshallingDecoder}.
41       *
42       * @param provider
43       *        the {@link UnmarshallerProvider} which is used to obtain the {@link Unmarshaller}
44       *        for the {@link Channel}
45       * @param maxObjectSize
46       *        the maximal size (in bytes) of the {@link Object} to unmarshal. Once the size is
47       *        exceeded the {@link Channel} will get closed. Use a maxObjectSize of
48       *        {@link Integer#MAX_VALUE} to disable this.  You should only do this if you are sure
49       *        that the received Objects will never be big and the sending side are trusted, as this
50       *        opens the possibility for a DOS-Attack due an {@link OutOfMemoryError}.
51       */
52      public CompatibleMarshallingDecoder(UnmarshallerProvider provider, int maxObjectSize) {
53          this.provider = provider;
54          this.maxObjectSize = maxObjectSize;
55      }
56  
57      @Override
58      protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
59          if (discardingTooLongFrame) {
60              buffer.skipBytes(actualReadableBytes());
61              checkpoint();
62              return;
63          }
64  
65          Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
66          ByteInput input = new ChannelBufferByteInput(buffer);
67          if (maxObjectSize != Integer.MAX_VALUE) {
68              input = new LimitingByteInput(input, maxObjectSize);
69          }
70          try {
71              unmarshaller.start(input);
72              Object obj = unmarshaller.readObject();
73              unmarshaller.finish();
74              out.add(obj);
75          } catch (LimitingByteInput.TooBigObjectException ignored) {
76              discardingTooLongFrame = true;
77              throw new TooLongFrameException();
78          } finally {
79              // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
80              // readable. This helps to be sure that we do not leak resource
81              unmarshaller.close();
82          }
83      }
84  
85      @Override
86      protected void decodeLast(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
87          switch (buffer.readableBytes()) {
88          case 0:
89              return;
90          case 1:
91              // Ignore the last TC_RESET
92              if (buffer.getByte(buffer.readerIndex()) == ObjectStreamConstants.TC_RESET) {
93                  buffer.skipBytes(1);
94                  return;
95              }
96          }
97  
98          decode(ctx, buffer, out);
99      }
100 
101     @Override
102     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
103         if (cause instanceof TooLongFrameException) {
104             ctx.close();
105         } else {
106             super.exceptionCaught(ctx, cause);
107         }
108     }
109 }