forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyStreamWithEncodingPreamble.cs
More file actions
175 lines (148 loc) · 5.44 KB
/
Copy pathReadOnlyStreamWithEncodingPreamble.cs
File metadata and controls
175 lines (148 loc) · 5.44 KB
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
// 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.Diagnostics.Contracts;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http.Internal
{
/// <summary>
/// This implements a read-only, forward-only stream around another readable stream, to ensure
/// that there is an appropriate encoding preamble in the stream.
/// </summary>
internal class ReadOnlyStreamWithEncodingPreamble : Stream
{
private static Task<int> _cancelledTask = GetCancelledTask();
private Stream _innerStream;
private ArraySegment<byte> _remainingBytes;
public ReadOnlyStreamWithEncodingPreamble(Stream innerStream, Encoding encoding)
{
Contract.Assert(innerStream != null);
Contract.Assert(innerStream.CanRead);
Contract.Assert(encoding != null);
_innerStream = innerStream;
// Determine whether we even have a preamble to be concerned about
byte[] preamble = encoding.GetPreamble();
int preambleLength = preamble.Length;
if (preambleLength <= 0)
{
return;
}
// Create a double sized buffer, and read enough bytes from the stream to know
// whether we have a preamble present already or not.
int finalBufferLength = preambleLength * 2;
byte[] finalBuffer = new byte[finalBufferLength];
int finalCount = preambleLength;
preamble.CopyTo(finalBuffer, 0);
// Read the first bytes of the stream and see if they already contain a preamble
for (; finalCount < finalBufferLength; finalCount++)
{
int b = innerStream.ReadByte();
if (b == -1)
{
break;
}
finalBuffer[finalCount] = (byte)b;
}
// Did we read enough bytes to do the comparison?
if (finalCount == finalBufferLength)
{
bool foundPreamble = true;
for (int idx = 0; idx < preambleLength; idx++)
{
if (finalBuffer[idx] != finalBuffer[idx + preambleLength])
{
foundPreamble = false;
break;
}
}
// If we found the preamble, then just exclude it from the data that we return
if (foundPreamble)
{
finalCount = preambleLength;
}
}
_remainingBytes = new ArraySegment<byte>(finalBuffer, 0, finalCount);
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override void Flush()
{
throw new NotImplementedException();
}
private static Task<int> GetCancelledTask()
{
var tcs = new TaskCompletionSource<int>();
tcs.SetCanceled();
return tcs.Task;
}
public override int Read(byte[] buffer, int offset, int count)
{
byte[] remainingArray = _remainingBytes.Array;
if (remainingArray == null)
{
return _innerStream.Read(buffer, offset, count);
}
int remainingCount = _remainingBytes.Count;
int remainingOffset = _remainingBytes.Offset;
int result = Math.Min(count, remainingCount);
for (int idx = 0; idx < result; ++idx)
{
buffer[offset + idx] = remainingArray[remainingOffset + idx];
}
if (result == remainingCount)
{
_remainingBytes = default(ArraySegment<byte>);
}
else
{
_remainingBytes = new ArraySegment<byte>(remainingArray, remainingOffset + result, remainingCount - result);
}
return result;
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
if (_remainingBytes.Array == null)
{
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
}
if (cancellationToken.IsCancellationRequested)
{
return _cancelledTask;
}
return Task.FromResult(Read(buffer, offset, count));
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}
}