java.lang.Object
org.klomp.snark.web.InclusiveByteRange

public class InclusiveByteRangeextends Object
Byte range inclusive of end points.

  parses the following types of byte ranges:

      bytes=100-499
      bytes=-300
      bytes=100-
      bytes=1-2,2-3,6-,-2

  given an entity length, converts range to string

      bytes 100-499/500

Based on RFC2616 3.12, 14.16, 14.35.1, 14.35.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) long
    The first byte position.
    (package private) long
    The last byte position.
  • Constructor Summary

    Constructors
    Constructor
    Description
    InclusiveByteRange(long first, long last)
    Creates a new byte range.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the first byte position in this range.
    long
    getFirst(long size)
    Returns the first byte position, resolved against the total entity size.
    long
    Returns the last byte position in this range.
    long
    getLast(long size)
    Returns the last byte position, resolved against the total entity size.
    long
    getSize(long size)
    Returns the number of bytes in this range, resolved against the total entity size.
    satisfiableRanges(Enumeration<?> headers, long size)
    Parse the Range header fields into a list of byte ranges.
    static String
    Formats a 416 Range Not Satisfiable response header value.
    Formats this range as an HTTP Content-Range header value.
    Returns a string representation of this range in the form first:last.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • first

      long first
      The first byte position.
    • last

      long last
      The last byte position.
  • Constructor Details

    • InclusiveByteRange

      public InclusiveByteRange(long first, long last)
      Creates a new byte range.
      Parameters:
      first - the first byte in the range (inclusive), or negative for suffix ranges
      last - the last byte in the range (inclusive), or negative for suffix ranges
  • Method Details

    • getFirst

      public long getFirst()
      Returns the first byte position in this range.
      Returns:
      the first byte position (may be negative for suffix ranges)
    • getLast

      public long getLast()
      Returns the last byte position in this range.
      Returns:
      the last byte position (may be negative for suffix ranges)
    • satisfiableRanges

      public static List<InclusiveByteRange> satisfiableRanges(Enumeration<?> headers, long size)
      Parse the Range header fields into a list of byte ranges.
      Parameters:
      headers - Enumeration of Range header fields.
      size - Size of the resource.
      Returns:
      List of satisfiable ranges
    • getFirst

      public long getFirst(long size)
      Returns the first byte position, resolved against the total entity size. For suffix ranges (negative first), returns max(0, size - last).
      Parameters:
      size - the total size of the entity
      Returns:
      the resolved first byte position
    • getLast

      public long getLast(long size)
      Returns the last byte position, resolved against the total entity size. Clamps to size - 1 for open-ended or out-of-bounds ranges.
      Parameters:
      size - the total size of the entity
      Returns:
      the resolved last byte position
    • getSize

      public long getSize(long size)
      Returns the number of bytes in this range, resolved against the total entity size.
      Parameters:
      size - the total size of the entity
      Returns:
      the number of bytes in the range (always >= 1)
    • toHeaderRangeString

      public String toHeaderRangeString(long size)
      Formats this range as an HTTP Content-Range header value. Returns a string of the form bytes first-last/size.
      Parameters:
      size - the total size of the entity
      Returns:
      the Content-Range header value
    • to416HeaderRangeString

      public static String to416HeaderRangeString(long size)
      Formats a 416 Range Not Satisfiable response header value. Returns a string of the form "bytes star/star-size".
      Parameters:
      size - the total size of the entity
      Returns:
      the Content-Range header value for a 416 response
    • toString

      public String toString()
      Returns a string representation of this range in the form first:last.
      Overrides:
      toString in class Object
      Returns:
      string representation of this range