public class

BytesRange

extends Object
java.lang.Object
   ↳ com.facebook.imagepipeline.common.BytesRange

Class Overview

A representation of the range of bytes requested or contained in a piece of content.

This is based on the spec of the HTTP "Content-Range" header so includes methods to parse and output appropriate strings for the header.

The header spec is at https://tools.ietf.org/html/rfc2616#section-14.16

As per that spec, the from and to values are inclusive. Requesting the first 100 bytes of an image can be achieved by calling BytesRange.toMax(99).

This might be useful because you want to limit the maximum size of a specific download or it might be that you already have part of the image data and only want the remainder.

These objects are currently only respected in image requests and only taken from responses if ImagePipelineExperiments.isPartialImageCachingEnabled() is true in the image pipeline config. It is also dependent on a NetworkFetcher which writes and reads these headers.

Summary

Constants
int TO_END_OF_CONTENT
Fields
public final int from The first byte of the range.
public final int to The final byte of the range inclusive, or TO_END_OF_CONTENT if it reaches to the end.
Public Constructors
BytesRange(int from, int to)
Public Methods
boolean contains(BytesRange compare)
Checks whether a provided range is within this one.
boolean equals(Object other)
static BytesRange from(int from)
Specifies a bytes range to request only the bytes from a specified index up to the end of the data.
static BytesRange fromContentRangeHeader(String header)
Creates an instance of BytesRange by parsing the value of a returned HTTP "Content-Range" header.
int hashCode()
String toHttpRangeHeaderValue()
static BytesRange toMax(int to)
Specifies a bytes range to request no more than a specified number of bytes.
String toString()
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int TO_END_OF_CONTENT

Constant Value: 2147483647 (0x7fffffff)

Fields

public final int from

The first byte of the range. Values begin at 0.

public final int to

The final byte of the range inclusive, or TO_END_OF_CONTENT if it reaches to the end.

If not TO_END_OF_CONTENT, values are inclusive. e.g. for the first 100 bytes this is 99.

Public Constructors

public BytesRange (int from, int to)

Public Methods

public boolean contains (BytesRange compare)

Checks whether a provided range is within this one.

Returns
  • true if the provided range is within this one, false if given null

public boolean equals (Object other)

public static BytesRange from (int from)

Specifies a bytes range to request only the bytes from a specified index up to the end of the data.

Parameters
from the first byte to request, must be positive or zero

public static BytesRange fromContentRangeHeader (String header)

Creates an instance of BytesRange by parsing the value of a returned HTTP "Content-Range" header.

If the range runs to the end of the available content, the end of the range will be set to TO_END_OF_CONTENT.

The header spec is at https://tools.ietf.org/html/rfc2616#section-14.16

Returns
  • the parsed range
Throws
IllegalArgumentException if the header is non-null but fails to match the format per the spec

public int hashCode ()

public String toHttpRangeHeaderValue ()

public static BytesRange toMax (int to)

Specifies a bytes range to request no more than a specified number of bytes. If the encoded image is less than the requested maximum you may still get the entire result.

Parameters
to the maximum byte to be requested, must be positive

public String toString ()