public final synchronized void flush ( ) throws Exception { List < Long > sortedKeys = new ArrayList < Long > ( buffer . keySet ( ) ) ; Collections . sort ( sortedKeys ) ; while ( sortedKeys . size ( ) > 0 ) { long firstKey = sortedKeys . get ( 0 ) ; int firstDataLength = buffer . get ( firstKey ) . length ; int i = 1 ; while ( i < sortedKeys . size ( ) && sortedKeys . get ( i ) == sortedKeys . get ( i - 1 ) + ( firstDataLength / getBlockSize ( ) ) ) { i ++ ; } byte [ ] data = new byte [ firstDataLength * i ] ; for ( int j = 0 ; j < i ; j ++ ) { System . arraycopy ( buffer . get ( sortedKeys . get ( j ) ) , 0 , data , j * firstDataLength , firstDataLength ) ; } device . write ( firstKey , data ) ; if ( sortedKeys . size ( ) != 1 ) { sortedKeys = sortedKeys . subList ( i , sortedKeys . size ( ) ) ; } else { sortedKeys . clear ( ) ; } } buffer . clear ( ) ; }
Flush the buffer to the target .