Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark Encoding as nullable in StreamWriter's constructor #106658

Merged
merged 6 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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));
stefannikolei marked this conversation as resolved.
Show resolved Hide resolved
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", Encoding.UTF8, new FileStreamOptions()));
AssertExtensions.Throws<ArgumentException>("path", () => new StreamWriter("", true, Encoding.UTF8, -1));
Expand Down
Loading