示例#1
0
        /// <summary>
        /// Returns writable stream associated with the buffer writer.
        /// </summary>
        /// <typeparam name="TWriter">The type of the writer.</typeparam>
        /// <param name="writer">The writer to be wrapped by the stream.</param>
        /// <param name="flush">Optional synchronous flush action.</param>
        /// <param name="flushAsync">Optiona asynchronous flush action.</param>
        /// <returns>The writable stream wrapping buffer writer.</returns>
        public static Stream AsStream <TWriter>(this TWriter writer, Action <TWriter>?flush = null, Func <TWriter, CancellationToken, Task>?flushAsync = null)
            where TWriter : class, IBufferWriter <byte>
        {
            flush ??= IFlushable.TryReflectFlushMethod(writer);
            flushAsync ??= IFlushable.TryReflectAsyncFlushMethod(writer);

            return(AsStream(Span.CopyTo <byte>, writer, flush, flushAsync));
        }
示例#2
0
        /// <summary>
        /// Creates text writer backed by the char buffer writer.
        /// </summary>
        /// <param name="writer">The buffer writer.</param>
        /// <param name="provider">The object that controls formatting.</param>
        /// <param name="flush">The optional implementation of <see cref="TextWriter.Flush"/> method.</param>
        /// <param name="flushAsync">The optional implementation of <see cref="TextWriter.FlushAsync"/> method.</param>
        /// <typeparam name="TWriter">The type of the char buffer writer.</typeparam>
        /// <returns>The text writer backed by the buffer writer.</returns>
        public static TextWriter AsTextWriter <TWriter>(this TWriter writer, IFormatProvider?provider = null, Action <TWriter>?flush = null, Func <TWriter, CancellationToken, Task>?flushAsync = null)
            where TWriter : class, IBufferWriter <char>
        {
            flush ??= IFlushable.TryReflectFlushMethod(writer);
            flushAsync ??= IFlushable.TryReflectAsyncFlushMethod(writer);

            return(new TextBufferWriter <TWriter>(writer, provider, flush, flushAsync));
        }
示例#3
0
        /// <summary>
        /// Returns writable stream associated with the buffer writer.
        /// </summary>
        /// <typeparam name="TWriter">The type of the writer.</typeparam>
        /// <param name="writer">The writer to be wrapped by the stream.</param>
        /// <param name="flush">Optional synchronous flush action.</param>
        /// <param name="flushAsync">Optiona asynchronous flush action.</param>
        /// <returns>The writable stream wrapping buffer writer.</returns>
        public static Stream AsStream <TWriter>(this TWriter writer, Action <TWriter>?flush = null, Func <TWriter, CancellationToken, Task>?flushAsync = null)
            where TWriter : class, IBufferWriter <byte>
        {
            flush ??= IFlushable.TryReflectFlushMethod(writer);
            flushAsync ??= IFlushable.TryReflectAsyncFlushMethod(writer);

            var callback = writer is IGrowableBuffer <byte>?
                           new ReadOnlySpanAction <byte, TWriter>(WriteToGrowableBuffer) :
                               new ReadOnlySpanAction <byte, TWriter>(Span.CopyTo <byte>);

            return(AsStream(callback, writer, flush, flushAsync));
示例#4
0
 internal AsyncBufferWriter(IBufferWriter <byte> writer)
 {
     this.writer = writer ?? throw new ArgumentNullException(nameof(writer));
     flush       = IFlushable.TryReflectAsyncFlushMethod(writer);
 }