Skip to content

Commit d005f39

Browse files
committed
Refactor SimaiFile for flexibility and resource management
SimaiFile class was refactored to improve the flexibility and ensure the proper disposal of the StreamReader object. Now, different types of file input (FileSystemInfo, string, Stream, StreamReader) are supported, which optimizes the file reading depending on the available input format. A IDisposable interface was implemented to ensure that the StreamReader (_simaiReader) is properly disposed, which helps to manage system resources by freeing up memory when the object is no longer needed. A 'Dispose' method was added to close the StreamReader when it's no longer needed. The changes lead to more flexible and efficient file reading and help manage system resources effectively.
1 parent 432f26c commit d005f39

1 file changed

Lines changed: 44 additions & 17 deletions

File tree

SimaiSharp/src/SimaiFile.cs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,63 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Text;
56
using SimaiSharp.Internal;
67

78
namespace SimaiSharp
89
{
9-
public sealed class SimaiFile
10+
public sealed class SimaiFile : IDisposable
1011
{
11-
private readonly string _fullFilePath;
12+
private readonly StreamReader _simaiReader;
1213

13-
public SimaiFile(string path)
14-
{
15-
_fullFilePath = path;
16-
}
17-
18-
public IEnumerable<KeyValuePair<string, string>> ToKeyValuePairs()
14+
public SimaiFile(FileSystemInfo file)
1915
{
2016
const int sampleSize = 64;
2117

22-
using var fileStream = new FileStream(_fullFilePath, FileMode.Open, FileAccess.Read);
18+
var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
2319

2420
// Determine the encoding of the file
2521
var buffer = new byte[64];
2622
var numCharsRead = fileStream.Read(buffer, 0, 64);
2723
var encoding = buffer[..numCharsRead].TryGetEncoding(sampleSize);
2824

29-
using var reader = new StreamReader(fileStream, encoding);
30-
// We've already read 64 chars in line 27, so we'll reset here.
31-
reader.BaseStream.Position = 0;
25+
// We've already read 64 chars, so we'll reset here.
26+
fileStream.Position = 0;
27+
28+
_simaiReader = new StreamReader(fileStream, encoding);
29+
}
30+
31+
public SimaiFile(string text)
32+
{
33+
var stream = new MemoryStream();
34+
35+
using var writer = new StreamWriter(stream);
36+
writer.Write(text);
37+
writer.Flush();
38+
stream.Position = 0;
39+
40+
_simaiReader = new StreamReader(stream);
41+
}
42+
43+
public SimaiFile(Stream stream)
44+
{
45+
_simaiReader = new StreamReader(stream);
46+
}
47+
48+
public SimaiFile(StreamReader reader)
49+
{
50+
_simaiReader = reader;
51+
}
3252

33-
var currentKey = "";
53+
public IEnumerable<KeyValuePair<string, string>> ToKeyValuePairs()
54+
{
55+
var currentKey = string.Empty;
3456
var currentValue = new StringBuilder();
3557

36-
while (!reader.EndOfStream)
58+
while (!_simaiReader.EndOfStream)
3759
{
38-
var line = reader.ReadLine();
60+
var line = _simaiReader.ReadLine();
3961

4062
if (line == null)
4163
break;
@@ -66,5 +88,10 @@ public string GetValue(string key)
6688
{
6789
return ToKeyValuePairs().FirstOrDefault(parameterPair => parameterPair.Key == key).Value;
6890
}
91+
92+
public void Dispose()
93+
{
94+
_simaiReader.Dispose();
95+
}
6996
}
70-
}
97+
}

0 commit comments

Comments
 (0)