scoop:scoop.http.asyncio:0.1.0
src/multipart/sink.scoop
Selected production source · src/multipart/sink.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer7
/**8
* A sequential multipart writer over a borrowed sink by default. Finish each part and then the9
* whole multipart message explicitly.10
*/11
public class MultipartSink private constructor(private val writer: MultipartWriter) : AsyncCloseable {12
public constructor(sink: AsyncByteSink, boundary: MultipartBoundary, bufferSize: Long = 8192L)13
: this(createMultipartWriter(sink, None, boundary, bufferSize)) {}15
/**16
* Factories that transfer underlying endpoint cleanup responsibility.17
*/18
public companion object {19
/**20
* Creates the adapter and assumes cleanup responsibility for the supplied asynchronous21
* endpoint.22
*/23
public fun <T> takeOwnership(sink: T, boundary: MultipartBoundary, bufferSize: Long = 8192L): MultipartSink24
where T : AsyncByteSink, T : AsyncCloseable =25
MultipartSink(createMultipartWriter(sink, Some<AsyncCloseable>(sink), boundary, bufferSize))26
}28
/**29
* Whether adapter cleanup has completed.30
*/31
public val isClosed: Boolean get() = writer.io.isClosed32
/**33
* Whether the complete protocol boundary or input end has been reached.34
*/35
public val isFinished: Boolean get() = writer.finished36
/**37
* The retained terminal exception, or None before failure.38
*/39
public val failure: Throwable? get() = writer.io.failure40
/**41
* Writes the next part header and returns its body writer; the previous part must already be42
* finished.43
*/44
public suspend fun beginPart(head: MultipartPartHead): MultipartPartWriter = writer.beginPart(head)45
/**46
* Drains pending multipart output without completing a part or the message.47
*/48
public suspend fun flush() { writer.flush(None) }49
/**50
* Writes the final multipart boundary and drains output.51
*/52
public suspend fun finish() { writer.finish() }53
/**54
* Closes parser/writer state and an endpoint whose ownership was transferred; borrowed55
* endpoints retain their own cleanup responsibility.56
*/57
public override suspend fun close() { writer.io.close() }58
}60
private fun createMultipartWriter(sink: AsyncByteSink, owner: AsyncCloseable?, boundary: MultipartBoundary,61
bufferSize: Long): MultipartWriter {62
val loop = currentEventLoop()63
val job = httpJob(loop, true)64
if (bufferSize <= 0L) throw IllegalArgumentException(Some("multipart buffer size must be positive"))65
val encoder = MultipartEncoder(boundary)66
val output = try { NativeByteBuffer.allocate(bufferSize) }67
catch (error: Throwable) { throw messageCleanup(Some(error)) { encoder.close() }!! }68
try {69
job.ensureActive()70
return MultipartWriter(sink, owner, loop, MultipartWriteState(encoder, output))71
} catch (error: Throwable) {72
var failure = messageCleanup(Some(error)) { output.close() }73
failure = messageCleanup(failure) { encoder.close() }74
throw failure!!75
}76
}