diff --git a/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs b/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs index 9d836c0b98b2..61accbf8996e 100644 --- a/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs +++ b/src/System.Private.CoreLib/shared/System/IO/StreamReader.cs @@ -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); } diff --git a/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs b/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs index 3a999195bc1d..856dd27280de 100644 --- a/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs +++ b/src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs @@ -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); } diff --git a/tests/CoreFX/CoreFX.issues.json b/tests/CoreFX/CoreFX.issues.json index e8428f5a928b..5fb301498fa5 100644 --- a/tests/CoreFX/CoreFX.issues.json +++ b/tests/CoreFX/CoreFX.issues.json @@ -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" } ] }