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.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.util.CharsetUtil;
21  
22  /**
23   * Web Socket continuation frame containing continuation text or binary data. This is used for
24   * fragmented messages where the contents of a messages is contained more than 1 frame.
25   */
26  public class ContinuationWebSocketFrame extends WebSocketFrame {
27  
28      /**
29       * Creates a new empty continuation frame.
30       */
31      public ContinuationWebSocketFrame() {
32          this(Unpooled.buffer(0));
33      }
34  
35      /**
36       * Creates a new continuation frame with the specified binary data. The final fragment flag is
37       * set to true.
38       *
39       * @param binaryData the content of the frame.
40       */
41      public ContinuationWebSocketFrame(ByteBuf binaryData) {
42          super(binaryData);
43      }
44  
45      /**
46       * Creates a new continuation frame with the specified binary data
47       *
48       * @param finalFragment
49       *            flag indicating if this frame is the final fragment
50       * @param rsv
51       *            reserved bits used for protocol extensions
52       * @param binaryData
53       *            the content of the frame.
54       */
55      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, ByteBuf binaryData) {
56          super(finalFragment, rsv, binaryData);
57      }
58  
59      /**
60       * Creates a new continuation frame with the specified text data
61       *
62       * @param finalFragment
63       *            flag indicating if this frame is the final fragment
64       * @param rsv
65       *            reserved bits used for protocol extensions
66       * @param text
67       *            text content of the frame.
68       */
69      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, String text) {
70          this(finalFragment, rsv, fromText(text));
71      }
72  
73      /**
74       * Returns the text data in this frame
75       */
76      public String text() {
77          return content().toString(CharsetUtil.UTF_8);
78      }
79  
80      /**
81       * Sets the string for this frame
82       *
83       * @param text
84       *            text to store
85       */
86      private static ByteBuf fromText(String text) {
87          if (text == null || text.isEmpty()) {
88              return Unpooled.EMPTY_BUFFER;
89          } else {
90              return Unpooled.copiedBuffer(text, CharsetUtil.UTF_8);
91          }
92      }
93  
94      @Override
95      public ContinuationWebSocketFrame copy() {
96          return new ContinuationWebSocketFrame(isFinalFragment(), rsv(), content().copy());
97      }
98  
99      @Override
100     public ContinuationWebSocketFrame duplicate() {
101         return new ContinuationWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
102     }
103 
104     @Override
105     public ContinuationWebSocketFrame retain() {
106         super.retain();
107         return this;
108     }
109 
110     @Override
111     public ContinuationWebSocketFrame retain(int increment) {
112         super.retain(increment);
113         return this;
114     }
115 }