Lean  $LEAN_TAG$
IStreamProvider.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;
17 using System.Collections.Generic;
18 using System.IO;
19 
20 namespace QuantConnect.ToolBox
21 {
22  /// <summary>
23  /// Defines how to open/close a source file
24  /// </summary>
25  public interface IStreamProvider : IDisposable
26  {
27  /// <summary>
28  /// Opens the specified source as read to be consumed stream
29  /// </summary>
30  /// <param name="source">The source file to be opened</param>
31  /// <returns>The stream representing the specified source</returns>
32  IEnumerable<Stream> Open(string source);
33 
34  /// <summary>
35  /// Closes the specified source file stream
36  /// </summary>
37  /// <param name="source">The source file to be closed</param>
38  void Close(string source);
39  }
40 
41  /// <summary>
42  /// Provides factor method for creating an <see cref="IStreamProvider"/> from a file name
43  /// </summary>
44  public static class StreamProvider
45  {
46  /// <summary>
47  /// Creates a new <see cref="IStreamProvider"/> capable of reading a file with the specified extenson
48  /// </summary>
49  /// <param name="extension">The file extension</param>
50  /// <returns>A new stream provider capable of reading files with the specified extension</returns>
51  public static IStreamProvider ForExtension(string extension)
52  {
53  var ext = Path.GetExtension(extension);
54  if (ext == ".zip")
55  {
56  return new ZipStreamProvider();
57  }
58  if (ext == ".bz2")
59  {
60  return new Bz2StreamProvider();
61  }
62  if (ext == ".gz")
63  {
64  return new GzipStreamProvider();
65  }
66 
67  return new FileStreamProvider();
68  }
69  }
70 }