Skip to content

Commit 0567556

Browse files
committed
Subclasses of Stream now have Close call base.Close to ensure disposal.
1 parent 8435ce9 commit 0567556

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

src/Microsoft.ML.Core/Utilities/HybridMemoryStream.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ public sealed class HybridMemoryStream : Stream
2525

2626
private bool _disposed;
2727

28-
private Stream MyStream { get { return _memStream ?? _overflowStream; } }
28+
private Stream MyStream => _memStream ?? _overflowStream;
2929

30-
private bool IsMemory { get { return _memStream != null; } }
30+
private bool IsMemory => _memStream != null;
3131

32-
public override long Position
33-
{
34-
get { return MyStream.Position; }
35-
set { Seek(value, SeekOrigin.Begin); }
32+
public override long Position {
33+
get => MyStream.Position;
34+
set => Seek(value, SeekOrigin.Begin);
3635
}
3736

38-
public override long Length { get { return MyStream.Length; } }
39-
public override bool CanWrite { get { return MyStream.CanWrite; } }
40-
public override bool CanSeek { get { return MyStream.CanSeek; } }
41-
public override bool CanRead { get { return MyStream.CanRead; } }
37+
public override long Length => MyStream.Length;
38+
public override bool CanWrite => MyStream.CanWrite;
39+
public override bool CanSeek => MyStream.CanSeek;
40+
public override bool CanRead => MyStream.CanRead;
4241

4342
/// <summary>
4443
/// Constructs an initially empty read-write stream. Once the number of
@@ -129,21 +128,22 @@ protected override void Dispose(bool disposing)
129128
}
130129
_disposed = true;
131130
AssertInvariants();
131+
base.Dispose(disposing);
132132
}
133133
}
134134

135135
public override void Close()
136136
{
137137
AssertInvariants();
138-
if (MyStream != null)
139-
MyStream.Close();
138+
MyStream?.Close();
139+
// The base Stream class Close will call Dispose(bool).
140+
base.Close();
140141
}
141142

142143
public override void Flush()
143144
{
144145
AssertInvariants();
145-
if (MyStream != null)
146-
MyStream.Flush();
146+
MyStream?.Flush();
147147
AssertInvariants();
148148
}
149149

src/Microsoft.ML.Core/Utilities/TextReaderStream.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
1414
/// compensates by inserting <c>\n</c> line feed characters at the end of every
1515
/// input line, including the last one.
1616
/// </summary>
17-
public class TextReaderStream : Stream
17+
public sealed class TextReaderStream : Stream
1818
{
1919
private readonly TextReader _baseReader;
2020
private readonly Encoding _encoding;
@@ -38,19 +38,11 @@ public class TextReaderStream : Stream
3838
public override bool CanWrite => false;
3939

4040
public override long Length
41-
{
42-
get
43-
{
44-
throw Contracts.ExceptNotSupp("Stream cannot determine length.");
45-
}
46-
}
41+
=> throw Contracts.ExceptNotSupp("Stream cannot determine length.");
4742

4843
public override long Position
4944
{
50-
get
51-
{
52-
return _position;
53-
}
45+
get => _position;
5446
set
5547
{
5648
if (value != Position)
@@ -96,6 +88,7 @@ public override void Close()
9688
protected override void Dispose(bool disposing)
9789
{
9890
_baseReader.Dispose();
91+
base.Dispose(disposing);
9992
}
10093

10194
public override void Flush()
@@ -182,18 +175,12 @@ public override int ReadByte()
182175
}
183176

184177
public override long Seek(long offset, SeekOrigin origin)
185-
{
186-
throw Contracts.ExceptNotSupp("Stream cannot seek.");
187-
}
178+
=> throw Contracts.ExceptNotSupp("Stream cannot seek.");
188179

189180
public override void Write(byte[] buffer, int offset, int count)
190-
{
191-
throw Contracts.ExceptNotSupp("Stream is not writable.");
192-
}
181+
=> throw Contracts.ExceptNotSupp("Stream is not writable.");
193182

194183
public override void SetLength(long value)
195-
{
196-
throw Contracts.ExceptNotSupp("Stream is not writable.");
197-
}
184+
=> throw Contracts.ExceptNotSupp("Stream is not writable.");
198185
}
199186
}

0 commit comments

Comments
 (0)