private int getWindowOffset ( long bytePos , int length ) throws IOException { long desiredMax = bytePos + length - 1 ; if ( ( bytePos >= windowStart ) && ( desiredMax < windowStart + buffer . capacity ( ) ) ) { long res = bytePos - windowStart ; if ( res < Integer . MAX_VALUE ) { return ( int ) res ; } else { throw new IOException ( "This buffer is quite large..." ) ; } } else { long bufferCapacity = Math . max ( bufferSize , length ) ; long size = channel . size ( ) ; bufferCapacity = Math . min ( bufferCapacity , size - bytePos ) ; if ( bufferCapacity > Integer . MAX_VALUE ) { throw new IOException ( "Woaw ! You want to have a REALLY LARGE buffer !" ) ; } windowStart = bytePos ; channel . position ( windowStart ) ; if ( buffer . capacity ( ) != bufferCapacity ) { ByteOrder order = buffer . order ( ) ; buffer = ByteBuffer . allocate ( ( int ) bufferCapacity ) ; buffer . order ( order ) ; } else { buffer . clear ( ) ; } channel . read ( buffer ) ; buffer . flip ( ) ; return ( int ) ( bytePos - windowStart ) ; } }
Moves the window if necessary to contain the desired byte and returns the position of the byte in the window