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 java.io.UnsupportedEncodingException;
19
20 import org.jboss.netty.buffer.ChannelBuffer;
21 import org.jboss.netty.buffer.ChannelBuffers;
22 import org.jboss.netty.util.CharsetUtil;
23
24
25
26
27 public class CloseWebSocketFrame extends WebSocketFrame {
28
29
30
31 public CloseWebSocketFrame() {
32 setBinaryData(ChannelBuffers.EMPTY_BUFFER);
33 }
34
35
36
37
38
39
40
41
42
43
44 public CloseWebSocketFrame(int statusCode, String reasonText) {
45 this(true, 0, statusCode, reasonText);
46 }
47
48
49
50
51
52
53
54
55
56 public CloseWebSocketFrame(boolean finalFragment, int rsv) {
57 this(finalFragment, rsv, null);
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public CloseWebSocketFrame(boolean finalFragment, int rsv, int statusCode, String reasonText) {
74 setFinalFragment(finalFragment);
75 setRsv(rsv);
76
77 byte[] reasonBytes = new byte[0];
78 if (reasonText != null) {
79 try {
80 reasonBytes = reasonText.getBytes("UTF-8");
81 } catch (UnsupportedEncodingException e) {
82
83 reasonBytes = reasonText.getBytes();
84 }
85 }
86
87 ChannelBuffer binaryData = ChannelBuffers.buffer(2 + reasonBytes.length);
88 binaryData.writeShort(statusCode);
89 if (reasonBytes.length > 0) {
90 binaryData.writeBytes(reasonBytes);
91 }
92
93 binaryData.readerIndex(0);
94 setBinaryData(binaryData);
95 }
96
97
98
99
100
101
102
103
104
105
106
107 public CloseWebSocketFrame(boolean finalFragment, int rsv, ChannelBuffer binaryData) {
108 setFinalFragment(finalFragment);
109 setRsv(rsv);
110 if (binaryData == null) {
111 setBinaryData(ChannelBuffers.EMPTY_BUFFER);
112 } else {
113 setBinaryData(binaryData);
114 }
115 }
116
117
118
119
120
121 public int getStatusCode() {
122 ChannelBuffer binaryData = getBinaryData();
123 if (binaryData == null || binaryData.capacity() == 0) {
124 return -1;
125 }
126
127 binaryData.readerIndex(0);
128 int statusCode = binaryData.readShort();
129 binaryData.readerIndex(0);
130
131 return statusCode;
132 }
133
134
135
136
137
138 public String getReasonText() {
139 ChannelBuffer binaryData = getBinaryData();
140 if (binaryData == null || binaryData.capacity() <= 2) {
141 return "";
142 }
143
144 binaryData.readerIndex(2);
145 String reasonText = binaryData.toString(CharsetUtil.UTF_8);
146 binaryData.readerIndex(0);
147
148 return reasonText;
149 }
150
151 @Override
152 public String toString() {
153 return getClass().getSimpleName();
154 }
155 }