CacheDataSink
@UnstableApi
public final class CacheDataSink implements DataSink
Writes data into a cache.
If the DataSpec passed to open has the length field set to LENGTH_UNSET and FLAG_DONT_CACHE_IF_LENGTH_UNKNOWN set, then write calls are ignored.
Summary
Nested types |
|---|
public final class CacheDataSink.CacheDataSinkException extends Cache.CacheExceptionThrown when an |
public final class CacheDataSink.Factory implements DataSink.Factory
|
Constants |
|
|---|---|
static final int |
DEFAULT_BUFFER_SIZE = 20480Default buffer size in bytes. |
static final long |
DEFAULT_FRAGMENT_SIZE = 5242880Default |
Public constructors |
|---|
CacheDataSink(Cache cache, long fragmentSize)Constructs an instance using |
CacheDataSink(Cache cache, long fragmentSize, int bufferSize) |
Public methods |
|
|---|---|
void |
close()Closes the sink. |
void |
Opens the sink to consume the specified data. |
void |
write(byte[] buffer, int offset, int length)Consumes the provided data. |
Constants
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE = 20480
Default buffer size in bytes.
DEFAULT_FRAGMENT_SIZE
public static final long DEFAULT_FRAGMENT_SIZE = 5242880
Default fragmentSize recommended for caching use cases.
Public constructors
CacheDataSink
public CacheDataSink(Cache cache, long fragmentSize)
Constructs an instance using DEFAULT_BUFFER_SIZE.
| Parameters | |
|---|---|
Cache cache |
The cache into which data should be written. |
long fragmentSize |
For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to |
CacheDataSink
public CacheDataSink(Cache cache, long fragmentSize, int bufferSize)
| Parameters | |
|---|---|
Cache cache |
The cache into which data should be written. |
long fragmentSize |
For requests that should be fragmented into multiple cache files, this is the maximum size of a cache file in bytes. If set to |
int bufferSize |
The buffer size in bytes for writing to a cache file. A zero or negative value disables buffering. |
Public methods
close
public void close()
Closes the sink.
Note: This method must be called even if the corresponding call to open threw an IOException. See open for more details.
| Throws | |
|---|---|
java.io.IOException |
If an error occurs closing the sink. |
androidx.media3.datasource.cache.CacheDataSink.CacheDataSinkException |
open
public void open(DataSpec dataSpec)
Opens the sink to consume the specified data.
Note: If an IOException is thrown, callers must still call close to ensure that any partial effects of the invocation are cleaned up.
| Parameters | |
|---|---|
DataSpec dataSpec |
Defines the data to be consumed. |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs opening the sink. |
androidx.media3.datasource.cache.CacheDataSink.CacheDataSinkException |
write
public void write(byte[] buffer, int offset, int length)
Consumes the provided data.
| Parameters | |
|---|---|
byte[] buffer |
The buffer from which data should be consumed. |
int offset |
The offset of the data to consume in |
int length |
The length of the data to consume, in bytes. |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs writing to the sink. |
androidx.media3.datasource.cache.CacheDataSink.CacheDataSinkException |