-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7327 Support conversion between byte channel interfaces and kotl…
…inx-io primitives (#4594) * KTOR-7327 Add Sink, RawSink, OutputStream to ByteWriteChannel converters * Replace deprecated testSuspend usages * KTOR-7327 Add ByteReadChannel to RawSource converter * KTOR-7327 Add ByteWriteChannel to RawSink converter * Update API Dump * Update ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt Co-authored-by: Jake Wharton <[email protected]> * Update ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt Co-authored-by: Jake Wharton <[email protected]> * Fix review issues * Fix refactoring issues * fixup! Merge branch 'main' into e5l/streams-bc --------- Co-authored-by: Jake Wharton <[email protected]>
- Loading branch information
1 parent
9535a8e
commit 18c18fe
Showing
16 changed files
with
484 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -385,6 +385,7 @@ final fun (kotlinx.coroutines/CoroutineScope).io.ktor.utils.io/writer(kotlin.cor | |
final fun (kotlinx.coroutines/CoroutineScope).io.ktor.utils.io/writer(kotlin.coroutines/CoroutineContext = ..., kotlin/Boolean = ..., kotlin.coroutines/SuspendFunction1<io.ktor.utils.io/WriterScope, kotlin/Unit>): io.ktor.utils.io/WriterJob // io.ktor.utils.io/writer|[email protected](kotlin.coroutines.CoroutineContext;kotlin.Boolean;kotlin.coroutines.SuspendFunction1<io.ktor.utils.io.WriterScope,kotlin.Unit>){}[0] | ||
final fun (kotlinx.io/Buffer).io.ktor.utils.io.core/canRead(): kotlin/Boolean // io.ktor.utils.io.core/canRead|[email protected](){}[0] | ||
final fun (kotlinx.io/Buffer).io.ktor.utils.io.core/readBytes(kotlin/Int = ...): kotlin/ByteArray // io.ktor.utils.io.core/readBytes|[email protected](kotlin.Int){}[0] | ||
final fun (kotlinx.io/RawSink).io.ktor.utils.io/asByteWriteChannel(): io.ktor.utils.io/ByteWriteChannel // io.ktor.utils.io/asByteWriteChannel|[email protected](){}[0] | ||
final fun (kotlinx.io/Sink).io.ktor.utils.io.core/append(kotlin/CharSequence, kotlin/Int = ..., kotlin/Int = ...) // io.ktor.utils.io.core/append|[email protected](kotlin.CharSequence;kotlin.Int;kotlin.Int){}[0] | ||
final fun (kotlinx.io/Sink).io.ktor.utils.io.core/build(): kotlinx.io/Source // io.ktor.utils.io.core/build|[email protected](){}[0] | ||
final fun (kotlinx.io/Sink).io.ktor.utils.io.core/writeFully(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...) // io.ktor.utils.io.core/writeFully|[email protected](kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] | ||
|
@@ -604,6 +605,12 @@ sealed class io.ktor.utils.io.errors/PosixException : kotlin/Exception { // io.k | |
} | ||
} | ||
|
||
// Targets: [native] | ||
final fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/asSource(): kotlinx.io/RawSource // io.ktor.utils.io/asSource|[email protected](){}[0] | ||
|
||
// Targets: [native] | ||
final fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/asSink(): kotlinx.io/RawSink // io.ktor.utils.io/asSink|[email protected](){}[0] | ||
|
||
// Targets: [native] | ||
final fun (kotlinx.io/Sink).io.ktor.utils.io.core/write(kotlin/Function3<kotlinx.cinterop/CPointer<kotlinx.cinterop/ByteVarOf<kotlin/Byte>>, kotlin/Long, kotlin/Long, kotlin/Long>): kotlin/Long // io.ktor.utils.io.core/write|[email protected](kotlin.Function3<kotlinx.cinterop.CPointer<kotlinx.cinterop.ByteVarOf<kotlin.Byte>>,kotlin.Long,kotlin.Long,kotlin.Long>){}[0] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
ktor-io/common/src/io/ktor/utils/io/SinkByteWriteChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.utils.io | ||
|
||
import kotlinx.atomicfu.AtomicRef | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.io.* | ||
|
||
/** | ||
* Creates a [ByteWriteChannel] that writes to this [Sink]. | ||
* | ||
* Example usage: | ||
* ```kotlin | ||
* suspend fun writeMessage(raw: RawSink) { | ||
* val channel = raw.asByteWriteChannel() | ||
* channel.writeByte(42) | ||
* channel.flushAndClose() | ||
* } | ||
* | ||
* val buffer = Buffer() | ||
* writeMessage(buffer) | ||
* buffer.readByte() // 42 | ||
* ``` | ||
* | ||
* Please note that the channel will be buffered even if the sink is not. | ||
*/ | ||
public fun RawSink.asByteWriteChannel(): ByteWriteChannel = SinkByteWriteChannel(this) | ||
|
||
internal class SinkByteWriteChannel(origin: RawSink) : ByteWriteChannel { | ||
val closed: AtomicRef<CloseToken?> = atomic(null) | ||
private val buffer = origin.buffered() | ||
|
||
override val isClosedForWrite: Boolean | ||
get() = closed.value != null | ||
|
||
override val closedCause: Throwable? | ||
get() = closed.value?.cause | ||
|
||
@InternalAPI | ||
override val writeBuffer: Sink | ||
get() { | ||
if (isClosedForWrite) throw closedCause ?: IOException("Channel is closed for write") | ||
return buffer | ||
} | ||
|
||
@OptIn(InternalAPI::class) | ||
override suspend fun flush() { | ||
writeBuffer.flush() | ||
} | ||
|
||
@OptIn(InternalAPI::class) | ||
override suspend fun flushAndClose() { | ||
writeBuffer.flush() | ||
if (!closed.compareAndSet(expect = null, update = CLOSED)) return | ||
} | ||
|
||
@OptIn(InternalAPI::class) | ||
override fun cancel(cause: Throwable?) { | ||
val token = if (cause == null) CLOSED else CloseToken(cause) | ||
if (!closed.compareAndSet(expect = null, update = token)) return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.