if ( TRACE ){
System.out.println( " swapping " + piece_number + " and " + swap_piece_number + ": " + piece_number + " <-> " + store_index );
}
DirectByteBuffer temp_buffer = DirectByteBufferPool.getBuffer( SS_FILE, piece_size );
DirectByteBuffer[] temp_buffers = new DirectByteBuffer[]{ temp_buffer };
try{
long store_offset = first_piece_length + ((store_index-1)*(long)piece_size );
long swap_offset = first_piece_length + ((piece_number-1)*(long)piece_size );
delegate.read( raf, temp_buffers, swap_offset );
piece_data.position( SS_FILE, 0 );
delegate.write( raf, new DirectByteBuffer[]{ piece_data }, swap_offset );
temp_buffer.position( SS_FILE, 0 );
delegate.write( raf, temp_buffers, store_offset );
piece_map[ piece_number ] = piece_number;
piece_reverse_map[ piece_number ] = piece_number;
piece_map[ swap_piece_number ] = store_index;
piece_reverse_map[ store_index ] = swap_piece_number;
setDirty();
if ( piece_number == num_pieces - 1 ){
long file_length = swap_offset + last_piece_length;
if ( delegate.getLength( raf ) > file_length ){
if ( TRACE ){
System.out.println( " truncating file to correct length of " + file_length );
}
delegate.setLength( raf, file_length );
}
}
}finally{
temp_buffer.returnToPool();
}
}else{
delegate.setPieceComplete( raf, piece_number, piece_data );
}