|
5 | 5 |
|
6 | 6 | package aws.smithy.kotlin.runtime.content |
7 | 7 |
|
| 8 | +import aws.smithy.kotlin.runtime.io.readToByteArray |
8 | 9 | import aws.smithy.kotlin.runtime.testing.RandomTempFile |
9 | 10 | import kotlinx.coroutines.test.runTest |
| 11 | +import java.io.BufferedInputStream |
| 12 | +import java.io.ByteArrayInputStream |
10 | 13 | import java.io.ByteArrayOutputStream |
11 | 14 | import java.io.InputStream |
12 | 15 | import java.io.OutputStream |
@@ -228,6 +231,31 @@ class ByteStreamJVMTest { |
228 | 231 | assertFalse(sos.closed) |
229 | 232 | } |
230 | 233 |
|
| 234 | + // https://github.com/awslabs/aws-sdk-kotlin/issues/1473 |
| 235 | + @Test |
| 236 | + fun testReplayableInputStreamAsByteStream() = runTest { |
| 237 | + val content = "Hello, Bytes!".encodeToByteArray() |
| 238 | + val byteArrayIns = ByteArrayInputStream(content) |
| 239 | + val nonReplayableIns = NonReplayableInputStream(byteArrayIns) |
| 240 | + |
| 241 | + // buffer the non-replayable stream, making it replayable... |
| 242 | + val bufferedIns = BufferedInputStream(nonReplayableIns) |
| 243 | + |
| 244 | + val byteStream = bufferedIns.asByteStream(content.size.toLong()) |
| 245 | + |
| 246 | + // Test that it can be read at least twice (once for hashing the body, once for transmitting the body) |
| 247 | + assertContentEquals(content, byteStream.readFrom().use { it.readToByteArray() }) |
| 248 | + assertContentEquals(content, byteStream.readFrom().use { it.readToByteArray() }) |
| 249 | + } |
| 250 | + |
| 251 | + private class NonReplayableInputStream(val inputStream: InputStream) : InputStream() { |
| 252 | + override fun markSupported(): Boolean = false // not replayable |
| 253 | + |
| 254 | + override fun read(): Int = inputStream.read() |
| 255 | + override fun mark(readlimit: Int)= inputStream.mark(readlimit) |
| 256 | + override fun reset() = inputStream.reset() |
| 257 | + } |
| 258 | + |
231 | 259 | private class StatusTrackingOutputStream(val os: OutputStream) : OutputStream() { |
232 | 260 | var closed: Boolean = false |
233 | 261 |
|
|
0 commit comments