ByteArraySourceProvider.kt
TLDR
The ByteArraySourceProvider.kt
file in the shark-hprof
module of the Demo Projects project is responsible for providing a source of data from a byte array. It implements the DualSourceProvider
interface and provides methods for opening a streaming source and a random access source.
Classes
ByteArraySourceProvider
This class is responsible for providing a source of data from a byte array. It implements the DualSourceProvider
interface and provides methods for opening a streaming source and a random access source. It takes a byte array as a constructor parameter.
Methods
No methods defined in the file.
package shark
import java.io.IOException
import okio.Buffer
import okio.BufferedSource
class ByteArraySourceProvider(private val byteArray: ByteArray) : DualSourceProvider {
override fun openStreamingSource(): BufferedSource = Buffer().apply { write(byteArray) }
override fun openRandomAccessSource(): RandomAccessSource {
return object : RandomAccessSource {
var closed = false
override fun read(
sink: Buffer,
position: Long,
byteCount: Long
): Long {
if (closed) {
throw IOException("Source closed")
}
val maxByteCount = byteCount.coerceAtMost(byteArray.size - position)
sink.write(byteArray, position.toInt(), maxByteCount.toInt())
return maxByteCount
}
override fun close() {
closed = true
}
}
}
}