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 final class ParsingLoadable.Builder<T>

Builder for ParsingLoadable.

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 @Nullable String

The ID of the steered pathway from which data is being loaded, or null if not applicable.

final int

The type of the data.

Public constructors

ParsingLoadable(
    DataSource dataSource,
    DataSpec dataSpec,
    @C.DataType int type,
    ParsingLoadable.Parser<T> parser
)

This method is deprecated.

Use Builder instead.

ParsingLoadable(
    DataSource dataSource,
    Uri uri,
    @C.DataType int type,
    ParsingLoadable.Parser<T> parser
)

This method is deprecated.

Use Builder instead.

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,
    @C.DataType int type
)

Loads a single parsable object.

static T
<T> load(
    DataSource dataSource,
    ParsingLoadable.Parser<T> parser,
    Uri uri,
    @C.DataType 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

steeredPathwayId

public final @Nullable String steeredPathwayId

The ID of the steered pathway from which data is being loaded, or null if not applicable.

type

@C.DataType
public final int type

The type of the data. For reporting only.

Public constructors

ParsingLoadable

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

ParsingLoadable

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

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 AtomicBoolean).

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,
    @C.DataType 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.

@C.DataType int type

The type of the data.

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,
    @C.DataType 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.

@C.DataType int type

The type of the data.

Returns
T

The parsed object

Throws
java.io.IOException

Thrown if there is an error while loading or parsing.