View Javadoc

    1   /*
    2    * Copyright 2010 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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   * The default {@link WebSocketFrame} implementation.
    24   *
    25   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    26   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    27   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    28   */
    29  public class DefaultWebSocketFrame implements WebSocketFrame {
    30
    31      private int type;
    32      private ChannelBuffer binaryData;
    33
    34      /**
    35       * Creates a new empty text frame.
    36       */
    37      public DefaultWebSocketFrame() {
    38          this(0, ChannelBuffers.EMPTY_BUFFER);
    39      }
    40
    41      /**
    42       * Creates a new text frame from with the specified string.
    43       */
    44      public DefaultWebSocketFrame(String textData) {
    45          this(0, ChannelBuffers.copiedBuffer(textData, CharsetUtil.UTF_8));
    46      }
    47
    48      /**
    49       * Creates a new frame with the specified frame type and the specified data.
    50       *
    51       * @param type
    52       *        the type of the frame. {@code 0} is the only allowed type currently.
    53       * @param binaryData
    54       *        the content of the frame.  If <tt>(type &amp; 0x80 == 0)</tt>,
    55       *        it must be encoded in UTF-8.
    56       *
    57       * @throws IllegalArgumentException
    58       *         if If <tt>(type &amp; 0x80 == 0)</tt> and the data is not encoded
    59       *         in UTF-8
    60       */
    61      public DefaultWebSocketFrame(int type, ChannelBuffer binaryData) {
    62          setData(type, binaryData);
    63      }
    64
    65      public int getType() {
    66          return type;
    67      }
    68
    69      public boolean isText() {
    70          return (getType() & 0x80) == 0;
    71      }
    72
    73      public boolean isBinary() {
    74          return !isText();
    75      }
    76
    77      public ChannelBuffer getBinaryData() {
    78          return binaryData;
    79      }
    80
    81      public String getTextData() {
    82          return getBinaryData().toString(CharsetUtil.UTF_8);
    83      }
    84
    85      public void setData(int type, ChannelBuffer binaryData) {
    86          if (binaryData == null) {
    87              throw new NullPointerException("binaryData");
    88          }
    89
    90          if ((type & 0x80) == 0) {
    91              // If text, data should not contain 0xFF.
    92              int delimPos = binaryData.indexOf(
    93                      binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
    94              if (delimPos >= 0) {
    95                  throw new IllegalArgumentException(
    96                          "a text frame should not contain 0xFF.");
    97              }
    98          }
    99
    100         this.type = type & 0xFF;
    101         this.binaryData = binaryData;
    102     }
    103
    104     @Override
    105     public String toString() {
    106         return getClass().getSimpleName() +
    107                "(type: " + getType() + ", " + "data: " + getBinaryData() + ')';
    108     }
    109 }