-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Utf8MemoryStream.cs
32 lines (26 loc) · 1.23 KB
/
Utf8MemoryStream.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace System.Text.Json.Serialization.Tests
{
public sealed class Utf8MemoryStream : MemoryStream
{
private readonly bool _ignoreCancellationTokenOnWriteAsync;
public Utf8MemoryStream(bool ignoreCancellationTokenOnWriteAsync = false) : base()
{
_ignoreCancellationTokenOnWriteAsync = ignoreCancellationTokenOnWriteAsync;
}
public Utf8MemoryStream(string text) : base(Encoding.UTF8.GetBytes(text))
{
}
#if NETCOREAPP
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
=> base.WriteAsync(buffer, _ignoreCancellationTokenOnWriteAsync ? default : cancellationToken);
#endif
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> base.WriteAsync(buffer, offset, count, _ignoreCancellationTokenOnWriteAsync ? default : cancellationToken);
public string AsString() => Encoding.UTF8.GetString(ToArray());
}
}