From 9683fbe7e98cf44dcf48697224e17533c649a037 Mon Sep 17 00:00:00 2001 From: Jim <10913919+jimdemis@users.noreply.github.com> Date: Thu, 25 Apr 2019 05:50:47 +0300 Subject: [PATCH] Added optional/default parameters for StreamWriter/StreamReader (dotnet/coreclr#24056) * Added optional/default parameters for StreamWriter/StreamReader * Disabled outdated test * Changed default encoding to UTF8NoBOM * Made encoding parameter nullable Commit migrated from https://github.com/dotnet/coreclr/commit/57a40b91ce8d340f60717b73ab5d779e9f6a53be --- src/coreclr/tests/CoreFX/CoreFX.issues.json | 4 ++++ .../src/System/IO/StreamReader.cs | 16 ++++++++++++---- .../src/System/IO/StreamWriter.cs | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/coreclr/tests/CoreFX/CoreFX.issues.json b/src/coreclr/tests/CoreFX/CoreFX.issues.json index 3a3a17b86e13c..0849d7137c7c0 100644 --- a/src/coreclr/tests/CoreFX/CoreFX.issues.json +++ b/src/coreclr/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" } ] } diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs b/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs index 9d836c0b98b20..61accbf8996e2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/StreamReader.cs +++ b/src/libraries/System.Private.CoreLib/src/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/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs b/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs index 3a999195bc1d7..856dd27280de1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs +++ b/src/libraries/System.Private.CoreLib/src/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); }