@@ -38,8 +38,9 @@ public class NettyWebSocket implements WebSocket {
38
38
private final Channel channel ;
39
39
private final ConcurrentLinkedQueue <WebSocketListener > listeners = new ConcurrentLinkedQueue <WebSocketListener >();
40
40
41
- private StringBuilder textBuffer ;
42
- private ByteArrayOutputStream byteBuffer ;
41
+ private final StringBuilder textBuffer = new StringBuilder ();
42
+ private final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream ();
43
+
43
44
private int maxBufferSize = 128000000 ;
44
45
45
46
public NettyWebSocket (Channel channel ) {
@@ -129,66 +130,87 @@ public void close(int statusCode, String reason) {
129
130
}
130
131
131
132
protected void onBinaryFragment (byte [] message , boolean last ) {
133
+
134
+ if (!last ) {
135
+ try {
136
+ byteBuffer .write (message );
137
+ } catch (Exception ex ) {
138
+ byteBuffer .reset ();
139
+ onError (ex );
140
+ return ;
141
+ }
142
+
143
+ if (byteBuffer .size () > maxBufferSize ) {
144
+ byteBuffer .reset ();
145
+ Exception e = new Exception ("Exceeded Netty Web Socket maximum buffer size of " + getMaxBufferSize ());
146
+ onError (e );
147
+ this .close ();
148
+ return ;
149
+ }
150
+ }
151
+
132
152
for (WebSocketListener l : listeners ) {
133
153
if (l instanceof WebSocketByteListener ) {
134
154
try {
135
- WebSocketByteListener .class .cast (l ).onFragment (message ,last );
136
-
137
- if (byteBuffer == null ) {
138
- byteBuffer = new ByteArrayOutputStream ();
139
- }
140
-
141
- byteBuffer .write (message );
142
-
143
- if (byteBuffer .size () > maxBufferSize ) {
144
- Exception e = new Exception ("Exceeded Netty Web Socket maximum buffer size of " + getMaxBufferSize ());
145
- l .onError (e );
146
- this .close ();
147
- return ;
148
- }
149
-
150
-
151
- if (last ) {
152
- WebSocketByteListener .class .cast (l ).onMessage (byteBuffer .toByteArray ());
153
- byteBuffer = null ;
154
- textBuffer = null ;
155
- }
155
+ if (!last ) {
156
+ WebSocketByteListener .class .cast (l ).onFragment (message , last );
157
+ } else {
158
+ if (byteBuffer .size () > 0 ) {
159
+ byteBuffer .write (message );
160
+ WebSocketByteListener .class .cast (l ).onFragment (message , last );
161
+ WebSocketByteListener .class .cast (l ).onMessage (byteBuffer .toByteArray ());
162
+ } else {
163
+ WebSocketByteListener .class .cast (l ).onMessage (message );
164
+ }
165
+ }
156
166
} catch (Exception ex ) {
157
167
l .onError (ex );
158
168
}
159
169
}
160
170
}
171
+
172
+ if (last ) {
173
+ byteBuffer .reset ();
174
+ }
161
175
}
162
176
163
177
protected void onTextFragment (String message , boolean last ) {
178
+
179
+ if (!last ) {
180
+ textBuffer .append (message );
181
+
182
+ if (textBuffer .length () > maxBufferSize ) {
183
+ textBuffer .setLength (0 );
184
+ Exception e = new Exception ("Exceeded Netty Web Socket maximum buffer size of " + getMaxBufferSize ());
185
+ onError (e );
186
+ this .close ();
187
+ return ;
188
+ }
189
+ }
190
+
164
191
for (WebSocketListener l : listeners ) {
165
192
if (l instanceof WebSocketTextListener ) {
166
193
try {
167
- WebSocketTextListener .class .cast (l ).onFragment (message ,last );
168
-
169
- if (textBuffer == null ) {
170
- textBuffer = new StringBuilder ();
171
- }
172
-
173
- textBuffer .append (message );
174
-
175
- if (textBuffer .length () > maxBufferSize ) {
176
- Exception e = new Exception ("Exceeded Netty Web Socket maximum buffer size of " + getMaxBufferSize ());
177
- l .onError (e );
178
- this .close ();
179
- return ;
180
- }
181
-
182
- if (last ) {
183
- WebSocketTextListener .class .cast (l ).onMessage (textBuffer .toString ());
184
- byteBuffer = null ;
185
- textBuffer = null ;
186
- }
194
+ if (!last ) {
195
+ WebSocketTextListener .class .cast (l ).onFragment (message , last );
196
+ } else {
197
+ if (textBuffer .length () > 0 ) {
198
+ WebSocketTextListener .class .cast (l ).onFragment (message , last );
199
+
200
+ WebSocketTextListener .class .cast (l ).onMessage (textBuffer .append (message ).toString ());
201
+ } else {
202
+ WebSocketTextListener .class .cast (l ).onMessage (message );
203
+ }
204
+ }
187
205
} catch (Exception ex ) {
188
206
l .onError (ex );
189
207
}
190
208
}
191
209
}
210
+
211
+ if (last ) {
212
+ textBuffer .setLength (0 );
213
+ }
192
214
}
193
215
194
216
protected void onError (Throwable t ) {
0 commit comments