Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 196c836

Browse files
committed
Merge pull request #2534 from justinvp/http_streamtostreamcopy
Make StreamToStreamCopy static
2 parents 6d01de0 + 1bb48a7 commit 196c836

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

src/System.Net.Http/src/System/Net/Http/StreamContent.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ protected override Task SerializeToStreamAsync(Stream stream, TransportContext c
4949

5050
PrepareContent();
5151
// If the stream can't be re-read, make sure that it gets disposed once it is consumed.
52-
StreamToStreamCopy sc = new StreamToStreamCopy(_content, stream, _bufferSize, !_content.CanSeek);
53-
return sc.StartAsync();
52+
return StreamToStreamCopy.CopyAsync(_content, stream, _bufferSize, !_content.CanSeek);
5453
}
5554

5655
protected internal override bool TryComputeLength(out long length)

src/System.Net.Http/src/System/Net/Http/StreamToStreamCopy.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,52 @@
88
namespace System.Net.Http
99
{
1010
// This helper class is used to copy the content of a source stream to a destination stream.
11-
// The type verifies if the source and/or destination stream are MemoryStreams (or derived types). If so, sync
11+
// The type verifies if the source and/or destination stream are MemoryStreams (or derived types). If so, sync
1212
// read/write is used on the MemoryStream to avoid context switches.
13-
internal class StreamToStreamCopy
13+
internal static class StreamToStreamCopy
1414
{
15-
private byte[] _buffer;
16-
private int _bufferSize;
17-
private Stream _source;
18-
private Stream _destination;
19-
private bool _disposeSource;
20-
private bool _sourceIsMemoryStream;
21-
private bool _destinationIsMemoryStream;
22-
23-
public StreamToStreamCopy(Stream source, Stream destination, int bufferSize, bool disposeSource)
15+
public static async Task CopyAsync(Stream source, Stream destination, int bufferSize, bool disposeSource)
2416
{
2517
Contract.Requires(source != null);
2618
Contract.Requires(destination != null);
2719
Contract.Requires(bufferSize > 0);
2820

29-
_buffer = new byte[bufferSize];
30-
_source = source;
31-
_destination = destination;
32-
_bufferSize = bufferSize;
33-
_disposeSource = disposeSource;
34-
_sourceIsMemoryStream = source is MemoryStream;
35-
_destinationIsMemoryStream = destination is MemoryStream;
36-
}
21+
byte[] buffer = new byte[bufferSize];
3722

38-
public async Task StartAsync()
39-
{
4023
// If both streams are MemoryStreams, just copy the whole content at once to avoid context switches.
4124
// This will not block since it will just result in a memcopy.
42-
if (_sourceIsMemoryStream && _destinationIsMemoryStream)
25+
if (source is MemoryStream && destination is MemoryStream)
4326
{
4427
for (; ;)
4528
{
46-
int bytesRead = _source.Read(_buffer, 0, _bufferSize);
29+
int bytesRead = source.Read(buffer, 0, bufferSize);
4730
if (bytesRead == 0)
4831
break;
49-
_destination.Write(_buffer, 0, bytesRead);
32+
destination.Write(buffer, 0, bytesRead);
5033
}
5134
}
5235
else
5336
{
5437
for (; ;)
5538
{
56-
int bytesRead = await _source.ReadAsync(_buffer, 0, _bufferSize).ConfigureAwait(false);
39+
int bytesRead = await source.ReadAsync(buffer, 0, bufferSize).ConfigureAwait(false);
5740
if (bytesRead == 0)
5841
break;
59-
await _destination.WriteAsync(_buffer, 0, bytesRead).ConfigureAwait(false);
42+
await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
6043
}
6144
}
6245

6346
try
6447
{
65-
if (_disposeSource)
48+
if (disposeSource)
6649
{
67-
_source.Dispose();
50+
source.Dispose();
6851
}
6952
}
7053
catch (Exception e)
7154
{
7255
// Dispose() should never throw, but since we're on an async codepath, make sure to catch the exception.
73-
if (Logging.On) Logging.Exception(Logging.Http, this, "SetCompleted", e);
56+
if (Logging.On) Logging.Exception(Logging.Http, null, "CopyAsync", e);
7457
}
7558
}
7659
}

0 commit comments

Comments
 (0)