private void fillBuffer ( long amount , boolean reload ) { try { if ( amount > bufferSize ) { amount = bufferSize ; } log . debug ( "Buffering amount: {} buffer size: {}" , amount , bufferSize ) ; // Read all remaining bytes if the requested amount reach the end of channel if ( channelSize - channel . position ( ) < amount ) { amount = channelSize - channel . position ( ) ; } if ( in == null ) { switch ( bufferType ) { case HEAP : in = IoBuffer . allocate ( bufferSize , false ) ; break ; case DIRECT : in = IoBuffer . allocate ( bufferSize , true ) ; break ; default : in = IoBuffer . allocate ( bufferSize ) ; } channel . read ( in . buf ( ) ) ; in . flip ( ) ; useLoadBuf = true ; } if ( ! useLoadBuf ) { return ; } if ( reload || in . remaining ( ) < amount ) { if ( ! reload ) { in . compact ( ) ; } else { in . clear ( ) ; } channel . read ( in . buf ( ) ) ; in . flip ( ) ; } } catch ( Exception e ) { log . error ( "Error fillBuffer" , e ) ; } }
Load enough bytes from channel to buffer . After the loading process the caller can make sure the amount in buffer is of size amount if we haven t reached the end of channel .