1 package shark 2 3 import java.io.IOException 4 import okio.Buffer 5 import okio.BufferedSource 6 7 class ByteArraySourceProvider(private val byteArray: ByteArray) : DualSourceProvider { <lambda>null8 override fun openStreamingSource(): BufferedSource = Buffer().apply { write(byteArray) } 9 openRandomAccessSourcenull10 override fun openRandomAccessSource(): RandomAccessSource { 11 return object : RandomAccessSource { 12 13 var closed = false 14 15 override fun read( 16 sink: Buffer, 17 position: Long, 18 byteCount: Long 19 ): Long { 20 if (closed) { 21 throw IOException("Source closed") 22 } 23 val maxByteCount = byteCount.coerceAtMost(byteArray.size - position) 24 sink.write(byteArray, position.toInt(), maxByteCount.toInt()) 25 return maxByteCount 26 } 27 28 override fun close() { 29 closed = true 30 } 31 } 32 } 33 } 34