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.channel.Channel;
    20  import org.jboss.netty.channel.ChannelHandler.Sharable;
    21  import org.jboss.netty.channel.ChannelHandlerContext;
    22  import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
    23
    24  /**
    25   * @deprecated Use <tt>org.jboss.netty.handler.codec.http.websocketx</tt> instead.
    26   *
    27   * Encodes a {@link WebSocketFrame} into a {@link ChannelBuffer}.
    28   * <p>
    29   * For the detailed instruction on adding add Web Socket support to your HTTP
    30   * server, take a look into the <tt>WebSocketServer</tt> example located in the
    31   * {@code org.jboss.netty.example.http.websocket} package.
    32   * @apiviz.landmark
    33   * @apiviz.uses org.jboss.netty.handler.codec.http.websocket.WebSocketFrame
    34   */
    35  @Deprecated
    36  @Sharable
    37  public class WebSocketFrameEncoder extends OneToOneEncoder {
    38
    39      @Override
    40      protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
    41          if (msg instanceof WebSocketFrame) {
    42              WebSocketFrame frame = (WebSocketFrame) msg;
    43              int type = frame.getType();
    44              if (frame.isText()) {
    45                  // Text frame
    46                  ChannelBuffer data = frame.getBinaryData();
    47                  ChannelBuffer encoded =
    48                      channel.getConfig().getBufferFactory().getBuffer(
    49                              data.order(), data.readableBytes() + 2);
    50                  encoded.writeByte((byte) type);
    51                  encoded.writeBytes(data, data.readerIndex(), data.readableBytes());
    52                  encoded.writeByte((byte) 0xFF);
    53                  return encoded;
    54              } else {
    55                  // Binary frame
    56                  ChannelBuffer data = frame.getBinaryData();
    57                  int dataLen = data.readableBytes();
    58                  ChannelBuffer encoded =
    59                      channel.getConfig().getBufferFactory().getBuffer(
    60                              data.order(), dataLen + 5);
    61
    62                  // Encode type.
    63                  encoded.writeByte((byte) type);
    64
    65                  // Encode length.
    66                  int b1 = dataLen >>> 28 & 0x7F;
    67                  int b2 = dataLen >>> 14 & 0x7F;
    68                  int b3 = dataLen >>> 7 & 0x7F;
    69                  int b4 = dataLen & 0x7F;
    70                  if (b1 == 0) {
    71                      if (b2 == 0) {
    72                          if (b3 == 0) {
    73                              encoded.writeByte(b4);
    74                          } else {
    75                              encoded.writeByte(b3 | 0x80);
    76                              encoded.writeByte(b4);
    77                          }
    78                      } else {
    79                          encoded.writeByte(b2 | 0x80);
    80                          encoded.writeByte(b3 | 0x80);
    81                          encoded.writeByte(b4);
    82                      }
    83                  } else {
    84                      encoded.writeByte(b1 | 0x80);
    85                      encoded.writeByte(b2 | 0x80);
    86                      encoded.writeByte(b3 | 0x80);
    87                      encoded.writeByte(b4);
    88                  }
    89
    90                  // Encode binary data.
    91                  encoded.writeBytes(data, data.readerIndex(), dataLen);
    92                  return encoded;
    93              }
    94          }
    95          return msg;
    96      }
    97  }