| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.IO.Pipelines; |
| | | 7 | | using System.Net; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace 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 | | |
| | 1 | 20 | | public PooledByteBufferWriter(int initialCapacity) |
| | 1 | 21 | | { |
| | 1 | 22 | | _buffer = new ArrayBuffer(initialCapacity, usePool: true); |
| | 1 | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | public PooledByteBufferWriter(int initialCapacity, Stream stream) : this(initialCapacity) |
| | 0 | 26 | | { |
| | 0 | 27 | | _stream = stream; |
| | 0 | 28 | | } |
| | | 29 | | |
| | 28 | 30 | | public ReadOnlySpan<byte> WrittenSpan => _buffer.ActiveSpan; |
| | | 31 | | |
| | 0 | 32 | | public ReadOnlyMemory<byte> WrittenMemory => _buffer.ActiveMemory; |
| | | 33 | | |
| | 0 | 34 | | public int Capacity => _buffer.Capacity; |
| | | 35 | | |
| | 0 | 36 | | public void Clear() => _buffer.Discard(_buffer.ActiveLength); |
| | | 37 | | |
| | 28 | 38 | | public void ClearAndReturnBuffers() => _buffer.ClearAndReturnBuffer(); |
| | | 39 | | |
| | 0 | 40 | | public void Dispose() => _buffer.Dispose(); |
| | | 41 | | |
| | | 42 | | public void InitializeEmptyInstance(int initialCapacity) |
| | 28 | 43 | | { |
| | 28 | 44 | | Debug.Assert(initialCapacity > 0); |
| | 28 | 45 | | Debug.Assert(_buffer.ActiveLength == 0); |
| | | 46 | | |
| | 28 | 47 | | _buffer.EnsureAvailableSpace(initialCapacity); |
| | 28 | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | public static PooledByteBufferWriter CreateEmptyInstanceForCaching() => new PooledByteBufferWriter(initialCapaci |
| | | 51 | | |
| | 28 | 52 | | public override void Advance(int count) => _buffer.Commit(count); |
| | | 53 | | |
| | | 54 | | public override Memory<byte> GetMemory(int sizeHint = MinimumBufferSize) |
| | 28 | 55 | | { |
| | 28 | 56 | | Debug.Assert(sizeHint > 0); |
| | | 57 | | |
| | 28 | 58 | | _buffer.EnsureAvailableSpace(sizeHint); |
| | 28 | 59 | | return _buffer.AvailableMemory; |
| | 28 | 60 | | } |
| | | 61 | | |
| | | 62 | | public override Span<byte> GetSpan(int sizeHint = MinimumBufferSize) |
| | 0 | 63 | | { |
| | 0 | 64 | | Debug.Assert(sizeHint > 0); |
| | | 65 | | |
| | 0 | 66 | | _buffer.EnsureAvailableSpace(sizeHint); |
| | 0 | 67 | | return _buffer.AvailableSpan; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | #if NET |
| | 0 | 71 | | 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) |
| | 0 | 77 | | { |
| | 0 | 78 | | Debug.Assert(_stream is not null); |
| | 0 | 79 | | await _stream.WriteAsync(WrittenMemory, cancellationToken).ConfigureAwait(false); |
| | 0 | 80 | | Clear(); |
| | | 81 | | |
| | 0 | 82 | | return new FlushResult(isCanceled: false, isCompleted: false); |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | public override bool CanGetUnflushedBytes => true; |
| | 0 | 86 | | 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 |
| | 0 | 90 | | public override void CancelPendingFlush() => throw new NotImplementedException(); |
| | 0 | 91 | | public override void Complete(Exception? exception = null) => throw new NotImplementedException(); |
| | | 92 | | } |
| | | 93 | | } |