@@ -79,15 +79,15 @@ public final class WebSocket {
7979 * The bytes that make up the current text frame being read.
8080 */
8181 private ByteBuffer currentFrame ;
82- /**
83- * Queue of buffers that need to be sent to the client.
84- */
85- private BlockingQueue <ByteBuffer > bufferQueue ;
86- /**
87- * Lock object to ensure that data is sent from the bufferQueue in
88- * the proper order
89- */
90- private Object bufferQueueMutex = new Object ();
82+ /**
83+ * Queue of buffers that need to be sent to the client.
84+ */
85+ private BlockingQueue <ByteBuffer > bufferQueue ;
86+ /**
87+ * Lock object to ensure that data is sent from the bufferQueue in
88+ * the proper order
89+ */
90+ private Object bufferQueueMutex = new Object ();
9191
9292
9393 // CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -96,16 +96,16 @@ public final class WebSocket {
9696 * @param socketChannel The <tt>SocketChannel</tt> instance to read and
9797 * write to. The channel should already be registered
9898 * with a Selector before construction of this object.
99- * @param bufferQueue The Queue that we should use to buffer data that
100- * hasn't been sent to the client yet.
99+ * @param bufferQueue The Queue that we should use to buffer data that
100+ * hasn't been sent to the client yet.
101101 * @param listener The {@link WebSocketListener} to notify of events when
102102 * they occur.
103103 */
104104 WebSocket (SocketChannel socketChannel , BlockingQueue <ByteBuffer > bufferQueue ,
105- WebSocketListener listener )
106- {
105+ WebSocketListener listener )
106+ {
107107 this .socketChannel = socketChannel ;
108- this .bufferQueue = bufferQueue ;
108+ this .bufferQueue = bufferQueue ;
109109 this .handshakeComplete = false ;
110110 this .remoteHandshake = this .currentFrame = null ;
111111 this .buffer = ByteBuffer .allocate (1 );
@@ -150,10 +150,10 @@ public void close() throws IOException {
150150 this .wsl .onClose (this );
151151 }
152152
153- /**
154- * @return True if all of the text was sent to the client by this thread.
155- * False if some of the text had to be buffered to be sent later.
156- */
153+ /**
154+ * @return True if all of the text was sent to the client by this thread.
155+ * False if some of the text had to be buffered to be sent later.
156+ */
157157 public boolean send (String text ) throws IOException {
158158 if (!this .handshakeComplete ) throw new NotYetConnectedException ();
159159 if (text == null ) throw new NullPointerException ("Cannot send 'null' data to a WebSocket." );
@@ -164,49 +164,49 @@ public boolean send(String text) throws IOException {
164164 b .put (START_OF_FRAME );
165165 b .put (textBytes );
166166 b .put (END_OF_FRAME );
167- b .rewind ();
167+ b .rewind ();
168168
169- // See if we have any backlog that needs to be sent first
170- if (handleWrite ()) {
171- // Write the ByteBuffer to the socket
172- this .socketChannel .write (b );
173- }
169+ // See if we have any backlog that needs to be sent first
170+ if (handleWrite ()) {
171+ // Write the ByteBuffer to the socket
172+ this .socketChannel .write (b );
173+ }
174174
175- // If we didn't get it all sent, add it to the buffer of buffers
176- if (b .remaining () > 0 ) {
177- if (!this .bufferQueue .offer (b )) {
178- throw new IOException ("Buffers are full, message could not be sent to" +
179- this .socketChannel .socket ().getRemoteSocketAddress ());
180- }
181- return false ;
182- }
175+ // If we didn't get it all sent, add it to the buffer of buffers
176+ if (b .remaining () > 0 ) {
177+ if (!this .bufferQueue .offer (b )) {
178+ throw new IOException ("Buffers are full, message could not be sent to" +
179+ this .socketChannel .socket ().getRemoteSocketAddress ());
180+ }
181+ return false ;
182+ }
183183
184- return true ;
184+ return true ;
185185 }
186186
187- boolean hasBufferedData () {
188- return !this .bufferQueue .isEmpty ();
189- }
187+ boolean hasBufferedData () {
188+ return !this .bufferQueue .isEmpty ();
189+ }
190190
191- /**
192- * @return True if all data has been sent to the client, false if there
193- * is still some buffered.
194- */
195- boolean handleWrite () throws IOException {
196- synchronized (this .bufferQueueMutex ) {
197- ByteBuffer buffer = this .bufferQueue .peek ();
198- while (buffer != null ) {
199- this .socketChannel .write (buffer );
200- if (buffer .remaining () > 0 ) {
201- return false ; // Didn't finish this buffer. There's more to send.
202- } else {
203- this .bufferQueue .poll (); // Buffer finished. Remove it.
204- buffer = this .bufferQueue .peek ();
205- }
206- }
207- return true ;
208- }
209- }
191+ /**
192+ * @return True if all data has been sent to the client, false if there
193+ * is still some buffered.
194+ */
195+ boolean handleWrite () throws IOException {
196+ synchronized (this .bufferQueueMutex ) {
197+ ByteBuffer buffer = this .bufferQueue .peek ();
198+ while (buffer != null ) {
199+ this .socketChannel .write (buffer );
200+ if (buffer .remaining () > 0 ) {
201+ return false ; // Didn't finish this buffer. There's more to send.
202+ } else {
203+ this .bufferQueue .poll (); // Buffer finished. Remove it.
204+ buffer = this .bufferQueue .peek ();
205+ }
206+ }
207+ return true ;
208+ }
209+ }
210210
211211 public SocketChannel socketChannel () {
212212 return this .socketChannel ;
@@ -254,16 +254,16 @@ private void recieveHandshake() throws IOException, NoSuchAlgorithmException {
254254 && h [h .length -19 ] == LF
255255 && h [h .length -18 ] == CR
256256 && h [h .length -17 ] == LF )){
257- byte [] handShakeBody = new byte [16 ];
258- handShakeBody [0 ]=h [h .length -16 ];
257+ byte [] handShakeBody = new byte [16 ];
258+ handShakeBody [0 ]=h [h .length -16 ];
259259 handShakeBody [1 ]=h [h .length -15 ];
260260 handShakeBody [2 ]=h [h .length -14 ];
261261 handShakeBody [3 ]=h [h .length -13 ];
262262 handShakeBody [4 ]=h [h .length -12 ];
263263 handShakeBody [5 ]=h [h .length -11 ];
264264 handShakeBody [6 ]=h [h .length -10 ];
265265 handShakeBody [7 ]=h [h .length -9 ];
266- handShakeBody [8 ]=h [h .length -8 ];
266+ handShakeBody [8 ]=h [h .length -8 ];
267267 handShakeBody [9 ]=h [h .length -7 ];
268268 handShakeBody [10 ]=h [h .length -6 ];
269269 handShakeBody [11 ]=h [h .length -5 ];
@@ -281,8 +281,8 @@ else if ((h.length>=12 && h[h.length-12] == CR
281281 && h [h .length -11 ] == LF
282282 && h [h .length -10 ] == CR
283283 && h [h .length -9 ] == LF ) && new String (this .remoteHandshake .array (), UTF8_CHARSET ).contains ("Sec-WebSocket-Key1" )) {
284- byte [] handShakeBody = new byte [8 ];
285- handShakeBody [0 ]=h [h .length -8 ];
284+ byte [] handShakeBody = new byte [8 ];
285+ handShakeBody [0 ]=h [h .length -8 ];
286286 handShakeBody [1 ]=h [h .length -7 ];
287287 handShakeBody [2 ]=h [h .length -6 ];
288288 handShakeBody [3 ]=h [h .length -5 ];
@@ -299,13 +299,13 @@ else if ((h.length>=12 && h[h.length-12] == CR
299299 && h [h .length -2 ] == CR
300300 && h [h .length -1 ] == LF ) && !(new String (this .remoteHandshake .array (), UTF8_CHARSET ).contains ("Sec" )) ||
301301 (h .length ==23 && h [h .length -1 ] == 0 ) ) {
302-
303- completeHandshake (null );
304- }
302+
303+ completeHandshake (null );
304+ }
305305 }
306306
307307 private void completeHandshake (byte [] handShakeBody ) throws IOException , NoSuchAlgorithmException {
308- byte [] handshakeBytes = this .remoteHandshake .array ();
308+ byte [] handshakeBytes = this .remoteHandshake .array ();
309309 String handshake = new String (handshakeBytes , UTF8_CHARSET );
310310 this .handshakeComplete = true ;
311311 if (this .wsl .onHandshakeRecieved (this , handshake , handShakeBody )) {
0 commit comments