Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: Update docs for ByteStream.writeToOutputStream, add ByteStream.appendToOutputStream #1172

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/cda907f0-b704-42de-a50f-dad204dacbfb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": "cda907f0-b704-42de-a50f-dad204dacbfb",
"type": "misc",
"description": "Correct documentation for `ByteStream.writeToOutputStream`, add `ByteStream.appendToOutputStream`"
}
1 change: 1 addition & 0 deletions runtime/runtime-core/api/runtime-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ public abstract class aws/smithy/kotlin/runtime/content/ByteStream$SourceStream
}

public final class aws/smithy/kotlin/runtime/content/ByteStreamJVMKt {
public static final fun appendToOutputStream (Laws/smithy/kotlin/runtime/content/ByteStream;Ljava/io/OutputStream;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun asByteStream (Ljava/io/File;JJ)Laws/smithy/kotlin/runtime/content/ByteStream;
public static final fun asByteStream (Ljava/io/File;Lkotlin/ranges/LongRange;)Laws/smithy/kotlin/runtime/content/ByteStream;
public static final fun asByteStream (Ljava/io/InputStream;Ljava/lang/Long;)Laws/smithy/kotlin/runtime/content/ByteStream$SourceStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public fun InputStream.asByteStream(contentLength: Long? = null): ByteStream.Sou
}

/**
* Writes this stream to the given [OutputStream]. This method does not flush or close the given [OutputStream].
* Writes this stream to the given [OutputStream], then closes it.
* @param outputStream The [OutputStream] to which the contents of this stream will be written
*/
public suspend fun ByteStream.writeToOutputStream(outputStream: OutputStream): Long = withContext(Dispatchers.IO) {
Expand All @@ -140,6 +140,21 @@ public suspend fun ByteStream.writeToOutputStream(outputStream: OutputStream): L
}
}

/**
* Writes this stream to the given [OutputStream]. This method does not flush or close the given [OutputStream].
* @param outputStream The [OutputStream] to which the contents of this stream will be written
*/
public suspend fun ByteStream.appendToOutputStream(outputStream: OutputStream): Long = withContext(Dispatchers.IO) {
val src = when (val stream = this@appendToOutputStream) {
is ByteStream.ChannelStream -> return@withContext outputStream.writeAll(stream.readFrom())
is ByteStream.Buffer -> stream.bytes().source()
is ByteStream.SourceStream -> stream.readFrom()
}
Comment on lines +148 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Duplicated code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


val out = outputStream.sink().buffer()
out.writeAll(src)
}

private suspend fun OutputStream.writeAll(chan: SdkByteReadChannel): Long =
sink().use {
chan.readAll(it)
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.OutputStream
import java.nio.file.Files
import kotlin.test.*

Expand Down Expand Up @@ -186,4 +187,38 @@ class ByteStreamJVMTest {
assertContentEquals(binaryData, output)
}
}

@Test
fun testWriteToByteStreamClosesOutput() = runTest {
val byteStream = ByteStream.fromString("Hello")

val sos = StatusTrackingOutputStream(ByteArrayOutputStream())

assertFalse(sos.closed)
byteStream.writeToOutputStream(sos)
assertTrue(sos.closed)
}

@Test
fun testAppendToByteStreamDoesNotCloseOutput() = runTest {
val byteStream = ByteStream.fromString("Don't close me!")

val sos = StatusTrackingOutputStream(ByteArrayOutputStream())

assertFalse(sos.closed)
byteStream.appendToOutputStream(sos)
assertFalse(sos.closed)
}

private class StatusTrackingOutputStream(val os: OutputStream) : OutputStream() {
var closed: Boolean = false

override fun write(b: Int) {
os.write(b)
}

override fun close() {
closed = true
}
}
}
Loading