public static WsByteBuffer [ ] compressBuffers ( WsByteBuffer [ ] src , boolean releaseEmpty ) { List < WsByteBuffer > output = new LinkedList < WsByteBuffer > ( ) ; boolean first = true ; int size = 0 ; for ( int i = 0 ; i < src . length ; i ++ ) { if ( null == src [ i ] ) { continue ; } // if we have data, then save it. If it's the first buffer found // with data, then slice just the remaining data if ( 0 < src [ i ] . remaining ( ) ) { if ( first ) { output . add ( src [ i ] . slice ( ) ) ; src [ i ] . release ( ) ; first = false ; } else { output . add ( src [ i ] ) ; } size ++ ; } else if ( releaseEmpty ) { src [ i ] . release ( ) ; } } if ( 0 == size ) { return null ; } WsByteBuffer [ ] data = new WsByteBuffer [ size ] ; output . toArray ( data ) ; return data ; }
Take the source buffers and extract all non - null non - empty buffers into a new list . If the releaseEmpty flag is true then it will release the empty buffers .