code: private boolean validateRange(Range range, long contentLength)
     {
        long start = range.getStart();
        long end = range.getEnd();
  
        // range set as bytes:-100
        // take 100 bytes from end
        if (start < 0 && end == -1)
        {
           if ((-1 * start) >= contentLength)
           {
              start = 0;
              end = contentLength - 1;
           }
           else
           {
              start = contentLength + start;
              end = contentLength - 1;
           }
        }
  
        // range set as bytes:100-
        // take from 100 to the end
        if (start >= 0 && end == -1)
           end = contentLength - 1;
  
        // normal range set as bytes:100-200
        // end can be greater then content-length
        if (end >= contentLength)
           end = contentLength - 1;
  
        if (start >= 0 && end >= 0 && start <= end)
        {
           range.setStart(start);
           range.setEnd(end);
           return true;
        }
        return false;
     }
docs: Checks is the range is valid.
  
  @param range range
  @param contentLength coontent length
  @return true if the range is valid else false