Skip to content

Commit 894fe57

Browse files
stefannikoleijtschuster
authored andcommitted
Mark Encoding as nullable in StreamWriter's constructor (dotnet#106658)
* Mark Encoding as nullable in StreamWriter's constructor * Update reference file * Align string constructors with Stream * add explicit test for null as encoding
1 parent 05327bf commit 894fe57

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public StreamWriter(Stream stream)
7777
{
7878
}
7979

80-
public StreamWriter(Stream stream, Encoding encoding)
80+
public StreamWriter(Stream stream, Encoding? encoding)
8181
: this(stream, encoding, DefaultBufferSize, false)
8282
{
8383
}
@@ -86,7 +86,7 @@ public StreamWriter(Stream stream, Encoding encoding)
8686
// character encoding is set by encoding and the buffer size,
8787
// in number of 16-bit characters, is set by bufferSize.
8888
//
89-
public StreamWriter(Stream stream, Encoding encoding, int bufferSize)
89+
public StreamWriter(Stream stream, Encoding? encoding, int bufferSize)
9090
: this(stream, encoding, bufferSize, false)
9191
{
9292
}
@@ -140,13 +140,13 @@ public StreamWriter(string path, bool append)
140140
{
141141
}
142142

143-
public StreamWriter(string path, bool append, Encoding encoding)
143+
public StreamWriter(string path, bool append, Encoding? encoding)
144144
: this(path, append, encoding, DefaultBufferSize)
145145
{
146146
}
147147

148-
public StreamWriter(string path, bool append, Encoding encoding, int bufferSize) :
149-
this(ValidateArgsAndOpenPath(path, append, encoding, bufferSize), encoding, bufferSize, leaveOpen: false)
148+
public StreamWriter(string path, bool append, Encoding? encoding, int bufferSize) :
149+
this(ValidateArgsAndOpenPath(path, append, bufferSize), encoding, bufferSize, leaveOpen: false)
150150
{
151151
}
152152

@@ -155,8 +155,8 @@ public StreamWriter(string path, FileStreamOptions options)
155155
{
156156
}
157157

158-
public StreamWriter(string path, Encoding encoding, FileStreamOptions options)
159-
: this(ValidateArgsAndOpenPath(path, encoding, options), encoding, DefaultFileStreamBufferSize)
158+
public StreamWriter(string path, Encoding? encoding, FileStreamOptions options)
159+
: this(ValidateArgsAndOpenPath(path, options), encoding, DefaultFileStreamBufferSize)
160160
{
161161
}
162162

@@ -169,10 +169,9 @@ private StreamWriter()
169169
_charBuffer = Array.Empty<char>();
170170
}
171171

172-
private static FileStream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options)
172+
private static FileStream ValidateArgsAndOpenPath(string path, FileStreamOptions options)
173173
{
174174
ArgumentException.ThrowIfNullOrEmpty(path);
175-
ArgumentNullException.ThrowIfNull(encoding);
176175
ArgumentNullException.ThrowIfNull(options);
177176
if ((options.Access & FileAccess.Write) == 0)
178177
{
@@ -182,10 +181,9 @@ private static FileStream ValidateArgsAndOpenPath(string path, Encoding encoding
182181
return new FileStream(path, options);
183182
}
184183

185-
private static FileStream ValidateArgsAndOpenPath(string path, bool append, Encoding encoding, int bufferSize)
184+
private static FileStream ValidateArgsAndOpenPath(string path, bool append, int bufferSize)
186185
{
187186
ArgumentException.ThrowIfNullOrEmpty(path);
188-
ArgumentNullException.ThrowIfNull(encoding);
189187
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize);
190188

191189
return new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read, DefaultFileStreamBufferSize);

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10750,15 +10750,15 @@ public partial class StreamWriter : System.IO.TextWriter
1075010750
{
1075110751
public static readonly new System.IO.StreamWriter Null;
1075210752
public StreamWriter(System.IO.Stream stream) { }
10753-
public StreamWriter(System.IO.Stream stream, System.Text.Encoding encoding) { }
10754-
public StreamWriter(System.IO.Stream stream, System.Text.Encoding encoding, int bufferSize) { }
10753+
public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding) { }
10754+
public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding, int bufferSize) { }
1075510755
public StreamWriter(System.IO.Stream stream, System.Text.Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false) { }
1075610756
public StreamWriter(string path) { }
1075710757
public StreamWriter(string path, bool append) { }
10758-
public StreamWriter(string path, bool append, System.Text.Encoding encoding) { }
10759-
public StreamWriter(string path, bool append, System.Text.Encoding encoding, int bufferSize) { }
10758+
public StreamWriter(string path, bool append, System.Text.Encoding? encoding) { }
10759+
public StreamWriter(string path, bool append, System.Text.Encoding? encoding, int bufferSize) { }
1076010760
public StreamWriter(string path, System.IO.FileStreamOptions options) { }
10761-
public StreamWriter(string path, System.Text.Encoding encoding, System.IO.FileStreamOptions options) { }
10761+
public StreamWriter(string path, System.Text.Encoding? encoding, System.IO.FileStreamOptions options) { }
1076210762
public virtual bool AutoFlush { get { throw null; } set { } }
1076310763
public virtual System.IO.Stream BaseStream { get { throw null; } }
1076410764
public override System.Text.Encoding Encoding { get { throw null; } }

src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.CtorTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ public static void UnicodeEncoding()
4848
TestEncoding(System.Text.Encoding.Unicode, "This is Unicode\u00FF");
4949
}
5050

51-
private static void TestEncoding(System.Text.Encoding encoding, string testString)
51+
[Fact]
52+
public static void NullEncoding()
53+
{
54+
TestEncoding(null, "This is UTF8\u00FF");
55+
}
56+
57+
private static void TestEncoding(System.Text.Encoding? encoding, string testString)
5258
{
5359
StreamWriter sw2;
5460
StreamReader sr2;

src/libraries/System.Runtime/tests/System.IO.Tests/StreamWriter/StreamWriter.StringCtorTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public static void NullArgs_ThrowsArgumentNullException()
1919
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true));
2020
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true, null));
2121
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true, null, -1));
22-
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", true, null));
23-
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", null, null));
24-
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", true, null, -1));
2522
}
2623

2724
[Fact]
@@ -31,6 +28,7 @@ public static void EmptyPath_ThrowsArgumentException()
3128
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter(""));
3229
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", new FileStreamOptions()));
3330
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true));
31+
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, null));
3432
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, Encoding.UTF8));
3533
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", Encoding.UTF8, new FileStreamOptions()));
3634
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, Encoding.UTF8, -1));

0 commit comments

Comments
 (0)