Skip to content

Commit

Permalink
Fix for repeated reads
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Feb 15, 2025
1 parent b342aee commit 0802079
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/MultipartReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ class MultipartReader
* 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)
val toRead =
when (delimiterIndex) {
-1L -> minOf(maxResult, limitSource.buffer.size - crlfDashDashBoundary.size + 1)
else -> minOf(maxResult, delimiterIndex)
}
return toRead
return when (delimiterIndex) {
-1L -> minOf(maxResult, limitSource.buffer.size - crlfDashDashBoundary.size + 1)
else -> minOf(maxResult, delimiterIndex)
}
}

@Throws(IOException::class)
Expand Down

0 comments on commit 0802079

Please sign in to comment.