public static void flipBuffers ( WsByteBuffer [ ] buffers , int totalSize ) { int size = 0 ; boolean overLimit = false ; for ( int i = 0 ; i < buffers . length && null != buffers [ i ] ; i ++ ) { if ( overLimit ) { buffers [ i ] . limit ( buffers [ i ] . position ( ) ) ; } else { buffers [ i ] . flip ( ) ; size += buffers [ i ] . remaining ( ) ; overLimit = ( size >= totalSize ) ; } } }
Flip the input list of buffers walking through the list until the flipped amount equals the input total size mark the rest of the buffers as empty .