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 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          super(Unpooled.buffer(0));
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          super(fromText(text));
42      }
43  
44      /**
45       * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
46       *
47       * @param binaryData
48       *            the content of the frame. Must be UTF-8 encoded
49       */
50      public TextWebSocketFrame(ByteBuf binaryData) {
51          super(binaryData);
52      }
53  
54      /**
55       * Creates a new text frame with the specified text string. The final fragment flag is set to true.
56       *
57       * @param finalFragment
58       *            flag indicating if this frame is the final fragment
59       * @param rsv
60       *            reserved bits used for protocol extensions
61       * @param text
62       *            String to put in the frame
63       */
64      public TextWebSocketFrame(boolean finalFragment, int rsv, String text) {
65          super(finalFragment, rsv, fromText(text));
66      }
67  
68      private static ByteBuf fromText(String text) {
69          if (text == null || text.isEmpty()) {
70              return Unpooled.EMPTY_BUFFER;
71          } else {
72              return Unpooled.copiedBuffer(text, CharsetUtil.UTF_8);
73          }
74      }
75  
76      /**
77       * Creates a new text frame with the specified binary data. The final fragment flag is set to true.
78       *
79       * @param finalFragment
80       *            flag indicating if this frame is the final fragment
81       * @param rsv
82       *            reserved bits used for protocol extensions
83       * @param binaryData
84       *            the content of the frame. Must be UTF-8 encoded
85       */
86      public TextWebSocketFrame(boolean finalFragment, int rsv, ByteBuf binaryData) {
87          super(finalFragment, rsv, binaryData);
88      }
89  
90      /**
91       * Returns the text data in this frame
92       */
93      public String text() {
94          return content().toString(CharsetUtil.UTF_8);
95      }
96  
97      @Override
98      public TextWebSocketFrame copy() {
99          return new TextWebSocketFrame(isFinalFragment(), rsv(), content().copy());
100     }
101 
102     @Override
103     public TextWebSocketFrame duplicate() {
104         return new TextWebSocketFrame(isFinalFragment(), rsv(), content().duplicate());
105     }
106 
107     @Override
108     public TextWebSocketFrame retain() {
109         super.retain();
110         return this;
111     }
112 
113     @Override
114     public TextWebSocketFrame retain(int increment) {
115         super.retain(increment);
116         return this;
117     }
118 }