Skip to content

Commit

Permalink
Added optional/default parameters for StreamWriter/StreamReader (dotn…
Browse files Browse the repository at this point in the history
…et/coreclr#24056)

* Added optional/default parameters for StreamWriter/StreamReader

* Disabled outdated test

* Changed default encoding to UTF8NoBOM

* Made encoding parameter nullable


Commit migrated from dotnet/coreclr@57a40b9
  • Loading branch information
jdemis authored and stephentoub committed Apr 25, 2019
1 parent 77b88c2 commit 9683fbe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/coreclr/tests/CoreFX/CoreFX.issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@
{
"name": "System.IO.Tests.StreamWriterWithBufferedStream_CloseTests.AfterCloseThrows",
"reason": "Readers/writers changed to not null out base stream on dispose"
},
{
"name": "System.IO.Tests.CtorTests.NullEncodingThrows",
"reason": "StreamReader/StreamWriter changed to accept null encoding"
}
]
}
Expand Down
16 changes: 12 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,25 @@ public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByt
{
}

public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
public StreamReader(Stream stream, Encoding? encoding = null, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false)
{
if (stream == null || encoding == null)
if (stream == null)
{
throw new ArgumentNullException(stream == null ? nameof(stream) : nameof(encoding));
throw new ArgumentNullException(nameof(stream));
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
if (!stream.CanRead)
{
throw new ArgumentException(SR.Argument_StreamNotReadable);
}
if (bufferSize <= 0)
if (bufferSize == -1)
{
bufferSize = DefaultBufferSize;
}
else if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
}
Expand Down
16 changes: 12 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ public StreamWriter(Stream stream, Encoding encoding, int bufferSize)
{
}

public StreamWriter(Stream stream, Encoding encoding, int bufferSize, bool leaveOpen)
public StreamWriter(Stream stream, Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false)
: base(null) // Ask for CurrentCulture all the time
{
if (stream == null || encoding == null)
if (stream == null)
{
throw new ArgumentNullException(stream == null ? nameof(stream) : nameof(encoding));
throw new ArgumentNullException(nameof(stream));
}
if (encoding == null)
{
encoding = UTF8NoBOM;
}
if (!stream.CanWrite)
{
throw new ArgumentException(SR.Argument_StreamNotWritable);
}
if (bufferSize <= 0)
if (bufferSize == -1)
{
bufferSize = DefaultBufferSize;
}
else if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
}
Expand Down

0 comments on commit 9683fbe

Please sign in to comment.