@@ -89,6 +89,11 @@ public enum Role {
8989 * Queue of buffers that need to be sent to the client.
9090 */
9191 private BlockingQueue <ByteBuffer > bufferQueue ;
92+ /**
93+ * The amount of bytes still in queue to be sent, at every given time.
94+ * It's updated at every send/sent operation.
95+ */
96+ private long bufferQueueTotalAmount = 0 ;
9297
9398 private Draft draft = null ;
9499
@@ -470,6 +475,15 @@ boolean hasBufferedData() {
470475 return !this .bufferQueue .isEmpty ();
471476 }
472477
478+ /**
479+ * The amount of data in Queue, ready to be sent.
480+ *
481+ * @return Amount of Data still in Queue and not sent yet of the socket
482+ */
483+ long bufferedDataAmount () {
484+ return bufferQueueTotalAmount ;
485+ }
486+
473487 /**
474488 * Empty the internal buffer, sending all the pending data before continuing.
475489 */
@@ -480,6 +494,10 @@ public void flush() throws IOException {
480494 if ( buffer .remaining () > 0 ) {
481495 continue ;
482496 } else {
497+ synchronized (this ) {
498+ // subtract this amount of data from the total queued (synchronized over this object)
499+ bufferQueueTotalAmount -= buffer .limit ();
500+ }
483501 this .bufferQueue .poll (); // Buffer finished. Remove it.
484502 buffer = this .bufferQueue .peek ();
485503 }
@@ -527,6 +545,10 @@ private void channelWrite( ByteBuffer buf ) throws InterruptedException {
527545 if ( DEBUG )
528546 System .out .println ( "write(" + buf .limit () + "): {" + ( buf .limit () > 1000 ? "too big to display" : new String ( buf .array () ) ) + "}" );
529547 buf .rewind ();
548+ synchronized (this ) {
549+ // add up the number of bytes to the total queued (synchronized over this object)
550+ bufferQueueTotalAmount += buf .limit ();
551+ }
530552 bufferQueue .put ( buf );
531553 wsl .onWriteDemand ( this );
532554 }
0 commit comments