1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http.websocket;
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
26
27
28
29 public class DefaultWebSocketFrame implements WebSocketFrame {
30
31 private int type;
32 private ChannelBuffer binaryData;
33
34
35
36
37 public DefaultWebSocketFrame() {
38 this(0, ChannelBuffers.EMPTY_BUFFER);
39 }
40
41
42
43
44 public DefaultWebSocketFrame(String textData) {
45 this(0, ChannelBuffers.copiedBuffer(textData, CharsetUtil.UTF_8));
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public DefaultWebSocketFrame(int type, ChannelBuffer binaryData) {
62 setData(type, binaryData);
63 }
64
65 public int getType() {
66 return type;
67 }
68
69 public boolean isText() {
70 return (getType() & 0x80) == 0;
71 }
72
73 public boolean isBinary() {
74 return !isText();
75 }
76
77 public ChannelBuffer getBinaryData() {
78 return binaryData;
79 }
80
81 public String getTextData() {
82 return getBinaryData().toString(CharsetUtil.UTF_8);
83 }
84
85 public void setData(int type, ChannelBuffer binaryData) {
86 if (binaryData == null) {
87 throw new NullPointerException("binaryData");
88 }
89
90 if ((type & 0x80) == 0) {
91
92 int delimPos = binaryData.indexOf(
93 binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
94 if (delimPos >= 0) {
95 throw new IllegalArgumentException(
96 "a text frame should not contain 0xFF.");
97 }
98 }
99
100 this.type = type & 0xFF;
101 this.binaryData = binaryData;
102 }
103
104 @Override
105 public String toString() {
106 return getClass().getSimpleName() +
107 "(type: " + getType() + ", " + "data: " + getBinaryData() + ')';
108 }
109 }