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.BufferAllocator;
20  import io.netty5.util.CharsetUtil;
21  
22  /**
23   * Web Socket continuation frame containing continuation text or binary data. This is used for
24   * fragmented messages where the contents of a messages is contained more than 1 frame.
25   */
26  public class ContinuationWebSocketFrame extends WebSocketFrame {
27      /**
28       * Creates a new continuation frame with the specified binary data. The final fragment flag is
29       * set to true.
30       *
31       * @param binaryData the content of the frame.
32       */
33      public ContinuationWebSocketFrame(Buffer binaryData) {
34          super(binaryData);
35      }
36  
37      /**
38       * Creates a new continuation frame with the specified binary data.
39       *
40       * @param finalFragment flag indicating if this frame is the final fragment
41       * @param rsv reserved bits used for protocol extensions
42       * @param binaryData the content of the frame.
43       */
44      public ContinuationWebSocketFrame(boolean finalFragment, int rsv, Buffer binaryData) {
45          super(finalFragment, rsv, binaryData);
46      }
47  
48      /**
49       * Creates a new continuation frame with the specified text data
50       *
51       * @param allocator {@link BufferAllocator} to use for allocating data.
52       * @param finalFragment flag indicating if this frame is the final fragment
53       * @param rsv reserved bits used for protocol extensions
54       * @param text text content of the frame.
55       */
56      public ContinuationWebSocketFrame(BufferAllocator allocator, boolean finalFragment, int rsv, String text) {
57          this(finalFragment, rsv, fromText(allocator, text));
58      }
59  
60      private ContinuationWebSocketFrame(ContinuationWebSocketFrame copyFrom, Buffer data) {
61          super(copyFrom, data);
62      }
63  
64      /**
65       * Returns the text data in this frame.
66       */
67      public String text() {
68          return binaryData().toString(CharsetUtil.UTF_8);
69      }
70  
71      /**
72       * Sets the string for this frame.
73       *
74       * @param text text to store.
75       */
76      private static Buffer fromText(BufferAllocator allocator, String text) {
77          if (text == null || text.isEmpty()) {
78              return allocator.allocate(0);
79          } else {
80              return allocator.copyOf(text.getBytes(CharsetUtil.UTF_8));
81          }
82      }
83  
84      @Override
85      protected WebSocketFrame receive(Buffer buf) {
86          return new ContinuationWebSocketFrame(this, buf);
87      }
88  }