查看本类的 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    *   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.http.websocketx;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.buffer.ChannelBuffers;
20  import org.jboss.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          setBinaryData(ChannelBuffers.EMPTY_BUFFER);
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
40       *            the content of the frame.
41       */
42      public ContinuationWebSocketFrame(ChannelBuffer binaryData) {
43          setBinaryData(binaryData);
44      }
45  
46      /**
47       * Creates a new continuation frame with the specified binary data
48       *
49       * @param finalFragment
50       *            flag indicating if this frame is the final fragment
51       * @param rsv
52       *            reserved bits used for protocol extensions
53       * @param binaryData
54       *            the content of the frame.
55       */
56      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData) {
57          setFinalFragment(finalFragment);
58          setRsv(rsv);
59          setBinaryData(binaryData);
60      }
61  
62      /**
63       * Creates a new continuation frame with the specified text data
64       *
65       * @param finalFragment
66       *            flag indicating if this frame is the final fragment
67       * @param rsv
68       *            reserved bits used for protocol extensions
69       * @param text
70       *            text content of the frame.
71       */
72      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, String text) {
73          setFinalFragment(finalFragment);
74          setRsv(rsv);
75          setText(text);
76      }
77  
78      /**
79       * Returns the text data in this frame
80       */
81      public String getText() {
82          if (getBinaryData() == null) {
83              return null;
84          }
85          return getBinaryData().toString(CharsetUtil.UTF_8);
86      }
87  
88      /**
89       * Sets the string for this frame
90       *
91       * @param text
92       *            text to store
93       */
94      public void setText(String text) {
95          if (text == null || text.length() == 0) {
96              setBinaryData(ChannelBuffers.EMPTY_BUFFER);
97          } else {
98              setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
99          }
100     }
101 
102     @Override
103     public String toString() {
104         return getClass().getSimpleName() + "(data: " + getBinaryData() + ')';
105     }
106 }