1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.websocketx;
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
24
25 public class TextWebSocketFrame extends WebSocketFrame {
26
27
28
29
30 public TextWebSocketFrame() {
31 setBinaryData(ChannelBuffers.EMPTY_BUFFER);
32 }
33
34
35
36
37
38
39
40 public TextWebSocketFrame(String text) {
41 if (text == null || text.length() == 0) {
42 setBinaryData(ChannelBuffers.EMPTY_BUFFER);
43 } else {
44 setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
45 }
46 }
47
48
49
50
51
52
53
54 public TextWebSocketFrame(ChannelBuffer binaryData) {
55 setBinaryData(binaryData);
56 }
57
58
59
60
61
62
63
64
65
66
67
68 public TextWebSocketFrame(boolean finalFragment, int rsv, String text) {
69 setFinalFragment(finalFragment);
70 setRsv(rsv);
71 if (text == null || text.length() == 0) {
72 setBinaryData(ChannelBuffers.EMPTY_BUFFER);
73 } else {
74 setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87
88 public TextWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData) {
89 setFinalFragment(finalFragment);
90 setRsv(rsv);
91 setBinaryData(binaryData);
92 }
93
94
95
96
97 public String getText() {
98 if (getBinaryData() == null) {
99 return null;
100 }
101 return getBinaryData().toString(CharsetUtil.UTF_8);
102 }
103
104
105
106
107
108
109
110 public void setText(String text) {
111 if (text == null) {
112 throw new NullPointerException("text");
113 }
114 setBinaryData(ChannelBuffers.copiedBuffer(text, CharsetUtil.UTF_8));
115 }
116
117 @Override
118 public String toString() {
119 return getClass().getSimpleName() + "(text: " + getText() + ')';
120 }
121 }