Skip to content

Commit 792a6f8

Browse files
authored
misc: Update docs for ByteStream.writeToOutputStream, add ByteStream.appendToOutputStream (#1172)
1 parent 40bf65d commit 792a6f8

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "cda907f0-b704-42de-a50f-dad204dacbfb",
3+
"type": "misc",
4+
"description": "Correct documentation for `ByteStream.writeToOutputStream`, add `ByteStream.appendToOutputStream`"
5+
}

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: 16 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], then closes it.
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,21 @@ 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)
156+
}
157+
143158
private suspend fun OutputStream.writeAll(chan: SdkByteReadChannel): Long =
144159
sink().use {
145160
chan.readAll(it)

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

Lines changed: 35 additions & 0 deletions
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.OutputStream
1112
import java.nio.file.Files
1213
import kotlin.test.*
1314

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

0 commit comments

Comments
 (0)