@@ -19,26 +19,24 @@ public sealed class HybridMemoryStream : Stream
19
19
{
20
20
private MemoryStream _memStream ;
21
21
private Stream _overflowStream ;
22
- private string _overflowPath ;
23
22
private readonly int _overflowBoundary ;
24
23
private const int _defaultMaxLen = 1 << 30 ;
25
24
26
25
private bool _disposed ;
27
26
28
- private Stream MyStream { get { return _memStream ?? _overflowStream ; } }
27
+ private Stream MyStream => _memStream ?? _overflowStream ;
29
28
30
- private bool IsMemory { get { return _memStream != null ; } }
29
+ private bool IsMemory => _memStream != null ;
31
30
32
- public override long Position
33
- {
34
- get { return MyStream . Position ; }
35
- set { Seek ( value , SeekOrigin . Begin ) ; }
31
+ public override long Position {
32
+ get => MyStream . Position ;
33
+ set => Seek ( value , SeekOrigin . Begin ) ;
36
34
}
37
35
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 ; } }
36
+ public override long Length => MyStream . Length ;
37
+ public override bool CanWrite => MyStream . CanWrite ;
38
+ public override bool CanSeek => MyStream . CanSeek ;
39
+ public override bool CanRead => MyStream . CanRead ;
42
40
43
41
/// <summary>
44
42
/// Constructs an initially empty read-write stream. Once the number of
@@ -123,27 +121,24 @@ protected override void Dispose(bool disposing)
123
121
var overflow = _overflowStream ;
124
122
_overflowStream = null ;
125
123
overflow . Dispose ( ) ;
126
- Contracts . AssertValue ( _overflowPath ) ;
127
- File . Delete ( _overflowPath ) ;
128
- _overflowPath = null ;
129
124
}
130
125
_disposed = true ;
131
126
AssertInvariants ( ) ;
127
+ base . Dispose ( disposing ) ;
132
128
}
133
129
}
134
130
135
131
public override void Close ( )
136
132
{
137
133
AssertInvariants ( ) ;
138
- if ( MyStream != null )
139
- MyStream . Close ( ) ;
134
+ // The base Stream class Close will call Dispose(bool).
135
+ base . Close ( ) ;
140
136
}
141
137
142
138
public override void Flush ( )
143
139
{
144
140
AssertInvariants ( ) ;
145
- if ( MyStream != null )
146
- MyStream . Flush ( ) ;
141
+ MyStream ? . Flush ( ) ;
147
142
AssertInvariants ( ) ;
148
143
}
149
144
@@ -164,9 +159,9 @@ private void EnsureOverflow()
164
159
// been closed.
165
160
Contracts . Check ( _memStream . CanRead , "attempt to perform operation on closed stream" ) ;
166
161
167
- Contracts . Assert ( _overflowPath == null ) ;
168
- _overflowPath = Path . GetTempFileName ( ) ;
169
- _overflowStream = new FileStream ( _overflowPath , FileMode . Open , FileAccess . ReadWrite ) ;
162
+ string overflowPath = Path . GetTempFileName ( ) ;
163
+ _overflowStream = new FileStream ( overflowPath , FileMode . Open , FileAccess . ReadWrite ,
164
+ FileShare . None , bufferSize : 4096 , FileOptions . DeleteOnClose ) ;
170
165
171
166
// The documentation is not clear on this point, but the source code for
172
167
// memory stream makes clear that this buffer is exposable for a memory
0 commit comments