Skip to content

Commit c511514

Browse files
committed
Update docs for writeToOutputStream, add appendToOutputStream
1 parent 32f1ed8 commit c511514

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

runtime/runtime-core/api/runtime-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ public abstract class aws/smithy/kotlin/runtime/content/ByteStream$SourceStream
416416
}
417417

418418
public final class aws/smithy/kotlin/runtime/content/ByteStreamJVMKt {
419+
public static final fun appendToOutputStream (Laws/smithy/kotlin/runtime/content/ByteStream;Ljava/io/OutputStream;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
419420
public static final fun asByteStream (Ljava/io/File;JJ)Laws/smithy/kotlin/runtime/content/ByteStream;
420421
public static final fun asByteStream (Ljava/io/File;Lkotlin/ranges/LongRange;)Laws/smithy/kotlin/runtime/content/ByteStream;
421422
public static final fun asByteStream (Ljava/io/InputStream;Ljava/lang/Long;)Laws/smithy/kotlin/runtime/content/ByteStream$SourceStream;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public fun InputStream.asByteStream(contentLength: Long? = null): ByteStream.Sou
123123
}
124124

125125
/**
126-
* Writes this stream to the given [OutputStream]. This method does not flush or close the given [OutputStream].
126+
* Writes this stream to the given [OutputStream].
127127
* @param outputStream The [OutputStream] to which the contents of this stream will be written
128128
*/
129129
public suspend fun ByteStream.writeToOutputStream(outputStream: OutputStream): Long = withContext(Dispatchers.IO) {
@@ -140,6 +140,24 @@ public suspend fun ByteStream.writeToOutputStream(outputStream: OutputStream): L
140140
}
141141
}
142142

143+
/**
144+
* Writes this stream to the given [OutputStream]. This method does not flush or close the given [OutputStream].
145+
* @param outputStream The [OutputStream] to which the contents of this stream will be written
146+
*/
147+
public suspend fun ByteStream.appendToOutputStream(outputStream: OutputStream): Long = withContext(Dispatchers.IO) {
148+
val src = when (val stream = this@appendToOutputStream) {
149+
is ByteStream.ChannelStream -> return@withContext outputStream.writeAll(stream.readFrom())
150+
is ByteStream.Buffer -> stream.bytes().source()
151+
is ByteStream.SourceStream -> stream.readFrom()
152+
}
153+
154+
val out = outputStream.sink().buffer()
155+
out.writeAll(src).also {
156+
out.flush()
157+
}
158+
}
159+
160+
143161
private suspend fun OutputStream.writeAll(chan: SdkByteReadChannel): Long =
144162
sink().use {
145163
chan.readAll(it)

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
package aws.smithy.kotlin.runtime.content
77

88
import aws.smithy.kotlin.runtime.testing.RandomTempFile
9+
import jdk.jshell.Snippet.Status
910
import kotlinx.coroutines.test.runTest
1011
import java.io.ByteArrayOutputStream
12+
import java.io.OutputStream
1113
import java.nio.file.Files
1214
import kotlin.test.*
1315

@@ -186,4 +188,38 @@ class ByteStreamJVMTest {
186188
assertContentEquals(binaryData, output)
187189
}
188190
}
191+
192+
@Test
193+
fun testWriteToByteStreamClosesOutput() = runTest {
194+
val byteStream = ByteStream.fromString("Hello")
195+
196+
val sos = StatusTrackingOutputStream(ByteArrayOutputStream())
197+
198+
assertFalse(sos.closed)
199+
byteStream.writeToOutputStream(sos)
200+
assertTrue(sos.closed)
201+
}
202+
203+
@Test
204+
fun testAppendToByteStreamDoesNotCloseOutput() = runTest {
205+
val byteStream = ByteStream.fromString("Don't close me!")
206+
207+
val sos = StatusTrackingOutputStream(ByteArrayOutputStream())
208+
209+
assertFalse(sos.closed)
210+
byteStream.appendToOutputStream(sos)
211+
assertFalse(sos.closed)
212+
}
213+
214+
private class StatusTrackingOutputStream(val os: OutputStream) : OutputStream() {
215+
var closed: Boolean = false
216+
217+
override fun write(b: Int) {
218+
os.write(b)
219+
}
220+
221+
override fun close() {
222+
closed = true
223+
}
224+
}
189225
}

0 commit comments

Comments
 (0)