Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Added optional/default parameters for StreamWriter/StreamReader #24056

Merged
merged 6 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 12 additions & 4 deletions src/System.Private.CoreLib/shared/System/IO/StreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,25 @@ public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByt
{
}

public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
public StreamReader(Stream stream, Encoding? encoding = null, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false)
{
if (stream == null || encoding == null)
if (stream == null)
{
throw new ArgumentNullException(stream == null ? nameof(stream) : nameof(encoding));
throw new ArgumentNullException(nameof(stream));
}
if (encoding == null)
{
encoding = Encoding.UTF8;
}
if (!stream.CanRead)
{
throw new ArgumentException(SR.Argument_StreamNotReadable);
}
if (bufferSize <= 0)
if (bufferSize == -1)
{
bufferSize = DefaultBufferSize;
}
else if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
}
Expand Down
16 changes: 12 additions & 4 deletions src/System.Private.CoreLib/shared/System/IO/StreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,26 @@ public StreamWriter(Stream stream, Encoding encoding, int bufferSize)
{
}

public StreamWriter(Stream stream, Encoding encoding, int bufferSize, bool leaveOpen)
public StreamWriter(Stream stream, Encoding? encoding = null, int bufferSize = -1, bool leaveOpen = false)
: base(null) // Ask for CurrentCulture all the time
{
if (stream == null || encoding == null)
if (stream == null)
{
throw new ArgumentNullException(stream == null ? nameof(stream) : nameof(encoding));
throw new ArgumentNullException(nameof(stream));
}
if (encoding == null)
{
encoding = UTF8NoBOM;
}
if (!stream.CanWrite)
{
throw new ArgumentException(SR.Argument_StreamNotWritable);
}
if (bufferSize <= 0)
if (bufferSize == -1)
{
bufferSize = DefaultBufferSize;
}
else if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException(nameof(bufferSize), SR.ArgumentOutOfRange_NeedPosNum);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/CoreFX/CoreFX.issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,10 @@
{
"name": "System.IO.Tests.StreamWriterWithBufferedStream_CloseTests.AfterCloseThrows",
"reason": "Readers/writers changed to not null out base stream on dispose"
},
{
"name": "System.IO.Tests.CtorTests.NullEncodingThrows",
"reason": "StreamReader/StreamWriter changed to accept null encoding"
}
]
}
Expand Down