void writeStreams ( ) { OkHttpClientStream [ ] streams = transport . getActiveStreams ( ) ; int connectionWindow = connectionState . window ( ) ; for ( int numStreams = streams . length ; numStreams > 0 && connectionWindow > 0 ; ) { int nextNumStreams = 0 ; int windowSlice = ( int ) ceil ( connectionWindow / ( float ) numStreams ) ; for ( int index = 0 ; index < numStreams && connectionWindow > 0 ; ++ index ) { OkHttpClientStream stream = streams [ index ] ; OutboundFlowState state = state ( stream ) ; int bytesForStream = min ( connectionWindow , min ( state . unallocatedBytes ( ) , windowSlice ) ) ; if ( bytesForStream > 0 ) { state . allocateBytes ( bytesForStream ) ; connectionWindow -= bytesForStream ; } if ( state . unallocatedBytes ( ) > 0 ) { // There is more data to process for this stream. Add it to the next // pass. streams [ nextNumStreams ++ ] = stream ; } } numStreams = nextNumStreams ; } // Now take one last pass through all of the streams and write any allocated bytes. WriteStatus writeStatus = new WriteStatus ( ) ; for ( OkHttpClientStream stream : transport . getActiveStreams ( ) ) { OutboundFlowState state = state ( stream ) ; state . writeBytes ( state . allocatedBytes ( ) , writeStatus ) ; state . clearAllocatedBytes ( ) ; } if ( writeStatus . hasWritten ( ) ) { flush ( ) ; } }
Writes as much data for all the streams as possible given the current flow control windows .