@@ -208,6 +208,10 @@ public enum Draft {
208
208
209
209
private final WebSocket instance ;
210
210
211
+ private ByteBuffer bigBuffer = ByteBuffer .allocate (1024 * 500 );
212
+ private byte [] tokenByteBuffer = new byte [1024 * 500 ];
213
+ private int tokenByteBufferCounter = 0 ;
214
+
211
215
/**
212
216
* Constructor.
213
217
*
@@ -530,52 +534,76 @@ private void _write(byte[] bytes) throws IOException {
530
534
}
531
535
532
536
private void _read () throws IOException , NoSuchAlgorithmException {
533
- this .buffer .rewind ();
534
-
535
537
int bytesRead = -1 ;
536
538
try {
537
- bytesRead = this .socketChannel .read (this .buffer );
539
+ if (!handshakeComplete ) {
540
+ buffer .rewind ();
541
+ bytesRead = socketChannel .read (this .buffer );
542
+ buffer .rewind ();
543
+ } else {
544
+ bigBuffer .rewind ();
545
+ bytesRead = socketChannel .read (this .bigBuffer );
546
+ bigBuffer .rewind ();
547
+ }
548
+
538
549
} catch (Exception ex ) {
550
+ Log .v ("websocket" , "Could not read data from socket channel, ex=" + ex .toString ());
539
551
}
540
552
541
553
if (bytesRead == -1 ) {
554
+ Log .v ("websocket" , "All Bytes readed" );
542
555
close ();
543
556
} else if (bytesRead > 0 ) {
544
- this .buffer .rewind ();
545
-
546
557
if (!this .handshakeComplete ) {
547
558
_readHandshake ();
548
559
} else {
549
- _readFrame ();
560
+ _readFrame (bytesRead );
550
561
}
551
562
}
552
563
}
553
564
554
- private void _readFrame () throws UnsupportedEncodingException {
555
- byte newestByte = this . buffer . get ();
565
+ private void _readFrame (int bytesRead ) throws UnsupportedEncodingException {
566
+ byte [] data = bigBuffer . array ();
556
567
557
- if ( newestByte == DATA_START_OF_FRAME ) { // Beginning of Frame
558
- this . currentFrame = null ;
568
+ int length = bytesRead ;
569
+ if ( length > 2000 ) length = 2000 ;
559
570
560
- } else if (newestByte == DATA_END_OF_FRAME ) { // End of Frame
561
- String textFrame = null ;
562
- // currentFrame will be null if END_OF_FRAME was send directly after
563
- // START_OF_FRAME, thus we will send 'null' as the sent message.
564
- if (this .currentFrame != null ) {
565
- textFrame = new String (this .currentFrame .array (), UTF8_CHARSET .toString ());
566
- }
567
- // fire onMessage method
568
- this .onMessage (textFrame );
569
-
570
- } else { // Regular frame data, add to current frame buffer
571
- ByteBuffer frame = ByteBuffer .allocate ((this .currentFrame != null ? this .currentFrame .capacity () : 0 )
572
- + this .buffer .capacity ());
573
- if (this .currentFrame != null ) {
574
- this .currentFrame .rewind ();
575
- frame .put (this .currentFrame );
576
- }
577
- frame .put (newestByte );
578
- this .currentFrame = frame ;
571
+ Log .v ("websocket" , "_readFrame - bytesRead: " + bytesRead + ", data: " + new String (data , 0 , length ));
572
+
573
+ // Get tokens
574
+ for (int i =0 ; i < bytesRead ; i ++) {
575
+
576
+ byte readByte = data [i ];
577
+
578
+ // Token message is finished
579
+ if (readByte == DATA_END_OF_FRAME ) {
580
+
581
+ // Make token public
582
+ this .onMessage (new String (tokenByteBuffer , 0 , tokenByteBufferCounter ));
583
+
584
+ // Reset counter for byte buffer
585
+ tokenByteBufferCounter = 0 ;
586
+
587
+ // Bytes to read as token message, skip start frame byte
588
+ } else if (readByte != DATA_START_OF_FRAME ) {
589
+ tokenByteBufferCounter ++;
590
+
591
+ // Array is out of bounds, make it greater
592
+ if (tokenByteBufferCounter == tokenByteBuffer .length ) {
593
+
594
+ byte [] newTokenByteBuffer = new byte [tokenByteBuffer .length *2 ];
595
+
596
+ Log .v ("websocket" , "expand token byte buffer, new size=" + newTokenByteBuffer .length );
597
+
598
+ // Copy old data
599
+ for (int i2 =0 ; i2 <tokenByteBuffer .length ; i2 ++) {
600
+ newTokenByteBuffer [i2 ] = tokenByteBuffer [i2 ];
601
+ }
602
+
603
+ tokenByteBuffer = newTokenByteBuffer ;
604
+ }
605
+
606
+ tokenByteBuffer [tokenByteBufferCounter -1 ] = readByte ;
579
607
}
580
608
}
581
609
0 commit comments