Skip to content

Commit d51e9a8

Browse files
committed
feedback
1 parent 6958bfc commit d51e9a8

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/io/SdkSource.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,13 @@ public fun SdkSource.readFully(sink: SdkBuffer, byteCount: Long) {
119119
*/
120120
@InternalApi
121121
public fun SdkSource.readRemaining(sink: SdkBuffer): Long {
122-
var totalReadBytes: Long = 0
123-
var readBytes: Long
122+
var totalBytesRead: Long = 0
123+
var bytesRead = read(sink, Long.MAX_VALUE)
124124

125-
do {
126-
// ensure any errors are propagated by attempting to read at least once
127-
readBytes = read(sink, Long.MAX_VALUE)
128-
totalReadBytes += readBytes
129-
} while (readBytes != -1L)
125+
while (bytesRead != -1L) {
126+
totalBytesRead += bytesRead
127+
bytesRead = read(sink, Long.MAX_VALUE)
128+
}
130129

131-
return totalReadBytes + 1L // Account for last -1 read
130+
return totalBytesRead
132131
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.io
7+
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class SdkSourceTest {
12+
@Test
13+
fun readRemaining() {
14+
val data = "Hello world"
15+
val dataLength = data.length.toLong()
16+
val readCycles = 100
17+
val totalDataLength = dataLength * readCycles
18+
19+
// Manual and readRemaining
20+
val source = createTestSource(data, dataLength, readCycles)
21+
val buffer = SdkBuffer()
22+
val manualReads = 10
23+
repeat(manualReads) {
24+
source.read(buffer, dataLength)
25+
}
26+
var readByReadRemaining = source.readRemaining(buffer)
27+
assertEquals(readByReadRemaining, totalDataLength - manualReads * dataLength)
28+
assertEquals(buffer.size, totalDataLength)
29+
30+
// Only readRemaining
31+
buffer.skip(totalDataLength)
32+
readByReadRemaining = createTestSource(data, dataLength, readCycles).readRemaining(buffer)
33+
assertEquals(readByReadRemaining, totalDataLength)
34+
assertEquals(buffer.size, totalDataLength)
35+
}
36+
}
37+
38+
private fun createTestSource(
39+
data: String,
40+
dataLength: Long,
41+
readCycles: Int,
42+
) =
43+
object : SdkSource {
44+
var remainingReadCycles = readCycles
45+
46+
override fun read(sink: SdkBuffer, limit: Long): Long {
47+
if (remainingReadCycles == 0) {
48+
return -1L
49+
}
50+
51+
sink.writeUtf8(data)
52+
remainingReadCycles--
53+
return dataLength
54+
}
55+
56+
override fun close() {}
57+
}

0 commit comments

Comments
 (0)