-
Notifications
You must be signed in to change notification settings - Fork 305
/
IBufferWriterStream{TWriter}.cs
171 lines (141 loc) · 4.62 KB
/
IBufferWriterStream{TWriter}.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
// 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.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace CommunityToolkit.HighPerformance.Streams;
/// <summary>
/// A <see cref="Stream"/> implementation wrapping an <see cref="IBufferWriter{T}"/> instance.
/// </summary>
/// <typeparam name="TWriter">The type of buffer writer to use.</typeparam>
internal sealed partial class IBufferWriterStream<TWriter> : Stream
where TWriter : struct, IBufferWriter<byte>
{
/// <summary>
/// The target <typeparamref name="TWriter"/> instance to use.
/// </summary>
private readonly TWriter bufferWriter;
/// <summary>
/// Indicates whether or not the current instance has been disposed
/// </summary>
private bool disposed;
/// <summary>
/// Initializes a new instance of the <see cref="IBufferWriterStream{TWriter}"/> class.
/// </summary>
/// <param name="bufferWriter">The target <see cref="IBufferWriter{T}"/> instance to use.</param>
public IBufferWriterStream(TWriter bufferWriter)
{
this.bufferWriter = bufferWriter;
}
/// <inheritdoc/>
public override bool CanRead => false;
/// <inheritdoc/>
public override bool CanSeek => false;
/// <inheritdoc/>
public override bool CanWrite
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => !this.disposed;
}
/// <inheritdoc/>
public override long Length => throw MemoryStream.GetNotSupportedException();
/// <inheritdoc/>
public override long Position
{
get => throw MemoryStream.GetNotSupportedException();
set => throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override void Flush()
{
}
/// <inheritdoc/>
public override Task FlushAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
return Task.CompletedTask;
}
/// <inheritdoc/>
public override Task<int> ReadAsync(byte[]? buffer, int offset, int count, CancellationToken cancellationToken)
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override Task WriteAsync(byte[]? buffer, int offset, int count, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled(cancellationToken);
}
try
{
Write(buffer, offset, count);
return Task.CompletedTask;
}
catch (OperationCanceledException e)
{
return Task.FromCanceled(e.CancellationToken);
}
catch (Exception e)
{
return Task.FromException(e);
}
}
/// <inheritdoc/>
public override long Seek(long offset, SeekOrigin origin)
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override void SetLength(long value)
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override int Read(byte[]? buffer, int offset, int count)
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override int ReadByte()
{
throw MemoryStream.GetNotSupportedException();
}
/// <inheritdoc/>
public override void Write(byte[]? buffer, int offset, int count)
{
MemoryStream.ValidateDisposed(this.disposed);
MemoryStream.ValidateBuffer(buffer, offset, count);
Span<byte> source = buffer.AsSpan(offset, count);
Span<byte> destination = this.bufferWriter.GetSpan(count);
if (!source.TryCopyTo(destination))
{
MemoryStream.ThrowArgumentExceptionForEndOfStreamOnWrite();
}
this.bufferWriter.Advance(count);
}
/// <inheritdoc/>
public override void WriteByte(byte value)
{
MemoryStream.ValidateDisposed(this.disposed);
this.bufferWriter.GetSpan(1)[0] = value;
this.bufferWriter.Advance(1);
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
this.disposed = true;
}
}