-
Notifications
You must be signed in to change notification settings - Fork 2
/
PooledBufferWriter.cs
154 lines (125 loc) · 4.15 KB
/
PooledBufferWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace JsonConverterGenerator
{
internal sealed class PooledByteBufferWriter : IBufferWriter<byte>, IDisposable
{
private byte[] _rentedBuffer;
private int _index;
private const int MinimumBufferSize = 256;
public PooledByteBufferWriter(int initialCapacity)
{
Debug.Assert(initialCapacity > 0);
_rentedBuffer = ArrayPool<byte>.Shared.Rent(initialCapacity);
_index = 0;
}
public ReadOnlyMemory<byte> WrittenMemory
{
get
{
Debug.Assert(_rentedBuffer != null);
Debug.Assert(_index <= _rentedBuffer.Length);
return _rentedBuffer.AsMemory(0, _index);
}
}
public int WrittenCount
{
get
{
Debug.Assert(_rentedBuffer != null);
return _index;
}
}
public int Capacity
{
get
{
Debug.Assert(_rentedBuffer != null);
return _rentedBuffer.Length;
}
}
public int FreeCapacity
{
get
{
Debug.Assert(_rentedBuffer != null);
return _rentedBuffer.Length - _index;
}
}
public void Clear()
{
ClearHelper();
}
private void ClearHelper()
{
Debug.Assert(_rentedBuffer != null);
Debug.Assert(_index <= _rentedBuffer.Length);
_rentedBuffer.AsSpan(0, _index).Clear();
_index = 0;
}
// Returns the rented buffer back to the pool
public void Dispose()
{
if (_rentedBuffer == null)
{
return;
}
ClearHelper();
ArrayPool<byte>.Shared.Return(_rentedBuffer);
_rentedBuffer = null;
}
public void Advance(int count)
{
Debug.Assert(_rentedBuffer != null);
Debug.Assert(count >= 0);
Debug.Assert(_index <= _rentedBuffer.Length - count);
_index += count;
}
public Memory<byte> GetMemory(int sizeHint = 0)
{
CheckAndResizeBuffer(sizeHint);
return _rentedBuffer.AsMemory(_index);
}
public Span<byte> GetSpan(int sizeHint = 0)
{
CheckAndResizeBuffer(sizeHint);
return _rentedBuffer.AsSpan(_index);
}
internal ValueTask WriteToStreamAsync(Stream destination, CancellationToken cancellationToken)
{
return destination.WriteAsync(WrittenMemory, cancellationToken);
}
private void CheckAndResizeBuffer(int sizeHint)
{
Debug.Assert(_rentedBuffer != null);
Debug.Assert(sizeHint >= 0);
if (sizeHint == 0)
{
sizeHint = MinimumBufferSize;
}
int availableSpace = _rentedBuffer.Length - _index;
if (sizeHint > availableSpace)
{
int growBy = Math.Max(sizeHint, _rentedBuffer.Length);
int newSize = checked(_rentedBuffer.Length + growBy);
byte[] oldBuffer = _rentedBuffer;
_rentedBuffer = ArrayPool<byte>.Shared.Rent(newSize);
Debug.Assert(oldBuffer.Length >= _index);
Debug.Assert(_rentedBuffer.Length >= _index);
Span<byte> previousBuffer = oldBuffer.AsSpan(0, _index);
previousBuffer.CopyTo(_rentedBuffer);
previousBuffer.Clear();
ArrayPool<byte>.Shared.Return(oldBuffer);
}
Debug.Assert(_rentedBuffer.Length - _index > 0);
Debug.Assert(_rentedBuffer.Length - _index >= sizeHint);
}
}
}