``` class FixedLengthSource(delegate: Source, priv...
# android
k
Copy code
class FixedLengthSource(delegate: Source, private var bytesRemaining: Long) : ForwardingSource(delegate) {

    override fun read(sink: Buffer, byteCount: Long): Long {
        if (byteCount < 0) throw IllegalArgumentException("byteCount < 0: " + byteCount)
        if (bytesRemaining == 0L) return -1

        val read = super.read(sink, byteCount.coerceAtMost(bytesRemaining))

        if (read == -1L) return -1

        bytesRemaining -= read

        return read
    }
}