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 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 text frame with assumed UTF-8 encoding
24   */
25  public class TextWebSocketFrame extends WebSocketFrame {
26  
27      /**
28       * Creates a new empty text frame.
29       */
30      public TextWebSocketFrame() {
31          setBinaryData(ChannelBuffers.EMPTY_BUFFER);
32      }
33  
34      /**
35       * Creates a new text frame with the specified text string. The final fragment flag is set to true.
36       *
37       * @param text
38       *            String to put in the frame
39       */
40      public TextWebSocketFrame(String text) {
41          if (text == null || text.length() == 0) {
42              setBinaryData(ChannelBuffers.EMPTY_BUFFER);
43          } else {
44              setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
45          }
46      }
47  
48      /**
49       * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
50       *
51       * @param binaryData
52       *            the content of the frame. Must be UTF-8 encoded
53       */
54      public TextWebSocketFrame(ChannelBuffer binaryData) {
55          setBinaryData(binaryData);
56      }
57  
58      /**
59       * Creates a new text frame with the specified text string. The final fragment flag is set to true.
60       *
61       * @param finalFragment
62       *            flag indicating if this frame is the final fragment
63       * @param rsv
64       *            reserved bits used for protocol extensions
65       * @param text
66       *            String to put in the frame
67       */
68      public TextWebSocketFrame(boolean finalFragment, int rsv, String text) {
69          setFinalFragment(finalFragment);
70          setRsv(rsv);
71          if (text == null || text.length() == 0) {
72              setBinaryData(ChannelBuffers.EMPTY_BUFFER);
73          } else {
74              setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
75          }
76      }
77  
78      /**
79       * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
80       *
81       * @param finalFragment
82       *            flag indicating if this frame is the final fragment
83       * @param rsv
84       *            reserved bits used for protocol extensions
85       * @param binaryData
86       *            the content of the frame. Must be UTF-8 encoded
87       */
88      public TextWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData) {
89          setFinalFragment(finalFragment);
90          setRsv(rsv);
91          setBinaryData(binaryData);
92      }
93  
94      /**
95       * Returns the text data in this frame
96       */
97      public String getText() {
98          if (getBinaryData() == null) {
99              return null;
100         }
101         return getBinaryData().toString(CharsetUtil.UTF_8);
102     }
103 
104     /**
105      * Sets the string for this frame
106      *
107      * @param text
108      *            text to store
109      */
110     public void setText(String text) {
111         if (text == null) {
112             throw new NullPointerException("text");
113         }
114         setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
115     }
116 
117     @Override
118     public String toString() {
119         return getClass().getSimpleName() + "(text: " + getText() + ')';
120     }
121 }