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    *   https://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.netty5.handler.codec.http.websocketx;
17  
18  import io.netty5.buffer.api.Buffer;
19  import io.netty5.buffer.api.BufferHolder;
20  import io.netty5.util.Resource;
21  import io.netty5.util.Send;
22  import io.netty5.util.internal.StringUtil;
23  
24  import static java.nio.charset.Charset.defaultCharset;
25  
26  /**
27   * Base class for web socket frames.
28   */
29  public abstract class WebSocketFrame extends BufferHolder<WebSocketFrame> {
30  
31      /**
32       * Flag to indicate if this frame is the final fragment in a message. The first fragment (frame) may also be the
33       * final fragment.
34       */
35      private final boolean finalFragment;
36  
37      /**
38       * RSV1, RSV2, RSV3 used for extensions
39       */
40      private final int rsv;
41  
42      protected WebSocketFrame(Buffer binaryData) {
43          this(true, 0, binaryData);
44      }
45  
46      protected WebSocketFrame(Send<Buffer> binaryData) {
47          super(binaryData);
48          finalFragment = true;
49          rsv = 0;
50      }
51  
52      protected WebSocketFrame(boolean finalFragment, int rsv, Buffer binaryData) {
53          super(binaryData);
54          this.finalFragment = finalFragment;
55          this.rsv = rsv;
56      }
57  
58      /**
59       * This is a copy-constructor, used by sub-classes to implement {@link Resource#send()}.
60       * The life cycle of the {@code binaryData} buffer is not manipulated by this constructor, but instead stored as-is.
61       *
62       * @param copyFrom The original frame instance to copy from.
63       * @param binaryData The binary data of the original frame.
64       */
65      protected WebSocketFrame(WebSocketFrame copyFrom, Buffer binaryData) {
66          super(binaryData);
67          finalFragment = copyFrom.finalFragment;
68          rsv = copyFrom.rsv;
69      }
70  
71      /**
72       * Flag to indicate if this frame is the final fragment in a message. The first fragment (frame) may also be the
73       * final fragment.
74       */
75      public boolean isFinalFragment() {
76          return finalFragment;
77      }
78  
79      /**
80       * Bits used for extensions to the standard.
81       */
82      public int rsv() {
83          return rsv;
84      }
85  
86      public Buffer binaryData() {
87          return getBuffer();
88      }
89  
90      @Override
91      public String toString() {
92          return StringUtil.simpleClassName(this) + "(data: " + getBuffer().toString(defaultCharset()) + ')';
93      }
94  }