File tree Expand file tree Collapse file tree 4 files changed +60
-1
lines changed
runtime/runtime-core/common
src/aws/smithy/kotlin/runtime/io
test/aws/smithy/kotlin/runtime/io Expand file tree Collapse file tree 4 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "id" : " 55bff483-d8ae-4872-9d64-6187c28ba923" ,
3+ "type" : " feature" ,
4+ "description" : " Add readFully extension method to SdkSource"
5+ }
Original file line number Diff line number Diff line change @@ -44,4 +44,4 @@ kotlinLoggingVersion=3.0.0
4444slf4jVersion =2.0.6
4545
4646# crt
47- crtKotlinVersion =0.6.8 -SNAPSHOT
47+ crtKotlinVersion =0.6.9 -SNAPSHOT
Original file line number Diff line number Diff line change @@ -53,3 +53,26 @@ public expect suspend fun SdkSource.readToByteArray(): ByteArray
5353 */
5454@InternalApi
5555public 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments