Skip to content

Commit 4f5fa48

Browse files
authored
feat: Add readFully extension method to SdkSource which reads an exact amount of bytes (#799)
1 parent 40f60ea commit 4f5fa48

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "55bff483-d8ae-4872-9d64-6187c28ba923",
3+
"type": "feature",
4+
"description": "Add readFully extension method to SdkSource"
5+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ kotlinLoggingVersion=3.0.0
4444
slf4jVersion=2.0.6
4545

4646
# crt
47-
crtKotlinVersion=0.6.8-SNAPSHOT
47+
crtKotlinVersion=0.6.9-SNAPSHOT

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,26 @@ public expect suspend fun SdkSource.readToByteArray(): ByteArray
5353
*/
5454
@InternalApi
5555
public expect fun SdkSource.toSdkByteReadChannel(coroutineScope: CoroutineScope? = null): SdkByteReadChannel
56+
57+
/**
58+
* Remove exactly [byteCount] bytes from this source and appends them to [sink].
59+
* @param sink The sink to append bytes to
60+
* @param byteCount the number of bytes to read from the source
61+
* @throws [IllegalArgumentException] when [byteCount] is less than zero
62+
* @throws [EOFException] when the source is exhausted before [byteCount] bytes could be read
63+
*/
64+
@Throws(IOException::class)
65+
public fun SdkSource.readFully(sink: SdkBuffer, byteCount: Long) {
66+
require(byteCount >= 0L) { "Invalid length ($byteCount) must be >= 0L" }
67+
68+
var totalBytesRead = 0L
69+
while (totalBytesRead != byteCount) {
70+
val rc = read(sink, byteCount - totalBytesRead)
71+
72+
if (rc == -1L) {
73+
throw EOFException("Unexpected EOF: expected ${byteCount - totalBytesRead} more bytes; consumed: $totalBytesRead")
74+
}
75+
76+
totalBytesRead += rc
77+
}
78+
}

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/io/SdkBufferedSourceTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,35 @@ abstract class BufferedSourceTest(
315315
source.require(1024 * 9 + 1)
316316
}
317317
}
318+
319+
@Test
320+
fun testReadFully() {
321+
val data = "123456789".repeat(1024)
322+
sink.writeUtf8(data)
323+
sink.flush()
324+
325+
val dest = SdkBuffer()
326+
source.readFully(dest, data.length.toLong())
327+
assertEquals(data, dest.readUtf8())
328+
}
329+
330+
@Test
331+
fun testReadFullyIllegalArgumentException() {
332+
val dest = SdkBuffer()
333+
assertFailsWith<IllegalArgumentException> {
334+
source.readFully(dest, -1)
335+
}
336+
}
337+
338+
@Test
339+
fun testReadFullyEOFException() {
340+
val data = "123456789".repeat(1024)
341+
sink.writeUtf8(data)
342+
sink.flush()
343+
344+
val dest = SdkBuffer()
345+
assertFailsWith<EOFException> {
346+
source.readFully(dest, data.length.toLong() + 1)
347+
}
348+
}
318349
}

0 commit comments

Comments
 (0)