Skip to content

Commit

Permalink
Replace additional RealBufferedSource.commonExhausted() logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdgr8 committed Jul 25, 2022
1 parent 031a3a1 commit 3571012
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 20 deletions.
10 changes: 2 additions & 8 deletions okio/src/commonMain/kotlin/okio/internal/-RealBufferedSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ internal inline fun RealBufferedSource.commonRead(sink: Buffer, byteCount: Long)
require(byteCount >= 0L) { "byteCount < 0: $byteCount" }
check(!closed) { "closed" }

if (buffer.size == 0L) {
val read = source.read(buffer, Segment.SIZE.toLong())
if (read == -1L) return -1L
}
if (exhausted()) return -1L

val toRead = minOf(byteCount, buffer.size)
return buffer.read(sink, toRead)
Expand Down Expand Up @@ -130,10 +127,7 @@ internal inline fun RealBufferedSource.commonReadFully(sink: ByteArray) {
internal inline fun RealBufferedSource.commonRead(sink: ByteArray, offset: Int, byteCount: Int): Int {
checkOffsetAndCount(sink.size.toLong(), offset.toLong(), byteCount.toLong())

if (buffer.size == 0L) {
val read = source.read(buffer, Segment.SIZE.toLong())
if (read == -1L) return -1
}
if (exhausted()) return -1

val toRead = okio.minOf(byteCount, buffer.size).toInt()
return buffer.read(sink, offset, toRead)
Expand Down
15 changes: 3 additions & 12 deletions okio/src/jvmMain/kotlin/okio/RealBufferedSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ internal actual class RealBufferedSource actual constructor(
commonRead(sink, offset, byteCount)

override fun read(sink: ByteBuffer): Int {
if (buffer.size == 0L) {
val read = source.read(buffer, Segment.SIZE.toLong())
if (read == -1L) return -1
}
if (exhausted()) return -1

return buffer.read(sink)
}
Expand Down Expand Up @@ -143,21 +140,15 @@ internal actual class RealBufferedSource actual constructor(
return object : InputStream() {
override fun read(): Int {
if (closed) throw IOException("closed")
if (buffer.size == 0L) {
val count = source.read(buffer, Segment.SIZE.toLong())
if (count == -1L) return -1
}
if (exhausted()) return -1
return buffer.readByte() and 0xff
}

override fun read(data: ByteArray, offset: Int, byteCount: Int): Int {
if (closed) throw IOException("closed")
checkOffsetAndCount(data.size.toLong(), offset.toLong(), byteCount.toLong())

if (buffer.size == 0L) {
val count = source.read(buffer, Segment.SIZE.toLong())
if (count == -1L) return -1
}
if (exhausted()) return -1

return buffer.read(data, offset, byteCount)
}
Expand Down

0 comments on commit 3571012

Please sign in to comment.