ParsingLoadable


@UnstableApi
public final class ParsingLoadable<T> implements Loader.Loadable


A Loadable for objects that can be parsed from binary data using a Parser.

Parameters
<T>

The type of the object being loaded.

Summary

Nested types

public interface ParsingLoadable.Parser<T>

Parses an object from loaded data.

Public fields

final DataSpec

The DataSpec that defines the data to be loaded.

final long

Identifies the load task for this loadable.

volatile @Nullable T
final int

The type of the data.

Public constructors

ParsingLoadable(
    DataSource dataSource,
    DataSpec dataSpec,
    int type,
    ParsingLoadable.Parser<T> parser
)
ParsingLoadable(
    DataSource dataSource,
    Uri uri,
    int type,
    ParsingLoadable.Parser<T> parser
)

Public methods

long

Returns the number of bytes loaded.

final void

Cancels the load.

Map<StringList<String>>

Returns the response headers associated with the load.

final @Nullable T

Returns the loaded object, or null if an object has not been loaded.

Uri

Returns the Uri from which data was read.

final void

Performs the load, returning on completion or cancellation.

static T
<T> load(
    DataSource dataSource,
    ParsingLoadable.Parser<T> parser,
    DataSpec dataSpec,
    int type
)

Loads a single parsable object.

static T
<T> load(
    DataSource dataSource,
    ParsingLoadable.Parser<T> parser,
    Uri uri,
    int type
)

Loads a single parsable object.

Public fields

dataSpec

public final DataSpec dataSpec

The DataSpec that defines the data to be loaded.

loadTaskId

public final long loadTaskId

Identifies the load task for this loadable.

result

volatile public @Nullableresult

type

public final int type

The type of the data. One of the DATA_TYPE_* constants defined in C. For reporting only.

Public constructors

ParsingLoadable

public ParsingLoadable(
    DataSource dataSource,
    DataSpec dataSpec,
    int type,
    ParsingLoadable.Parser<T> parser
)
Parameters
DataSource dataSource

A DataSource to use when loading the data.

DataSpec dataSpec

The DataSpec from which the object should be loaded.

int type

See type.

ParsingLoadable.Parser<T> parser

Parses the object from the response.

ParsingLoadable

public ParsingLoadable(
    DataSource dataSource,
    Uri uri,
    int type,
    ParsingLoadable.Parser<T> parser
)
Parameters
DataSource dataSource

A DataSource to use when loading the data.

Uri uri

The Uri from which the object should be loaded.

int type

See type.

ParsingLoadable.Parser<T> parser

Parses the object from the response.

Public methods

bytesLoaded

public long bytesLoaded()

Returns the number of bytes loaded. In the case that the network response was compressed, the value returned is the size of the data after decompression. Must only be called after the load completed, failed, or was canceled.

cancelLoad

public final void cancelLoad()

Cancels the load.

Loadable implementations should ensure that a currently executing load call will exit reasonably quickly after this method is called. The load call may exit either by returning or by throwing an IOException.

If there is a currently executing load call, then the thread on which that call is being made will be interrupted immediately after the call to this method. Hence implementations do not need to (and should not attempt to) interrupt the loading thread themselves.

Although the loading thread will be interrupted, Loadable implementations should not use the interrupted status of the loading thread in load to determine whether the load has been canceled. This approach is not robust [Internal ref: b/79223737]. Instead, implementations should use their own flag to signal cancelation (for example, using ).

getResponseHeaders

public Map<StringList<String>> getResponseHeaders()

Returns the response headers associated with the load. Must only be called after the load completed, failed, or was canceled.

getResult

public final @NullablegetResult()

Returns the loaded object, or null if an object has not been loaded.

getUri

public Uri getUri()

Returns the Uri from which data was read. If redirection occurred, this is the redirected uri. Must only be called after the load completed, failed, or was canceled.

load

public final void load()

Performs the load, returning on completion or cancellation.

Throws
java.io.IOException

If the input could not be loaded.

load

public static T <T> load(
    DataSource dataSource,
    ParsingLoadable.Parser<T> parser,
    DataSpec dataSpec,
    int type
)

Loads a single parsable object.

Parameters
DataSource dataSource

The DataSource through which the object should be read.

ParsingLoadable.Parser<T> parser

The Parser to parse the object from the response.

DataSpec dataSpec

The DataSpec of the object to read.

int type

The type of the data. One of the CDATA_TYPE_* constants.

Returns
T

The parsed object

Throws
java.io.IOException

Thrown if there is an error while loading or parsing.

load

public static T <T> load(
    DataSource dataSource,
    ParsingLoadable.Parser<T> parser,
    Uri uri,
    int type
)

Loads a single parsable object.

Parameters
DataSource dataSource

The DataSource through which the object should be read.

ParsingLoadable.Parser<T> parser

The Parser to parse the object from the response.

Uri uri

The Uri of the object to read.

int type

The type of the data. One of the CDATA_TYPE_* constants.

Returns
T

The parsed object

Throws
java.io.IOException

Thrown if there is an error while loading or parsing.