Skip to content

Commit 49b3007

Browse files
committed
Replace BaseStream with auto-properties
***NO_CI***
1 parent fa230e1 commit 49b3007

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

System.IO.Streams/StreamReader.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public class StreamReader : TextReader
1717
private const int c_MaxReadLineLen = 0xFFFF;
1818
private const int c_BufferSize = 512;
1919

20-
private Stream _stream;
21-
2220
// Initialized in constructor by CurrentEncoding scheme.
2321
// Encoding can be changed by resetting this variable.
2422
readonly Decoder _decoder;
@@ -42,7 +40,7 @@ public class StreamReader : TextReader
4240
/// <remarks>
4341
/// You use this property to access the underlying stream. The StreamReader class buffers input from the underlying stream when you call one of the Read methods. If you manipulate the position of the underlying stream after reading data into the buffer, the position of the underlying stream might not match the position of the internal buffer. To reset the internal buffer, call the DiscardBufferedData method; however, this method slows performance and should be called only when absolutely necessary. The StreamReader constructors that have the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader object.
4442
/// </remarks>
45-
public virtual Stream BaseStream => _stream;
43+
public virtual Stream BaseStream { get; private set; }
4644

4745
/// <summary>
4846
/// Gets the current character encoding that the current <see cref="StreamReader"/> object is using.
@@ -84,7 +82,7 @@ public StreamReader(Stream stream)
8482
_buffer = new byte[c_BufferSize];
8583
_curBufPos = 0;
8684
_curBufLen = 0;
87-
_stream = stream;
85+
BaseStream = stream;
8886
_decoder = CurrentEncoding.GetDecoder();
8987
_disposed = false;
9088
}
@@ -112,14 +110,14 @@ public override void Close()
112110
/// </remarks>
113111
protected override void Dispose(bool disposing)
114112
{
115-
if (_stream != null)
113+
if (BaseStream != null)
116114
{
117115
if (disposing)
118116
{
119-
_stream.Close();
117+
BaseStream.Close();
120118
}
121119

122-
_stream = null;
120+
BaseStream = null;
123121
_buffer = null;
124122
_curBufPos = 0;
125123
_curBufLen = 0;
@@ -160,16 +158,16 @@ public override int Peek()
160158
try
161159
{
162160
// retry read until response timeout expires
163-
while (_stream.Length > 0 && totRead < _buffer.Length)
161+
while (BaseStream.Length > 0 && totRead < _buffer.Length)
164162
{
165163
int len = (int)(_buffer.Length - totRead);
166164

167-
if (len > _stream.Length)
165+
if (len > BaseStream.Length)
168166
{
169-
len = (int)_stream.Length;
167+
len = (int)BaseStream.Length;
170168
}
171169

172-
len = _stream.Read(_buffer, totRead, len);
170+
len = BaseStream.Read(_buffer, totRead, len);
173171

174172
if (len <= 0)
175173
{
@@ -238,7 +236,7 @@ public override int Read()
238236
int readCount = _buffer.Length;
239237

240238
// Put it to the maximum of available data and readCount
241-
readCount = readCount > (int)_stream.Length ? (int)_stream.Length : readCount;
239+
readCount = readCount > (int)BaseStream.Length ? (int)BaseStream.Length : readCount;
242240

243241
if (readCount == 0)
244242
{
@@ -421,7 +419,7 @@ public override string ReadToEnd()
421419
{
422420
char[] result = null;
423421

424-
if (_stream.CanSeek)
422+
if (BaseStream.CanSeek)
425423
{
426424
result = ReadSeekableStream();
427425
}
@@ -435,7 +433,7 @@ public override string ReadToEnd()
435433

436434
private char[] ReadSeekableStream()
437435
{
438-
char[] chars = new char[(int)_stream.Length];
436+
char[] chars = new char[(int)BaseStream.Length];
439437

440438
_ = Read(chars, 0, chars.Length);
441439

@@ -518,7 +516,7 @@ private int FillBufferAndReset(int count)
518516

519517
if (count > spaceLeft) count = spaceLeft;
520518

521-
int read = _stream.Read(_buffer, _curBufLen, count);
519+
int read = BaseStream.Read(_buffer, _curBufLen, count);
522520

523521
if (read == 0) break;
524522

System.IO.Streams/StreamWriter.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class StreamWriter : TextWriter
1616
private const string c_NewLine = "\r\n";
1717
private const int c_BufferSize = 0xFFF;
1818

19-
private Stream _stream;
2019
private bool _disposed;
2120
private byte[] _buffer;
2221
private int _curBufPos;
@@ -25,7 +24,7 @@ public class StreamWriter : TextWriter
2524
/// Gets the underlying stream that interfaces with a backing store.
2625
/// </summary>
2726
/// <value>The stream this <see cref="StreamWriter"/> is writing to.</value>
28-
public virtual Stream BaseStream => _stream;
27+
public virtual Stream BaseStream { get; private set; }
2928

3029
/// <summary>
3130
/// Gets the <see cref="Encoding"/> in which the output is written.
@@ -51,7 +50,7 @@ public StreamWriter(Stream stream)
5150
throw new ArgumentException();
5251
}
5352

54-
_stream = stream;
53+
BaseStream = stream;
5554
_buffer = new byte[c_BufferSize];
5655
_curBufPos = 0;
5756
_disposed = false;
@@ -80,13 +79,13 @@ public override void Close()
8079
/// </remarks>
8180
protected override void Dispose(bool disposing)
8281
{
83-
if (_stream != null)
82+
if (BaseStream != null)
8483
{
8584
if (disposing)
8685
{
8786
try
8887
{
89-
if (_stream.CanWrite)
88+
if (BaseStream.CanWrite)
9089
{
9190
Flush();
9291
}
@@ -95,12 +94,12 @@ protected override void Dispose(bool disposing)
9594

9695
try
9796
{
98-
_stream.Close();
97+
BaseStream.Close();
9998
}
10099
catch { }
101100
}
102101

103-
_stream = null;
102+
BaseStream = null;
104103
_buffer = null;
105104
_curBufPos = 0;
106105
}
@@ -128,7 +127,7 @@ public override void Flush()
128127
{
129128
try
130129
{
131-
_stream.Write(_buffer, 0, _curBufPos);
130+
BaseStream.Write(_buffer, 0, _curBufPos);
132131
}
133132
catch (Exception e)
134133
{
@@ -197,10 +196,10 @@ internal void WriteBytes(
197196
// directly to stream.
198197
try
199198
{
200-
_stream.Write(_buffer, 0, _curBufPos);
199+
BaseStream.Write(_buffer, 0, _curBufPos);
201200
_curBufPos = 0;
202201

203-
_stream.Write(buffer, index, count);
202+
BaseStream.Write(buffer, index, count);
204203

205204
return;
206205
}

0 commit comments

Comments
 (0)