Structure
BufferedMultipartWriter
A
MultipartWriter that buffers data up to a specified capacity before forwarding to an underlying writer.struct BufferedMultipartWriter<UnderlyingWriter> where UnderlyingWriter : MultipartWriter
Overview
This writer acts as a buffer between your application and another writer implementation, helping to optimize memory usage and improve performance when dealing with large multipart messages. It accumulates data until a threshold is reached, then forwards the buffered content to the underlying writer and clears its internal buffer.
// Example: Create a buffered writer with 8KB capacity that writes to a socket
var writer = BufferedMultipartWriter(
boundary: "boundary123",
bufferCapacity: 8192,
underlyingWriter: SocketMultipartWriter(socket: mySocket)
)
// Use the writer as normal - buffering happens automatically
try await writer.writePart(myPart)
try await writer.finish()
Topics
Initializers
init(boundary: String, buffer: BufferedMultipartWriter<UnderlyingWriter>.OutboundBody, bufferCapacity: Int, underlyingWriter: UnderlyingWriter)Creates a new buffered multipart writer with a user-supplied buffer.init(boundary: String, bufferCapacity: Int, underlyingWriter: UnderlyingWriter)Creates a new buffered multipart writer.
Instance Properties
let boundary: StringThe boundary string used to separate multipart parts.
Instance Methods
func finish(writingEndBoundary: Bool) async throws(BufferedMultipartWriter<UnderlyingWriter>.Failure)If the buffer has not been emptied by the last write, flushes the final part of the message to the underlying writer.func write(bytes: some Collection<UInt8> & Sendable) async throws(BufferedMultipartWriter<UnderlyingWriter>.Failure)Writes bytes to the buffer, flushing to the underlying writer if capacity is reached.
Type Aliases
typealias FailureThis writer only throws errors that originate from the underlying writer.typealias OutboundBodyThe body type of the underlying writer, which this writer buffers into before forwarding.
Relationships
Conforms To
MultipartWriterSwift.SendableSwift.SendableMetatype