Skip to content

Commit

Permalink
Mark Encoding as nullable in StreamWriter's constructor (dotnet#106658)
Browse files Browse the repository at this point in the history
* Mark Encoding as nullable in StreamWriter's constructor

* Update reference file

* Align string constructors with Stream

* add explicit test for null as encoding
  • Loading branch information
stefannikolei authored Sep 16, 2024
1 parent 1bde882 commit eac892a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
20 changes: 9 additions & 11 deletions src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand All @@ -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)
{
}
Expand Down Expand Up @@ -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)
{
}

Expand All @@ -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)
{
}

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

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)
{
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public static void NullArgs_ThrowsArgumentNullException()
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true));
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true, null));
AssertExtensions.Throws<ArgumentNullException>("path", () => new StreamWriter((string)null, true, null, -1));
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", true, null));
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", null, null));
AssertExtensions.Throws<ArgumentNullException>("encoding", () => new StreamWriter("path", true, null, -1));
}

[Fact]
Expand All @@ -31,6 +28,7 @@ public static void EmptyPath_ThrowsArgumentException()
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter(""));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", new FileStreamOptions()));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, null));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, Encoding.UTF8));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", Encoding.UTF8, new FileStreamOptions()));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, Encoding.UTF8, -1));
Expand Down

0 comments on commit eac892a

Please sign in to comment.