< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 99
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%110%
Clear()100%110%
ClearAndReturnBuffers()100%110%
Dispose()100%110%
InitializeEmptyInstance(...)100%110%
CreateEmptyInstanceForCaching()100%110%
Advance(...)100%110%
GetMemory(...)100%110%
GetSpan(...)100%110%
WriteToStream(...)100%110%
FlushAsync()100%110%
CancelPendingFlush()100%110%
Complete(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Text/Json/PooledByteBufferWriter.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.IO;
 6using System.IO.Pipelines;
 7using System.Net;
 8using System.Threading;
 9using System.Threading.Tasks;
 10
 11namespace System.Text.Json
 12{
 13    internal sealed class PooledByteBufferWriter : PipeWriter, IDisposable
 14    {
 15        private const int MinimumBufferSize = 256;
 16
 17        private ArrayBuffer _buffer;
 18        private readonly Stream? _stream;
 19
 020        public PooledByteBufferWriter(int initialCapacity)
 021        {
 022            _buffer = new ArrayBuffer(initialCapacity, usePool: true);
 023        }
 24
 025        public PooledByteBufferWriter(int initialCapacity, Stream stream) : this(initialCapacity)
 026        {
 027            _stream = stream;
 028        }
 29
 030        public ReadOnlySpan<byte> WrittenSpan => _buffer.ActiveSpan;
 31
 032        public ReadOnlyMemory<byte> WrittenMemory => _buffer.ActiveMemory;
 33
 034        public int Capacity => _buffer.Capacity;
 35
 036        public void Clear() => _buffer.DiscardAll();
 37
 038        public void ClearAndReturnBuffers() => _buffer.ClearAndReturnBuffer();
 39
 040        public void Dispose() => _buffer.Dispose();
 41
 42        public void InitializeEmptyInstance(int initialCapacity)
 043        {
 044            Debug.Assert(initialCapacity > 0);
 045            Debug.Assert(_buffer.ActiveLength == 0);
 46
 047            _buffer.EnsureAvailableSpace(initialCapacity);
 048        }
 49
 050        public static PooledByteBufferWriter CreateEmptyInstanceForCaching() => new PooledByteBufferWriter(initialCapaci
 51
 052        public override void Advance(int count) => _buffer.Commit(count);
 53
 54        public override Memory<byte> GetMemory(int sizeHint = MinimumBufferSize)
 055        {
 056            Debug.Assert(sizeHint > 0);
 57
 058            _buffer.EnsureAvailableSpace(sizeHint);
 059            return _buffer.AvailableMemory;
 060        }
 61
 62        public override Span<byte> GetSpan(int sizeHint = MinimumBufferSize)
 063        {
 064            Debug.Assert(sizeHint > 0);
 65
 066            _buffer.EnsureAvailableSpace(sizeHint);
 067            return _buffer.AvailableSpan;
 068        }
 69
 70#if NET
 071        internal void WriteToStream(Stream destination) => destination.Write(_buffer.ActiveSpan);
 72#else
 73        internal void WriteToStream(Stream destination) => destination.Write(_buffer.ActiveMemory);
 74#endif
 75
 76        public override async ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken = default)
 077        {
 078            Debug.Assert(_stream is not null);
 079            await _stream.WriteAsync(WrittenMemory, cancellationToken).ConfigureAwait(false);
 080            Clear();
 81
 82            // Flushing a PipeWriter makes the written data available to the reader, so do the equivalent
 83            // for the destination stream. This ensures that consumers of streaming payloads (e.g. values
 84            // of an IAsyncEnumerable) aren't held back by any intermediate buffering.
 085            await _stream.FlushAsync(cancellationToken).ConfigureAwait(false);
 86
 087            return new FlushResult(isCanceled: false, isCompleted: false);
 088        }
 89
 090        public override bool CanGetUnflushedBytes => true;
 091        public override long UnflushedBytes => _buffer.ActiveLength;
 92
 93        // This type is used internally in JsonSerializer to help buffer and flush bytes to the underlying Stream.
 94        // It's only pretending to be a PipeWriter and doesn't need Complete or CancelPendingFlush for the internal usag
 095        public override void CancelPendingFlush() => throw new NotImplementedException();
 096        public override void Complete(Exception? exception = null) => throw new NotImplementedException();
 97    }
 98}
 99