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