Skip to content

Commit 1b5d3ce

Browse files
authored
fix: Check if an InputStream is replayable when setting isOneShot (#1187)
1 parent 5f288d6 commit 1b5d3ce

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "68aac1d5-64f9-4569-bf95-db8d5712a2df",
3+
"type": "bugfix",
4+
"description": "Infer the replayability of `InputStream` using `markSupported()` when setting `isOneShot`"
5+
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/content/ByteStreamJVM.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public fun InputStream.asByteStream(contentLength: Long? = null): ByteStream.Sou
117117
val source = source()
118118
return object : ByteStream.SourceStream() {
119119
override val contentLength: Long? = contentLength
120-
override val isOneShot: Boolean = true
120+
override val isOneShot: Boolean = !markSupported()
121121
override fun readFrom(): SdkSource = source
122122
}
123123
}

runtime/runtime-core/jvm/test/aws/smithy/kotlin/runtime/content/ByteStreamJVMTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package aws.smithy.kotlin.runtime.content
88
import aws.smithy.kotlin.runtime.testing.RandomTempFile
99
import kotlinx.coroutines.test.runTest
1010
import java.io.ByteArrayOutputStream
11+
import java.io.InputStream
1112
import java.io.OutputStream
1213
import java.nio.file.Files
1314
import kotlin.test.*
@@ -159,7 +160,7 @@ class ByteStreamJVMTest {
159160
binaryData.inputStream().use { inputStream ->
160161
val byteStream = inputStream.asByteStream()
161162
assertNull(byteStream.contentLength)
162-
assertTrue(byteStream.isOneShot)
163+
assertFalse(byteStream.isOneShot)
163164

164165
val output = byteStream.toByteArray()
165166
assertContentEquals(binaryData, output)
@@ -169,6 +170,23 @@ class ByteStreamJVMTest {
169170
@Test
170171
fun testInputStreamAsByteStreamWithLength() = runTest {
171172
binaryData.inputStream().use { inputStream ->
173+
val byteStream = inputStream.asByteStream(binaryData.size.toLong())
174+
assertEquals(binaryData.size.toLong(), byteStream.contentLength)
175+
assertFalse(byteStream.isOneShot)
176+
177+
val output = byteStream.toByteArray()
178+
assertContentEquals(binaryData, output)
179+
}
180+
}
181+
182+
@Test
183+
fun testOneShotInputStream() = runTest {
184+
class NonReplayableInputStream(val delegate: InputStream) : InputStream() {
185+
override fun read(): Int = delegate.read()
186+
override fun markSupported(): Boolean = false
187+
}
188+
189+
NonReplayableInputStream(binaryData.inputStream()).use { inputStream ->
172190
val byteStream = inputStream.asByteStream(binaryData.size.toLong())
173191
assertEquals(binaryData.size.toLong(), byteStream.contentLength)
174192
assertTrue(byteStream.isOneShot)

0 commit comments

Comments
 (0)