This repository was archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathUtf8BufferTextWriter.cs
208 lines (175 loc) · 6.34 KB
/
Utf8BufferTextWriter.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Microsoft.AspNetCore.Internal
{
internal sealed class Utf8BufferTextWriter : TextWriter
{
private static readonly UTF8Encoding _utf8NoBom = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
private static readonly int MaximumBytesPerUtf8Char = 4;
[ThreadStatic]
private static Utf8BufferTextWriter _cachedInstance;
private readonly Encoder _encoder;
private IBufferWriter<byte> _bufferWriter;
private Memory<byte> _memory;
private int _memoryUsed;
#if DEBUG
private bool _inUse;
#endif
public override Encoding Encoding => _utf8NoBom;
public Utf8BufferTextWriter()
{
_encoder = _utf8NoBom.GetEncoder();
}
public static Utf8BufferTextWriter Get(IBufferWriter<byte> bufferWriter)
{
var writer = _cachedInstance;
if (writer == null)
{
writer = new Utf8BufferTextWriter();
}
// Taken off the thread static
_cachedInstance = null;
#if DEBUG
if (writer._inUse)
{
throw new InvalidOperationException("The writer wasn't returned!");
}
writer._inUse = true;
#endif
writer.SetWriter(bufferWriter);
return writer;
}
public static void Return(Utf8BufferTextWriter writer)
{
_cachedInstance = writer;
writer._encoder.Reset();
writer._memory = Memory<byte>.Empty;
writer._memoryUsed = 0;
writer._bufferWriter = null;
#if DEBUG
writer._inUse = false;
#endif
}
public void SetWriter(IBufferWriter<byte> bufferWriter)
{
_bufferWriter = bufferWriter;
}
public override void Write(char[] buffer, int index, int count)
{
WriteInternal(buffer.AsSpan(index, count));
}
public override void Write(char[] buffer)
{
WriteInternal(buffer);
}
public override void Write(char value)
{
if (value <= 127)
{
EnsureBuffer();
// Only need to set one byte
// Avoid Memory<T>.Slice overhead for perf
_memory.Span[_memoryUsed] = (byte)value;
_memoryUsed++;
}
else
{
WriteMultiByteChar(value);
}
}
private unsafe void WriteMultiByteChar(char value)
{
var destination = GetBuffer();
// Json.NET only writes ASCII characters by themselves, e.g. {}[], etc
// this should be an exceptional case
var bytesUsed = 0;
var charsUsed = 0;
#if NETCOREAPP3_0
_encoder.Convert(new Span<char>(&value, 1), destination, false, out charsUsed, out bytesUsed, out _);
#else
fixed (byte* destinationBytes = &MemoryMarshal.GetReference(destination))
{
_encoder.Convert(&value, 1, destinationBytes, destination.Length, false, out charsUsed, out bytesUsed, out _);
}
#endif
Debug.Assert(charsUsed == 1);
_memoryUsed += bytesUsed;
}
public override void Write(string value)
{
WriteInternal(value.AsSpan());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Span<byte> GetBuffer()
{
EnsureBuffer();
return _memory.Span.Slice(_memoryUsed, _memory.Length - _memoryUsed);
}
private void EnsureBuffer()
{
// We need at least enough bytes to encode a single UTF-8 character, or Encoder.Convert will throw.
// Normally, if there isn't enough space to write every character of a char buffer, Encoder.Convert just
// writes what it can. However, if it can't even write a single character, it throws. So if the buffer has only
// 2 bytes left and the next character to write is 3 bytes in UTF-8, an exception is thrown.
var remaining = _memory.Length - _memoryUsed;
if (remaining < MaximumBytesPerUtf8Char)
{
// Used up the memory from the buffer writer so advance and get more
if (_memoryUsed > 0)
{
_bufferWriter.Advance(_memoryUsed);
}
_memory = _bufferWriter.GetMemory(MaximumBytesPerUtf8Char);
_memoryUsed = 0;
}
}
private void WriteInternal(ReadOnlySpan<char> buffer)
{
while (buffer.Length > 0)
{
// The destination byte array might not be large enough so multiple writes are sometimes required
var destination = GetBuffer();
var bytesUsed = 0;
var charsUsed = 0;
#if NETCOREAPP3_0
_encoder.Convert(buffer, destination, false, out charsUsed, out bytesUsed, out _);
#else
unsafe
{
fixed (char* sourceChars = &MemoryMarshal.GetReference(buffer))
fixed (byte* destinationBytes = &MemoryMarshal.GetReference(destination))
{
_encoder.Convert(sourceChars, buffer.Length, destinationBytes, destination.Length, false, out charsUsed, out bytesUsed, out _);
}
}
#endif
buffer = buffer.Slice(charsUsed);
_memoryUsed += bytesUsed;
}
}
public override void Flush()
{
if (_memoryUsed > 0)
{
_bufferWriter.Advance(_memoryUsed);
_memory = _memory.Slice(_memoryUsed, _memory.Length - _memoryUsed);
_memoryUsed = 0;
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
Flush();
}
}
}
}