BufferingResource

class BufferingResource(resource: Resource, resourceLength: Long? = null, bufferSize: Int = DEFAULT_BUFFER_SIZE) : Resource

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

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

Link copied to clipboard
constructor(resource: Resource, resourceLength: Long? = null, bufferSize: Int = DEFAULT_BUFFER_SIZE)

Properties

Link copied to clipboard
open override val sourceUrl: AbsoluteUrl?

URL locating this resource, if any.

Functions

Link copied to clipboard
fun Readable.asInputStream(range: LongRange? = null, wrapError: (ReadError) -> IOException = { ReadException(it) }): InputStream

Wraps a Readable into an InputStream.

Link copied to clipboard

Returns a new Readable accessing the same data but not owning them.

Returns a new Resource accessing the same data but not owning them.

Link copied to clipboard
fun Readable.buffered(contentLength: Long? = null, bufferSize: Int = DEFAULT_BUFFER_SIZE): Readable

Wraps this resource into a buffer to improve reading performances.

fun Resource.buffered(resourceLength: Long? = null, bufferSize: Int = DEFAULT_BUFFER_SIZE): BufferingResource

Wraps this resource in a BufferingResource to improve reading performances.

Link copied to clipboard
open 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.

Link copied to clipboard
fun Resource.fallback(fallbackResourceFactory: (ReadError) -> Resource?): Resource

Falls back to alternative resources when the receiver fails.

fun Resource.fallback(fallbackResource: Resource): Resource

Falls back to the given alternative Resource when the receiver fails.

Link copied to clipboard
fun <R : Resource> Resource.flatMap(transform: suspend (Resource) -> R): LazyResource
Link copied to clipboard
open suspend override fun length(): Try<Long, ReadError>

Returns data length from metadata if available, or calculated from reading the bytes otherwise.

Link copied to clipboard
fun Resource.map(transform: suspend (ByteArray) -> Try<ByteArray, ReadError>): Resource
Link copied to clipboard
open suspend override fun properties(): Try<Resource.Properties, ReadError>

Properties associated to the resource.

Link copied to clipboard
open suspend override fun read(range: LongRange?): Try<ByteArray, ReadError>

Reads the bytes at the given range.

Link copied to clipboard
inline suspend fun <R> Readable.readDecodeOrElse(decode: (value: ByteArray) -> Try<R, DecodeError>, recover: (ReadError) -> R): R
inline suspend fun <R> Readable.readDecodeOrElse(decode: (value: ByteArray) -> Try<R, DecodeError>, recoverRead: (ReadError) -> R, recoverDecode: (DecodeError.Decoding) -> R): R
Link copied to clipboard
inline suspend fun <R> Readable.readDecodeOrNull(decode: (value: ByteArray) -> Try<R, DecodeError>): R?
Link copied to clipboard
inline suspend fun Readable.readOrElse(recover: (ReadError) -> ByteArray): ByteArray
Link copied to clipboard

Wraps this resource in a SynchronizedResource to protect the access from multiple threads.