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.websocket;
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   * @deprecated Use <tt>org.jboss.netty.handler.codec.http.websocketx</tt> instead.
24   *
25   * The default {@link WebSocketFrame} implementation.
26   */
27  @Deprecated
28  public class DefaultWebSocketFrame implements WebSocketFrame {
29  
30      private int type;
31      private ChannelBuffer binaryData;
32  
33      /**
34       * Creates a new empty text frame.
35       */
36      public DefaultWebSocketFrame() {
37          this(0, ChannelBuffers.EMPTY_BUFFER);
38      }
39  
40      /**
41       * Creates a new text frame from with the specified string.
42       */
43      public DefaultWebSocketFrame(String textData) {
44          this(0, ChannelBuffers.copiedBuffer(textData, CharsetUtil.UTF_8));
45      }
46  
47      /**
48       * Creates a new frame with the specified frame type and the specified data.
49       *
50       * @param type
51       *        the type of the frame. {@code 0} is the only allowed type currently.
52       * @param binaryData
53       *        the content of the frame.  If <tt>(type &amp; 0x80 == 0)</tt>,
54       *        it must be encoded in UTF-8.
55       *
56       * @throws IllegalArgumentException
57       *         if If <tt>(type &amp; 0x80 == 0)</tt> and the data is not encoded
58       *         in UTF-8
59       */
60      public DefaultWebSocketFrame(int type, ChannelBuffer binaryData) {
61          setData(type, binaryData);
62      }
63  
64      public int getType() {
65          return type;
66      }
67  
68      public boolean isText() {
69          return (getType() & 0x80) == 0;
70      }
71  
72      public boolean isBinary() {
73          return !isText();
74      }
75  
76      public ChannelBuffer getBinaryData() {
77          return binaryData;
78      }
79  
80      public String getTextData() {
81          return getBinaryData().toString(CharsetUtil.UTF_8);
82      }
83  
84      public void setData(int type, ChannelBuffer binaryData) {
85          if (binaryData == null) {
86              throw new NullPointerException("binaryData");
87          }
88  
89          if ((type & 0x80) == 0) {
90              // If text, data should not contain 0xFF.
91              int delimPos = binaryData.indexOf(
92                      binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
93              if (delimPos >= 0) {
94                  throw new IllegalArgumentException(
95                          "a text frame should not contain 0xFF.");
96              }
97          }
98  
99          this.type = type & 0xFF;
100         this.binaryData = binaryData;
101     }
102 
103     @Override
104     public String toString() {
105         return getClass().getSimpleName() +
106                "(type: " + getType() + ", " + "data: " + getBinaryData() + ')';
107     }
108 }