Skip to content

//readium-shared/org.readium.r2.shared.fetcher/BufferingResource

BufferingResource

[androidJvm]\ class BufferingResource(resource: Resource, resourceLength: Long? = null, bufferSize: Long = DEFAULT_BUFFER_SIZE) : ProxyResource

Wraps a Resource and buffers its content.

Expensive interaction with the underlying resource is minimized, since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that copying takes place when filling that buffer, but this is usually outweighed by the performance benefits.

Note that this implementation is pretty limited and the benefits are only apparent when reading forward and consecutively – e.g. when downloading the resource by chunks. The buffer is ignored when reading backward or far ahead.

Parameters

androidJvm

resource Underlying resource which will be buffered.
resourceLength The total length of the resource, when known. This can improve performance by avoiding requesting the length from the underlying resource.
bufferSize Size of the buffer chunks to read.

Constructors

BufferingResource [androidJvm]
fun BufferingResource(resource: Resource, resourceLength: Long? = null, bufferSize: Long = DEFAULT_BUFFER_SIZE)

Types

Name Summary
Companion [androidJvm]
object Companion

Functions

Name Summary
close [androidJvm]
open suspend override fun close()
Closes this object and releases any resources associated with it. If the object is already closed then invoking this method has no effect.
length [androidJvm]
open suspend override fun length(): ResourceTry<Long>
Returns data length from metadata if available, or calculated from reading the bytes otherwise.
link [androidJvm]
open suspend override fun link(): Link
Returns the link from which the resource was retrieved.
read [androidJvm]
open suspend override fun read(range: LongRange?): ResourceTry<ByteArray>
Reads the bytes at the given range.
readAsBitmap [androidJvm]
open suspend fun readAsBitmap(): ResourceTry<Bitmap>
Reads the full content as a Bitmap.
readAsJson [androidJvm]
open suspend fun readAsJson(): ResourceTry<JSONObject>
Reads the full content as a JSON object.
readAsString [androidJvm]
open suspend fun readAsString(charset: Charset? = null): ResourceTry<String>
Reads the full content as a String.
readAsXml [androidJvm]
open suspend fun readAsXml(): ResourceTry<ElementNode>
Reads the full content as an XML document.
toString [androidJvm]
open override fun toString(): String

Properties

Name Summary
file [androidJvm]
open override val file: File?
Direct file to this resource, when available.

Extensions

Name Summary
buffered [androidJvm]
fun Resource.buffered(resourceLength: Long? = null, size: Long = BufferingResource.DEFAULT_BUFFER_SIZE): BufferingResource
Wraps this resource in a BufferingResource to improve reading performances.
cached [androidJvm]
fun Resource.~~cached~~(): Resource
Creates a cached resource wrapping this resource.
fallback [androidJvm]
fun Resource.fallback(fallbackResourceFactory: (Resource.Exception) -> Resource): Resource
Falls back to alternative resources when the receiver fails.
[androidJvm]
fun Resource.fallback(fallbackResource: Resource): Resource
Falls back to the given alternative resource when the receiver fails.
synchronized [androidJvm]
fun Resource.synchronized(): SynchronizedResource
Wraps this resource in a SynchronizedResource to protect the access from multiple threads.