Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/MultipartReader.kt (Kotlin)

Code before:
        require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
        check(currentPart == this) { "closed" }

        source.timeout().intersectWith(timeout) {
          return when (val limit = currentPartBytesRemaining(maxResult = byteCount)) {
            0L -> -1L // No more bytes in this part.
            else -> source.read(sink, limit)
          }
        }

        error("unreachable") // TODO(jwilson): fix intersectWith() to return T.
      }

      override fun timeout(): Timeout = timeout
    }

    /**
     * Returns a value in [0..maxByteCount] with the number of bytes that can be read from [source] in
     * the current part. If this returns 0 the current part is exhausted; otherwise it has at least
     * one byte left to read.
     */
    private fun currentPartBytesRemaining(maxResult: Long): Long {
      source.require(crlfDashDashBoundary.size.toLong())

      return when (val delimiterIndex = source.buffer.indexOf(crlfDashDashBoundary)) {
        -1L -> minOf(maxResult, source.buffer.size - crlfDashDashBoundary.size + 1)
        else -> minOf(maxResult, delimiterIndex)
      }
    }

    @Throws(IOException::class)
    override fun close() {
      if (closed) return
      closed = true
      currentPart = null
      source.close()
    }

    /** A single part in a multipart body. */
    class Part(
      @get:JvmName("headers") val headers: Headers,
      @get:JvmName("body") val body: BufferedSource,
    ) : Closeable by body

    internal companion object {
      /** These options follow the boundary. */
      val afterBoundaryOptions =
        Options.of(
          // 0.  "\r\n"  More parts.
          "\r\n".encodeUtf8(),
          // 1.  "--"    No more parts.

Review comment (suggestion):
Ugh, I regret that BufferedSource doesn’t have an API that does what you want without needing to `peek()` and `limit()`

Code after:
      ): Long {
        require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
        check(currentPart == this) { "closed" }

        source.timeout().intersectWith(timeout) {
          return when (val limit = currentPartBytesRemaining(maxResult = byteCount)) {
            0L -> -1L // No more bytes in this part.
            else -> source.read(sink, limit)
          }
        }

        error("unreachable") // TODO(jwilson): fix intersectWith() to return T.
      }

      override fun timeout(): Timeout = timeout
    }

    /**
     * Returns a value in [0..maxByteCount] with the number of bytes that can be read from [source] in
     * the current part. If this returns 0 the current part is exhausted; otherwise it has at least
     * one byte left to read.
     */
    private fun currentPartBytesRemaining(maxResult: Long): Long {
      // Avoid indexOf scanning repeatedly over the entire source by using limit
      // Since maxResult could be midway through the boundary, read further to be safe.
      val limitSource = source.peek().limit(maxResult + crlfDashDashBoundary.size).buffer()
      limitSource.require(crlfDashDashBoundary.size.toLong())

      val delimiterIndex = limitSource.buffer.indexOf(crlfDashDashBoundary)
      return when (delimiterIndex) {
        -1L -> minOf(maxResult, limitSource.buffer.size - crlfDashDashBoundary.size + 1)
        else -> minOf(maxResult, delimiterIndex)
      }
    }

    @Throws(IOException::class)
    override fun close() {
      if (closed) return
      closed = true
      currentPart = null
      source.close()
    }

    /** A single part in a multipart body. */
    class Part(
      @get:JvmName("headers") val headers: Headers,
      @get:JvmName("body") val body: BufferedSource,
    ) : Closeable by body

    internal companion object {
      /** These options follow the boundary. */