< Summary

Information
Class: System.Text.Json.PooledByteBufferWriter
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\Common\src\System\Text\Json\PooledByteBufferWriter.cs
Line coverage
42%
Covered lines: 18
Uncovered lines: 24
Coverable lines: 42
Total lines: 93
Line coverage: 42.8%
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%11100%
.ctor(...)100%110%
Clear()100%110%
ClearAndReturnBuffers()100%11100%
Dispose()100%110%
InitializeEmptyInstance(...)100%11100%
CreateEmptyInstanceForCaching()100%11100%
Advance(...)100%11100%
GetMemory(...)100%11100%
GetSpan(...)100%110%
WriteToStream(...)100%110%
FlushAsync()100%110%
CancelPendingFlush()100%110%
Complete(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\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
 120        public PooledByteBufferWriter(int initialCapacity)
 121        {
 122            _buffer = new ArrayBuffer(initialCapacity, usePool: true);
 123        }
 24
 025        public PooledByteBufferWriter(int initialCapacity, Stream stream) : this(initialCapacity)
 026        {
 027            _stream = stream;
 028        }
 29
 2830        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.Discard(_buffer.ActiveLength);
 37
 2838        public void ClearAndReturnBuffers() => _buffer.ClearAndReturnBuffer();
 39
 040        public void Dispose() => _buffer.Dispose();
 41
 42        public void InitializeEmptyInstance(int initialCapacity)
 2843        {
 2844            Debug.Assert(initialCapacity > 0);
 2845            Debug.Assert(_buffer.ActiveLength == 0);
 46
 2847            _buffer.EnsureAvailableSpace(initialCapacity);
 2848        }
 49
 150        public static PooledByteBufferWriter CreateEmptyInstanceForCaching() => new PooledByteBufferWriter(initialCapaci
 51
 2852        public override void Advance(int count) => _buffer.Commit(count);
 53
 54        public override Memory<byte> GetMemory(int sizeHint = MinimumBufferSize)
 2855        {
 2856            Debug.Assert(sizeHint > 0);
 57
 2858            _buffer.EnsureAvailableSpace(sizeHint);
 2859            return _buffer.AvailableMemory;
 2860        }
 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
 082            return new FlushResult(isCanceled: false, isCompleted: false);
 083        }
 84
 085        public override bool CanGetUnflushedBytes => true;
 086        public override long UnflushedBytes => _buffer.ActiveLength;
 87
 88        // This type is used internally in JsonSerializer to help buffer and flush bytes to the underlying Stream.
 89        // It's only pretending to be a PipeWriter and doesn't need Complete or CancelPendingFlush for the internal usag
 090        public override void CancelPendingFlush() => throw new NotImplementedException();
 091        public override void Complete(Exception? exception = null) => throw new NotImplementedException();
 92    }
 93}