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 58efa18407c70..dd0263133846f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs @@ -77,7 +77,7 @@ public StreamWriter(Stream stream) { } - public StreamWriter(Stream stream, Encoding encoding) + public StreamWriter(Stream stream, Encoding? encoding) : this(stream, encoding, DefaultBufferSize, false) { } @@ -86,7 +86,7 @@ public StreamWriter(Stream stream, Encoding encoding) // character encoding is set by encoding and the buffer size, // in number of 16-bit characters, is set by bufferSize. // - public StreamWriter(Stream stream, Encoding encoding, int bufferSize) + public StreamWriter(Stream stream, Encoding? encoding, int bufferSize) : this(stream, encoding, bufferSize, false) { } @@ -140,13 +140,13 @@ public StreamWriter(string path, bool append) { } - public StreamWriter(string path, bool append, Encoding encoding) + public StreamWriter(string path, bool append, Encoding? encoding) : this(path, append, encoding, DefaultBufferSize) { } - public StreamWriter(string path, bool append, Encoding encoding, int bufferSize) : - this(ValidateArgsAndOpenPath(path, append, encoding, bufferSize), encoding, bufferSize, leaveOpen: false) + public StreamWriter(string path, bool append, Encoding? encoding, int bufferSize) : + this(ValidateArgsAndOpenPath(path, append, bufferSize), encoding, bufferSize, leaveOpen: false) { } @@ -155,8 +155,8 @@ public StreamWriter(string path, FileStreamOptions options) { } - public StreamWriter(string path, Encoding encoding, FileStreamOptions options) - : this(ValidateArgsAndOpenPath(path, encoding, options), encoding, DefaultFileStreamBufferSize) + public StreamWriter(string path, Encoding? encoding, FileStreamOptions options) + : this(ValidateArgsAndOpenPath(path, options), encoding, DefaultFileStreamBufferSize) { } @@ -169,10 +169,9 @@ private StreamWriter() _charBuffer = Array.Empty(); } - private static FileStream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options) + private static FileStream ValidateArgsAndOpenPath(string path, FileStreamOptions options) { ArgumentException.ThrowIfNullOrEmpty(path); - ArgumentNullException.ThrowIfNull(encoding); ArgumentNullException.ThrowIfNull(options); if ((options.Access & FileAccess.Write) == 0) { @@ -182,10 +181,9 @@ private static FileStream ValidateArgsAndOpenPath(string path, Encoding encoding return new FileStream(path, options); } - private static FileStream ValidateArgsAndOpenPath(string path, bool append, Encoding encoding, int bufferSize) + private static FileStream ValidateArgsAndOpenPath(string path, bool append, int bufferSize) { ArgumentException.ThrowIfNullOrEmpty(path); - ArgumentNullException.ThrowIfNull(encoding); ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize); return new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, DefaultFileStreamBufferSize); diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index c043fb81ab51e..1d7f9cd468111 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -10750,15 +10750,15 @@ public partial class StreamWriter : System.IO.TextWriter { public static readonly new System.IO.StreamWriter Null; public StreamWriter(System.IO.Stream stream) { } - public StreamWriter(System.IO.Stream stream, System.Text.Encoding encoding) { } - public StreamWriter(System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize) { } + public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding) { } + public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding, int bufferSize) { } public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false) { } public StreamWriter(string path) { } public StreamWriter(string path, bool append) { } - public StreamWriter(string path, bool append, System.Text.Encoding encoding) { } - public StreamWriter(string path, bool append, System.Text.Encoding encoding, int bufferSize) { } + public StreamWriter(string path, bool append, System.Text.Encoding? encoding) { } + public StreamWriter(string path, bool append, System.Text.Encoding? encoding, int bufferSize) { } public StreamWriter(string path, System.IO.FileStreamOptions options) { } - public StreamWriter(string path, System.Text.Encoding encoding, System.IO.FileStreamOptions options) { } + public StreamWriter(string path, System.Text.Encoding? encoding, System.IO.FileStreamOptions options) { } public virtual bool AutoFlush { get { throw null; } set { } } public virtual System.IO.Stream BaseStream { get { throw null; } } public override System.Text.Encoding Encoding { get { throw null; } } diff --git a/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.CtorTests.cs b/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.CtorTests.cs index bf768cf9ff725..bf0075139e699 100644 --- a/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.CtorTests.cs +++ b/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.CtorTests.cs @@ -48,7 +48,13 @@ public static void UnicodeEncoding() TestEncoding(System.Text.Encoding.Unicode, "This is Unicode\u00FF"); } - private static void TestEncoding(System.Text.Encoding encoding, string testString) + [Fact] + public static void NullEncoding() + { + TestEncoding(null, "This is UTF8\u00FF"); + } + + private static void TestEncoding(System.Text.Encoding? encoding, string testString) { StreamWriter sw2; StreamReader sr2; diff --git a/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.StringCtorTests.cs b/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.StringCtorTests.cs index 6900fb973843b..ec94e8cb60637 100644 --- a/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.StringCtorTests.cs +++ b/src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.StringCtorTests.cs @@ -19,9 +19,6 @@ public static void NullArgs_ThrowsArgumentNullException() AssertExtensions.Throws("path", () => new StreamWriter((string)null, true)); AssertExtensions.Throws("path", () => new StreamWriter((string)null, true, null)); AssertExtensions.Throws("path", () => new StreamWriter((string)null, true, null, -1)); - AssertExtensions.Throws("encoding", () => new StreamWriter("path", true, null)); - AssertExtensions.Throws("encoding", () => new StreamWriter("path", null, null)); - AssertExtensions.Throws("encoding", () => new StreamWriter("path", true, null, -1)); } [Fact] @@ -31,6 +28,7 @@ public static void EmptyPath_ThrowsArgumentException() AssertExtensions.Throws("path", () => new StreamWriter("")); AssertExtensions.Throws("path", () => new StreamWriter("", new FileStreamOptions())); AssertExtensions.Throws("path", () => new StreamWriter("", true)); + AssertExtensions.Throws("path", () => new StreamWriter("", true, null)); AssertExtensions.Throws("path", () => new StreamWriter("", true, Encoding.UTF8)); AssertExtensions.Throws("path", () => new StreamWriter("", Encoding.UTF8, new FileStreamOptions())); AssertExtensions.Throws("path", () => new StreamWriter("", true, Encoding.UTF8, -1));