Skip to content

Commit

Permalink
fix: Check if an InputStream is replayable when setting isOneShot (
Browse files Browse the repository at this point in the history
  • Loading branch information
lauzadis authored Nov 22, 2024
1 parent 5f288d6 commit 1b5d3ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/68aac1d5-64f9-4569-bf95-db8d5712a2df.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "68aac1d5-64f9-4569-bf95-db8d5712a2df",
"type": "bugfix",
"description": "Infer the replayability of `InputStream` using `markSupported()` when setting `isOneShot`"
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public fun InputStream.asByteStream(contentLength: Long? = null): ByteStream.Sou
val source = source()
return object : ByteStream.SourceStream() {
override val contentLength: Long? = contentLength
override val isOneShot: Boolean = true
override val isOneShot: Boolean = !markSupported()
override fun readFrom(): SdkSource = source
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package aws.smithy.kotlin.runtime.content
import aws.smithy.kotlin.runtime.testing.RandomTempFile
import kotlinx.coroutines.test.runTest
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.nio.file.Files
import kotlin.test.*
Expand Down Expand Up @@ -159,7 +160,7 @@ class ByteStreamJVMTest {
binaryData.inputStream().use { inputStream ->
val byteStream = inputStream.asByteStream()
assertNull(byteStream.contentLength)
assertTrue(byteStream.isOneShot)
assertFalse(byteStream.isOneShot)

val output = byteStream.toByteArray()
assertContentEquals(binaryData, output)
Expand All @@ -169,6 +170,23 @@ class ByteStreamJVMTest {
@Test
fun testInputStreamAsByteStreamWithLength() = runTest {
binaryData.inputStream().use { inputStream ->
val byteStream = inputStream.asByteStream(binaryData.size.toLong())
assertEquals(binaryData.size.toLong(), byteStream.contentLength)
assertFalse(byteStream.isOneShot)

val output = byteStream.toByteArray()
assertContentEquals(binaryData, output)
}
}

@Test
fun testOneShotInputStream() = runTest {
class NonReplayableInputStream(val delegate: InputStream) : InputStream() {
override fun read(): Int = delegate.read()
override fun markSupported(): Boolean = false
}

NonReplayableInputStream(binaryData.inputStream()).use { inputStream ->
val byteStream = inputStream.asByteStream(binaryData.size.toLong())
assertEquals(binaryData.size.toLong(), byteStream.contentLength)
assertTrue(byteStream.isOneShot)
Expand Down

0 comments on commit 1b5d3ce

Please sign in to comment.