Lean  $LEAN_TAG$
LocalFileSubscriptionStreamReader.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 
17 using System.IO;
18 using Ionic.Zip;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System;
23 
25 {
26  /// <summary>
27  /// Represents a stream reader capable of reading lines from disk
28  /// </summary>
30  {
31  private readonly ZipFile _zipFile;
32 
33  /// <summary>
34  /// Gets whether or not this stream reader should be rate limited
35  /// </summary>
36  public bool ShouldBeRateLimited => false;
37 
38  /// <summary>
39  /// Direct access to the StreamReader instance
40  /// </summary>
41  public StreamReader StreamReader { get; private set; }
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="LocalFileSubscriptionStreamReader"/> class.
45  /// </summary>
46  /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param>
47  /// <param name="source">The local file to be read</param>
48  /// <param name="entryName">Specifies the zip entry to be opened. Leave null if not applicable,
49  /// or to open the first zip entry found regardless of name</param>
50  public LocalFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string entryName = null)
51  {
52  var stream = dataCacheProvider.Fetch(source);
53 
54  if (stream != null)
55  {
56  StreamReader = new StreamReader(stream);
57  }
58  }
59 
60  /// <summary>
61  /// Initializes a new instance of the <see cref="LocalFileSubscriptionStreamReader"/> class.
62  /// </summary>
63  /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param>
64  /// <param name="source">The local file to be read</param>
65  /// <param name="startingPosition">The position in the stream from which to start reading</param>
66  public LocalFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, long startingPosition)
67  {
68  var stream = dataCacheProvider.Fetch(source);
69 
70  if (stream != null)
71  {
72  StreamReader = new StreamReader(stream);
73 
74  if (startingPosition != 0)
75  {
76  StreamReader.BaseStream.Seek(startingPosition, SeekOrigin.Begin);
77  }
78  }
79  }
80 
81  /// <summary>
82  /// Initializes a new instance of the <see cref="LocalFileSubscriptionStreamReader"/> class.
83  /// </summary>
84  /// <param name="zipFile">The local zip archive to be read</param>
85  /// <param name="entryName">Specifies the zip entry to be opened. Leave null if not applicable,
86  /// or to open the first zip entry found regardless of name</param>
87  public LocalFileSubscriptionStreamReader(ZipFile zipFile, string entryName = null)
88  {
89  _zipFile = zipFile;
90  var entry = _zipFile.Entries.FirstOrDefault(x => entryName == null || string.Compare(x.FileName, entryName, StringComparison.OrdinalIgnoreCase) == 0);
91  if (entry != null)
92  {
93  var stream = new MemoryStream();
94  entry.OpenReader().CopyTo(stream);
95  stream.Position = 0;
96  StreamReader = new StreamReader(stream);
97  }
98  }
99 
100  /// <summary>
101  /// Returns the list of zip entries if local file stream reader is reading zip archive
102  /// </summary>
103  public IEnumerable<string> EntryFileNames
104  {
105  get
106  {
107  return _zipFile != null ? _zipFile.Entries.Select(x => x.FileName).ToList() : Enumerable.Empty<string>();
108  }
109  }
110 
111  /// <summary>
112  /// Gets <see cref="SubscriptionTransportMedium.LocalFile"/>
113  /// </summary>
115  {
116  get { return SubscriptionTransportMedium.LocalFile; }
117  }
118 
119  /// <summary>
120  /// Gets whether or not there's more data to be read in the stream
121  /// </summary>
122  public bool EndOfStream
123  {
124  get { return StreamReader == null || StreamReader.EndOfStream; }
125  }
126 
127  /// <summary>
128  /// Gets the next line/batch of content from the stream
129  /// </summary>
130  public string ReadLine()
131  {
132  return StreamReader.ReadLine();
133  }
134 
135  /// <summary>
136  /// Disposes of the stream
137  /// </summary>
138  public void Dispose()
139  {
140  if (StreamReader != null)
141  {
142  StreamReader.Dispose();
143  StreamReader = null;
144  }
145  }
146  }
147 }