why should we concerning about compacting the buffer after writing its contents thoroughly?(1) That code isn't guaranteed to 'write the contents thoroughly'.
Is it not true that the write() method above, should drain the whole buffer contentsNo.
and write its contents to the output channel?As much as it can, yes.
Then why should we concern about the remaining data?Because it hasn't been written yet.
Is it not fully drained?Not necessarily. That's why write() returns a count.
Where does this remaining data came from?From the read().