2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Diagnostics ;
5
6
using System . IO ;
6
7
using System . Threading ;
7
8
using System . Threading . Tasks ;
@@ -18,14 +19,26 @@ public class FileBufferingReadStream : Stream
18
19
{
19
20
private readonly Stream _inner ;
20
21
private readonly int _memoryThreshold ;
21
- private readonly string _tempFileDirectory ;
22
+ private string _tempFileDirectory ;
23
+ private readonly Func < string > _tempFileDirectoryAccessor ;
22
24
23
25
private Stream _buffer = new MemoryStream ( ) ; // TODO: We could have a more efficiently expanding buffer stream.
24
26
private bool _inMemory = true ;
25
27
private bool _completelyBuffered ;
26
28
27
29
private bool _disposed ;
28
30
31
+ // TODO: allow for an optional buffer size limit to prevent filling hard disks. 1gb?
32
+ public FileBufferingReadStream (
33
+ [ NotNull ] Stream inner ,
34
+ int memoryThreshold ,
35
+ [ NotNull ] Func < string > tempFileDirectoryAccessor )
36
+ {
37
+ _inner = inner ;
38
+ _memoryThreshold = memoryThreshold ;
39
+ _tempFileDirectoryAccessor = tempFileDirectoryAccessor ;
40
+ }
41
+
29
42
// TODO: allow for an optional buffer size limit to prevent filling hard disks. 1gb?
30
43
public FileBufferingReadStream ( [ NotNull ] Stream inner , int memoryThreshold , [ NotNull ] string tempFileDirectory )
31
44
{
@@ -88,6 +101,13 @@ public override long Seek(long offset, SeekOrigin origin)
88
101
89
102
private Stream CreateTempFile ( )
90
103
{
104
+ if ( _tempFileDirectory == null )
105
+ {
106
+ Debug . Assert ( _tempFileDirectoryAccessor != null ) ;
107
+ _tempFileDirectory = _tempFileDirectoryAccessor ( ) ;
108
+ Debug . Assert ( _tempFileDirectory != null ) ;
109
+ }
110
+
91
111
var fileName = Path . Combine ( _tempFileDirectory , "ASPNET_" + Guid . NewGuid ( ) . ToString ( ) + ".tmp" ) ;
92
112
return new FileStream ( fileName , FileMode . Create , FileAccess . ReadWrite , FileShare . Delete , 1024 * 16 ,
93
113
FileOptions . Asynchronous | FileOptions . DeleteOnClose | FileOptions . SequentialScan ) ;
0 commit comments