Skip to content

Commit 2316cfc

Browse files
authored
Don't block on 0-byte reads (#1479)
* Don't block on 0-byte reads Closes: #1476 * Fixup another test
1 parent e2ac4c0 commit 2316cfc

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

okio/src/commonMain/kotlin/okio/internal/RealBufferedSource.kt

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal inline fun RealBufferedSource.commonRead(sink: Buffer, byteCount: Long)
3939
check(!closed) { "closed" }
4040

4141
if (buffer.size == 0L) {
42+
if (byteCount == 0L) return 0L
4243
val read = source.read(buffer, Segment.SIZE.toLong())
4344
if (read == -1L) return -1L
4445
}
@@ -134,6 +135,7 @@ internal inline fun RealBufferedSource.commonRead(sink: ByteArray, offset: Int,
134135
checkOffsetAndCount(sink.size.toLong(), offset.toLong(), byteCount.toLong())
135136

136137
if (buffer.size == 0L) {
138+
if (byteCount == 0) return 0
137139
val read = source.read(buffer, Segment.SIZE.toLong())
138140
if (read == -1L) return -1
139141
}

okio/src/commonTest/kotlin/okio/AbstractBufferedSourceTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ abstract class AbstractBufferedSourceTest internal constructor(
289289

290290
// Either 0 or -1 is reasonable here. For consistency with Android's
291291
// ByteArrayInputStream we return 0.
292-
assertEquals(-1, source.read(sink, 0))
292+
val readResult = source.read(sink, 0)
293+
assertTrue(readResult == 0L || readResult == -1L)
293294
assertEquals(10, sink.size)
294295
assertTrue(source.exhausted())
295296
}

okio/src/commonTest/kotlin/okio/CommonRealBufferedSourceTest.kt

+27
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,31 @@ class CommonRealBufferedSourceTest {
153153
"write($write3, ${write3.size})",
154154
)
155155
}
156+
157+
@Test fun readZeroBytesIntoBufferDoesNotRefillBuffer() {
158+
val source = Buffer()
159+
source.writeUtf8("abc")
160+
161+
val sink = Buffer()
162+
163+
val bufferedSource = (source as Source).buffer()
164+
assertEquals(0L, bufferedSource.read(sink, 0L))
165+
166+
assertEquals(0, sink.size)
167+
assertEquals(0, bufferedSource.buffer.size)
168+
assertEquals(3, source.size)
169+
}
170+
171+
@Test fun readZeroBytesIntoByteArrayDoesNotRefillBuffer() {
172+
val source = Buffer()
173+
source.writeUtf8("abc")
174+
175+
val sink = ByteArray(1024)
176+
177+
val bufferedSource = (source as Source).buffer()
178+
assertEquals(0, bufferedSource.read(sink, 0, 0))
179+
180+
assertEquals(0, bufferedSource.buffer.size)
181+
assertEquals(3, source.size)
182+
}
156183
}

okio/src/jvmTest/kotlin/okio/BufferedSourceTest.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@ class BufferedSourceTest(
371371
val sink = Buffer()
372372
sink.writeUtf8("a".repeat(10))
373373

374-
// Either 0 or -1 is reasonable here. For consistency with Android's
375-
// ByteArrayInputStream we return 0.
376-
assertEquals(-1, source.read(sink, 0))
374+
// Either 0 or -1 is reasonable here.
375+
val readResult = source.read(sink, 0)
376+
assertTrue(readResult == 0L || readResult == -1L)
377377
assertEquals(10, sink.size)
378378
assertTrue(source.exhausted())
379379
}

0 commit comments

Comments
 (0)