Lean  $LEAN_TAG$
FileStreamProvider.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.Collections.Generic;
17 using System.IO;
18 
19 namespace QuantConnect.ToolBox
20 {
21  /// <summary>
22  /// Provides an implementation of <see cref="IStreamProvider"/> that just returns a file stream
23  /// </summary>
25  {
26  private readonly Dictionary<string, FileStream> _files = new Dictionary<string, FileStream>();
27 
28  /// <summary>
29  /// Opens the specified source as read to be consumed stream
30  /// </summary>
31  /// <param name="source">The source file to be opened</param>
32  /// <returns>The stream representing the specified source</returns>
33  public IEnumerable<Stream> Open(string source)
34  {
35  yield return File.OpenRead(source);
36  }
37 
38  /// <summary>
39  /// Closes the specified source file stream
40  /// </summary>
41  /// <param name="source">The source file to be closed</param>
42  public void Close(string source)
43  {
44  // it's expected that users will dispose the stream
45  // from the open call, this is used to clean up any
46  // other resources, for example a ZipFile stream
47  // when we returned a ZipEntry stream
48  _files.Remove(source);
49  }
50 
51  /// <summary>
52  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
53  /// </summary>
54  public void Dispose()
55  {
56  foreach (var kvp in _files)
57  {
58  kvp.Value.Dispose();
59  }
60  }
61  }
62 }