public static long transfer ( final ReadableByteChannel source , final long count , final ByteBuffer throughBuffer , final WritableByteChannel sink ) throws IOException { long total = 0L ; while ( total < count ) { throughBuffer . clear ( ) ; if ( count - total < throughBuffer . remaining ( ) ) { throughBuffer . limit ( ( int ) ( count - total ) ) ; } try { long res = source . read ( throughBuffer ) ; if ( res <= 0 ) { return total == 0L ? res : total ; } } finally { throughBuffer . flip ( ) ; } while ( throughBuffer . hasRemaining ( ) ) { long res = sink . write ( throughBuffer ) ; if ( res <= 0 ) { return total ; } total += res ; } } return total ; }
Transfer the data from the source to the sink using the given through buffer to pass data through .