1 package shark 2 3 import java.io.File 4 import okio.Buffer 5 import okio.BufferedSource 6 import okio.Source 7 import okio.buffer 8 9 class MetricsDualSourceProvider( 10 private val realSourceProvider: DualSourceProvider 11 ) : DualSourceProvider { 12 13 constructor(file: File) : this(FileSourceProvider(file)) 14 15 val sourcesMetrics = mutableListOf<MutableList<Int>>() 16 openStreamingSourcenull17 override fun openStreamingSource(): BufferedSource { 18 val sourceMetrics = mutableListOf<Int>() 19 sourcesMetrics += sourceMetrics 20 val fileSource = realSourceProvider.openStreamingSource() 21 return object : Source { 22 override fun read( 23 sink: Buffer, 24 byteCount: Long 25 ): Long { 26 val bytesRead = fileSource.read(sink, byteCount) 27 sourceMetrics += if (bytesRead >= 0) bytesRead.toInt() else 0 28 return bytesRead 29 } 30 31 override fun close() = fileSource.close() 32 33 override fun timeout() = fileSource.timeout() 34 }.buffer() 35 } 36 openRandomAccessSourcenull37 override fun openRandomAccessSource(): RandomAccessSource { 38 val sourceMetrics = mutableListOf<Int>() 39 sourcesMetrics += sourceMetrics 40 val randomAccessSource = realSourceProvider.openRandomAccessSource() 41 return object : RandomAccessSource { 42 override fun read( 43 sink: Buffer, 44 position: Long, 45 byteCount: Long 46 ): Long { 47 val bytesRead = randomAccessSource.read(sink, position, byteCount) 48 sourceMetrics += bytesRead.toInt() 49 return bytesRead 50 } 51 52 override fun close() = randomAccessSource.close() 53 } 54 } 55 } 56