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

Commit c28fbce

Browse files
rmkerrstephentoub
authored andcommitted
Modify StreamContent to use default CopyToAsync buffer size when no size is provided (#27176)
* Add a tentative fix * Minor fixes. * Remove unused default value. * Re-order error checks for compatibility reasons. * Add a test to pin exception behavior when both parameters are incorrect.
1 parent a9f4a46 commit c28fbce

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ namespace System.Net.Http
1111
{
1212
public class StreamContent : HttpContent
1313
{
14-
private const int DefaultBufferSize = 4096;
15-
1614
private Stream _content;
1715
private int _bufferSize;
1816
private bool _contentConsumed;
1917
private long _start;
2018

2119
public StreamContent(Stream content)
22-
: this(content, DefaultBufferSize)
2320
{
21+
if (content == null)
22+
{
23+
throw new ArgumentNullException(nameof(content));
24+
}
25+
26+
// Indicate that we should use default buffer size by setting size to 0.
27+
InitializeContent(content, 0);
2428
}
2529

2630
public StreamContent(Stream content, int bufferSize)
@@ -34,6 +38,11 @@ public StreamContent(Stream content, int bufferSize)
3438
throw new ArgumentOutOfRangeException(nameof(bufferSize));
3539
}
3640

41+
InitializeContent(content, bufferSize);
42+
}
43+
44+
private void InitializeContent(Stream content, int bufferSize)
45+
{
3746
_content = content;
3847
_bufferSize = bufferSize;
3948
if (content.CanSeek)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,20 @@ internal static class StreamToStreamCopy
1919
/// <summary>Copies the source stream from its current position to the destination stream at its current position.</summary>
2020
/// <param name="source">The source stream from which to copy.</param>
2121
/// <param name="destination">The destination stream to which to copy.</param>
22-
/// <param name="bufferSize">The size of the buffer to allocate if one needs to be allocated.</param>
22+
/// <param name="bufferSize">The size of the buffer to allocate if one needs to be allocated. If zero, use the default buffer size.</param>
2323
/// <param name="disposeSource">Whether to dispose of the source stream after the copy has finished successfully.</param>
2424
/// <param name="cancellationToken">CancellationToken used to cancel the copy operation.</param>
2525
public static Task CopyAsync(Stream source, Stream destination, int bufferSize, bool disposeSource, CancellationToken cancellationToken = default(CancellationToken))
2626
{
2727
Debug.Assert(source != null);
2828
Debug.Assert(destination != null);
29-
Debug.Assert(bufferSize > 0);
29+
Debug.Assert(bufferSize >= 0);
3030

3131
try
3232
{
33-
Task copyTask = source.CopyToAsync(destination, bufferSize, cancellationToken);
33+
Task copyTask = bufferSize == 0 ?
34+
source.CopyToAsync(destination, cancellationToken) :
35+
source.CopyToAsync(destination, bufferSize, cancellationToken);
3436
return disposeSource ?
3537
DisposeSourceWhenCompleteAsync(copyTask, source) :
3638
copyTask;

src/System.Net.Http/tests/FunctionalTests/StreamContentTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public void Ctor_ZeroBufferSize_ThrowsArgumentOutOfRangeException()
3232
Assert.Throws<ArgumentOutOfRangeException>(() => new StreamContent(new MemoryStream(), 0));
3333
}
3434

35+
[Fact]
36+
public void Ctor_NullStreamAndZeroBufferSize_ThrowsArgumentNullException()
37+
{
38+
Assert.Throws<ArgumentNullException>(() => new StreamContent(null, 0));
39+
}
40+
3541
[Fact]
3642
public void ContentLength_SetStreamSupportingSeeking_StreamLengthMatchesHeaderValue()
3743
{

0 commit comments

Comments
 (0)