|
2 | 2 |
|
3 | 3 | internal class SharedStringsDiskCache : IDictionary<int, string>, IDisposable |
4 | 4 | { |
| 5 | + private const int ExcelCellMaxLength = 32767; |
5 | 6 | private static readonly Encoding Encoding = new UTF8Encoding(true); |
6 | 7 |
|
7 | 8 | private readonly FileStream _positionFs; |
8 | 9 | private readonly FileStream _lengthFs; |
9 | 10 | private readonly FileStream _valueFs; |
10 | 11 |
|
11 | | - private long _maxIndx = -1; |
12 | | - |
| 12 | + private long _maxIndex = -1; |
13 | 13 | private bool _disposedValue; |
14 | 14 |
|
15 | | - public int Count => checked((int)(_maxIndx + 1)); |
| 15 | + public int Count => checked((int)(_maxIndex + 1)); |
16 | 16 |
|
17 | 17 | public string this[int key] |
18 | 18 | { |
19 | 19 | get => GetValue(key); |
20 | 20 | set => Add(key, value); |
21 | 21 | } |
22 | 22 |
|
23 | | - public bool ContainsKey(int key) => key <= _maxIndx; |
| 23 | + public bool ContainsKey(int key) => key <= _maxIndex; |
24 | 24 |
|
25 | | - public SharedStringsDiskCache() |
| 25 | + public SharedStringsDiskCache(string sharedStringsCacheDir) |
26 | 26 | { |
27 | | - var path = $"{Guid.NewGuid().ToString()}_miniexcelcache"; |
28 | | - |
29 | | - _positionFs = new FileStream($"{path}_position", FileMode.OpenOrCreate); |
30 | | - _lengthFs = new FileStream($"{path}_length", FileMode.OpenOrCreate); |
31 | | - _valueFs = new FileStream($"{path}_data", FileMode.OpenOrCreate); |
| 27 | + if (string.IsNullOrWhiteSpace(sharedStringsCacheDir) || !Directory.Exists(sharedStringsCacheDir)) |
| 28 | + throw new DirectoryNotFoundException($"\"{sharedStringsCacheDir}\" is not a valid directory for the shared strings cache."); |
| 29 | + |
| 30 | + var prefix = $"{Path.GetRandomFileName()}_miniexcel"; |
| 31 | + _positionFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_position"), FileMode.OpenOrCreate); |
| 32 | + _lengthFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_length"), FileMode.OpenOrCreate); |
| 33 | + _valueFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_data"), FileMode.OpenOrCreate); |
32 | 34 | } |
33 | 35 |
|
34 | 36 | // index must start with 0-N |
35 | | - internal void Add(int index, string value) |
| 37 | + private void Add(int index, string value) |
36 | 38 | { |
37 | | - if (index > _maxIndx) |
38 | | - _maxIndx = index; |
| 39 | + if (index > _maxIndex) |
| 40 | + _maxIndex = index; |
39 | 41 |
|
40 | 42 | var valueBs = Encoding.GetBytes(value); |
41 | | - if (value.Length > 32767) //check info length, becasue cell string max length is 47483647 |
| 43 | + if (value.Length > ExcelCellMaxLength) //check info length, becasue cell string max length is 47483647 |
42 | 44 | throw new ArgumentOutOfRangeException("", "Excel one cell max length is 32,767 characters"); |
43 | 45 |
|
44 | 46 | _positionFs.Write(BitConverter.GetBytes(_valueFs.Position), 0, 4); |
@@ -105,13 +107,13 @@ public bool Remove(KeyValuePair<int, string> item) |
105 | 107 |
|
106 | 108 | public IEnumerator<KeyValuePair<int, string>> GetEnumerator() |
107 | 109 | { |
108 | | - for (int i = 0; i < _maxIndx; i++) |
| 110 | + for (int i = 0; i <= _maxIndex; i++) |
109 | 111 | yield return new KeyValuePair<int, string>(i, this[i]); |
110 | 112 | } |
111 | 113 |
|
112 | 114 | IEnumerator IEnumerable.GetEnumerator() |
113 | 115 | { |
114 | | - for (int i = 0; i < _maxIndx; i++) |
| 116 | + for (int i = 0; i <= _maxIndex; i++) |
115 | 117 | yield return this[i]; |
116 | 118 | } |
117 | 119 |
|
|
0 commit comments