From 05b383ac15766775419e22b6ff641a2e0a1c2a81 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Mon, 19 Aug 2024 21:38:26 +0200 Subject: [PATCH 1/4] Mark Encoding as nullable in StreamWriter's constructor --- .../System.Private.CoreLib/src/System/IO/StreamWriter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..d9725c200a41f 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) { } From a3f117634c13253c10787887a5b67ea49f6dded9 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Wed, 21 Aug 2024 20:32:58 +0200 Subject: [PATCH 2/4] Update reference file --- src/libraries/System.Runtime/ref/System.Runtime.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index b5ecc14826bfa..8274bdfe71627 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -10749,8 +10749,8 @@ 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) { } From af15dbdaca601ee776a1857e76d14467c2f120c7 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Wed, 21 Aug 2024 20:38:45 +0200 Subject: [PATCH 3/4] Align string constructors with Stream --- .../src/System/IO/StreamWriter.cs | 16 +++++++--------- .../System.Runtime/ref/System.Runtime.cs | 6 +++--- .../StreamWriter/StreamWriter.StringCtorTests.cs | 4 +--- 3 files changed, 11 insertions(+), 15 deletions(-) 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 d9725c200a41f..dd0263133846f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs @@ -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 8274bdfe71627..b849f9c625cd8 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -10754,10 +10754,10 @@ public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding, int 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.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)); From 4591bff76a5f7f07dfd92d1215a55435413b8f39 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Tue, 3 Sep 2024 20:45:13 +0200 Subject: [PATCH 4/4] add explicit test for null as encoding --- .../StreamWriter/StreamWriter.CtorTests.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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;