View Javadoc
1   /*
2    * Copyright 2014 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.http.websocketx.extensions.compression;
17  
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
20  import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
21  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
22  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
23  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtension;
24  
25  import java.util.List;
26  
27  /**
28   * Per-message implementation of deflate compressor.
29   */
30  class PerMessageDeflateEncoder extends DeflateEncoder {
31  
32      private boolean compressing;
33  
34      /**
35       * Constructor
36       * @param compressionLevel compression level of the compressor.
37       * @param windowSize maximum size of the window compressor buffer.
38       * @param noContext true to disable context takeover.
39       */
40      public PerMessageDeflateEncoder(int compressionLevel, int windowSize, boolean noContext) {
41          super(compressionLevel, windowSize, noContext);
42      }
43  
44      @Override
45      public boolean acceptOutboundMessage(Object msg) throws Exception {
46          return ((msg instanceof TextWebSocketFrame ||
47                  msg instanceof BinaryWebSocketFrame) &&
48                     (((WebSocketFrame) msg).rsv() & WebSocketExtension.RSV1) == 0) ||
49                 (msg instanceof ContinuationWebSocketFrame && compressing);
50      }
51  
52      @Override
53      protected int rsv(WebSocketFrame msg) {
54          return msg instanceof TextWebSocketFrame || msg instanceof BinaryWebSocketFrame ?
55                  msg.rsv() | WebSocketExtension.RSV1 : msg.rsv();
56      }
57  
58      @Override
59      protected boolean removeFrameTail(WebSocketFrame msg) {
60          return msg.isFinalFragment();
61      }
62  
63      @Override
64      protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg,
65              List<Object> out) throws Exception {
66          super.encode(ctx, msg, out);
67  
68          if (msg.isFinalFragment()) {
69              compressing = false;
70          } else if (msg instanceof TextWebSocketFrame || msg instanceof BinaryWebSocketFrame) {
71              compressing = true;
72          }
73      }
74  
75  }