public void parseRange ( ) throws RangeNotSatisfiableException , IOException { if ( requestedRanges . length > 1 ) { throw new RangeNotSatisfiableException ( origResource , requestedRanges , "Multiple ranges are not supported yet" ) ; } final long [ ] firstRange = requestedRanges [ 0 ] ; long start = firstRange [ 0 ] ; long stop = firstRange [ 1 ] ; long [ ] availRange = availableRange ( ) ; if ( availRange == null ) { throw new RangeNotSatisfiableException ( origResource , requestedRanges , "available range cannot be determined" ) ; } if ( start < 0 ) { // tail range (-N) - translate to absolute positions start = availRange [ 2 ] + start ; stop = availRange [ 2 ] ; } if ( stop < 0 ) { // M- stop = availRange [ 2 ] ; } if ( start < availRange [ 0 ] || stop > availRange [ 1 ] ) { // requested range is not satisfiable with content available // in the resource. // TODO: if availRange[1] == file length, stop > availRange[1] // is okay. We just replay start..availRange[1]. Guessing it // must be extremely rare to have such capture. throw new RangeNotSatisfiableException ( origResource , requestedRanges , "requested range is not available in this capture" ) ; } if ( start > availRange [ 0 ] ) { origResource . skip ( start - availRange [ 0 ] ) ; } outputLength = stop - start ; setInputStream ( ByteStreams . limit ( origResource , outputLength ) ) ; httpHeaders = new HashMap < String , String > ( ) ; httpHeaders . putAll ( origResource . getHttpHeaders ( ) ) ; // TODO: Add Content-Range String contentRange = String . format ( "bytes %d-%d/%d" , start , stop - 1 , availRange [ 2 ] ) ; HttpHeaderOperation . replaceHeader ( httpHeaders , HttpHeaderOperation . HTTP_CONTENT_RANGE_HEADER , contentRange ) ; HttpHeaderOperation . replaceHeader ( httpHeaders , HttpHeaderOperation . HTTP_LENGTH_HEADER , Long . toString ( stop - start ) ) ; }
Prepare response for requested range .