Skip to content

Commit

Permalink
Subclasses of Stream now have Close call base.Close to ensure d…
Browse files Browse the repository at this point in the history
…isposal.
  • Loading branch information
TomFinley committed Jun 18, 2018
1 parent 8435ce9 commit 0567556
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
28 changes: 14 additions & 14 deletions src/Microsoft.ML.Core/Utilities/HybridMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,19 @@ public sealed class HybridMemoryStream : Stream

private bool _disposed;

private Stream MyStream { get { return _memStream ?? _overflowStream; } }
private Stream MyStream => _memStream ?? _overflowStream;

private bool IsMemory { get { return _memStream != null; } }
private bool IsMemory => _memStream != null;

public override long Position
{
get { return MyStream.Position; }
set { Seek(value, SeekOrigin.Begin); }
public override long Position {
get => MyStream.Position;
set => Seek(value, SeekOrigin.Begin);
}

public override long Length { get { return MyStream.Length; } }
public override bool CanWrite { get { return MyStream.CanWrite; } }
public override bool CanSeek { get { return MyStream.CanSeek; } }
public override bool CanRead { get { return MyStream.CanRead; } }
public override long Length => MyStream.Length;
public override bool CanWrite => MyStream.CanWrite;
public override bool CanSeek => MyStream.CanSeek;
public override bool CanRead => MyStream.CanRead;

/// <summary>
/// Constructs an initially empty read-write stream. Once the number of
Expand Down Expand Up @@ -129,21 +128,22 @@ protected override void Dispose(bool disposing)
}
_disposed = true;
AssertInvariants();
base.Dispose(disposing);
}
}

public override void Close()
{
AssertInvariants();
if (MyStream != null)
MyStream.Close();
MyStream?.Close();
// The base Stream class Close will call Dispose(bool).
base.Close();
}

public override void Flush()
{
AssertInvariants();
if (MyStream != null)
MyStream.Flush();
MyStream?.Flush();
AssertInvariants();
}

Expand Down
27 changes: 7 additions & 20 deletions src/Microsoft.ML.Core/Utilities/TextReaderStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.ML.Runtime.Internal.Utilities
/// compensates by inserting <c>\n</c> line feed characters at the end of every
/// input line, including the last one.
/// </summary>
public class TextReaderStream : Stream
public sealed class TextReaderStream : Stream
{
private readonly TextReader _baseReader;
private readonly Encoding _encoding;
Expand All @@ -38,19 +38,11 @@ public class TextReaderStream : Stream
public override bool CanWrite => false;

public override long Length
{
get
{
throw Contracts.ExceptNotSupp("Stream cannot determine length.");
}
}
=> throw Contracts.ExceptNotSupp("Stream cannot determine length.");

public override long Position
{
get
{
return _position;
}
get => _position;
set
{
if (value != Position)
Expand Down Expand Up @@ -96,6 +88,7 @@ public override void Close()
protected override void Dispose(bool disposing)
{
_baseReader.Dispose();
base.Dispose(disposing);
}

public override void Flush()
Expand Down Expand Up @@ -182,18 +175,12 @@ public override int ReadByte()
}

public override long Seek(long offset, SeekOrigin origin)
{
throw Contracts.ExceptNotSupp("Stream cannot seek.");
}
=> throw Contracts.ExceptNotSupp("Stream cannot seek.");

public override void Write(byte[] buffer, int offset, int count)
{
throw Contracts.ExceptNotSupp("Stream is not writable.");
}
=> throw Contracts.ExceptNotSupp("Stream is not writable.");

public override void SetLength(long value)
{
throw Contracts.ExceptNotSupp("Stream is not writable.");
}
=> throw Contracts.ExceptNotSupp("Stream is not writable.");
}
}

0 comments on commit 0567556

Please sign in to comment.