11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Threading . Tasks ;
45
@@ -115,6 +116,62 @@ public void Dispose()
115116 }
116117 }
117118
119+ ~ LocalFile ( )
120+ {
121+ Dispose ( ) ;
122+ }
123+
124+ private void CloseFileStream ( )
125+ {
126+ _stream ? . Dispose ( ) ;
127+ _stream = null ;
128+ }
129+
130+ public void Close ( )
131+ {
132+ lock ( _lockObject )
133+ {
134+ CloseFileStream ( ) ;
135+ }
136+ }
137+
138+ public async Task < LocalFile > CopyFromStreamAsync ( Stream stream )
139+ {
140+ await stream . CopyToAsync ( FileStream ) ;
141+ FileStream . Dispose ( ) ;
142+ return this ;
143+ }
144+
145+ public static async Task < LocalFile > FromStreamAsync ( Stream stream )
146+ {
147+ var file = new LocalFile ( ) ;
148+ await stream . CopyToAsync ( file . FileStream ) ;
149+ file . FileStream . Dispose ( ) ;
150+
151+ return file ;
152+ }
153+
154+ public static async Task < LocalFile > FromStreamAsync ( Stream stream , string fileName )
155+ {
156+ var file = FromFileName ( fileName ) ;
157+ await stream . CopyToAsync ( file . FileStream ) ;
158+ await file . FileStream . DisposeAsync ( ) ;
159+
160+ return file ;
161+ }
162+
163+ public static LocalFile FromFileName ( string fileName )
164+ {
165+ return new LocalFile ( Path . Combine ( Path . GetTempPath ( ) , fileName ) ) ;
166+ }
167+
168+ public static LocalFile FromTempFile ( )
169+ {
170+ return new LocalFile ( ) ;
171+ }
172+
173+ #region Read
174+
118175 public string ReadAllText ( )
119176 {
120177 lock ( _lockObject )
@@ -151,57 +208,90 @@ public Task<string[]> ReadAllLinesAsync()
151208 }
152209 }
153210
154- ~ LocalFile ( )
211+ public byte [ ] ReadAllBytes ( )
155212 {
156- Dispose ( ) ;
213+ lock ( _lockObject )
214+ {
215+ CloseFileStream ( ) ;
216+ return File . ReadAllBytes ( FilePath ) ;
217+ }
157218 }
158219
159- private void CloseFileStream ( )
220+ public Task < byte [ ] > ReadAllBytesAsync ( )
160221 {
161- _stream ? . Dispose ( ) ;
162- _stream = null ;
222+ lock ( _lockObject )
223+ {
224+ CloseFileStream ( ) ;
225+ return File . ReadAllBytesAsync ( FilePath ) ;
226+ }
163227 }
164228
165- public void Close ( )
229+ public IEnumerable < string > ReadLines ( )
166230 {
167231 lock ( _lockObject )
168232 {
169233 CloseFileStream ( ) ;
234+ return File . ReadLines ( FilePath ) ;
170235 }
171236 }
172237
173- public async Task < LocalFile > CopyFromStreamAsync ( Stream stream )
238+ #endregion
239+
240+ #region Write
241+
242+ public void WriteAllText ( string content )
174243 {
175- await stream . CopyToAsync ( FileStream ) ;
176- FileStream . Dispose ( ) ;
177- return this ;
244+ lock ( _lockObject )
245+ {
246+ CloseFileStream ( ) ;
247+ File . WriteAllText ( FilePath , content ) ;
248+ }
178249 }
179250
180- public static async Task < LocalFile > FromStreamAsync ( Stream stream )
251+ public Task WriteAllTextAsync ( string content )
181252 {
182- var file = new LocalFile ( ) ;
183- await stream . CopyToAsync ( file . FileStream ) ;
184- file . FileStream . Dispose ( ) ;
185-
186- return file ;
253+ lock ( _lockObject )
254+ {
255+ CloseFileStream ( ) ;
256+ return File . WriteAllTextAsync ( FilePath , content ) ;
257+ }
187258 }
188259
189- public static async Task < LocalFile > FromStreamAsync ( Stream stream , string fileName )
260+ public void WriteAllLines ( IEnumerable < string > contents )
190261 {
191- var file = FromFileName ( fileName ) ;
192- await stream . CopyToAsync ( file . FileStream ) ;
193- await file . FileStream . DisposeAsync ( ) ;
262+ lock ( _lockObject )
263+ {
264+ CloseFileStream ( ) ;
265+ File . WriteAllLines ( FilePath , contents ) ;
266+ }
267+ }
194268
195- return file ;
269+ public Task WriteAllLinesAsync ( IEnumerable < string > contents )
270+ {
271+ lock ( _lockObject )
272+ {
273+ CloseFileStream ( ) ;
274+ return File . WriteAllLinesAsync ( FilePath , contents ) ;
275+ }
196276 }
197277
198- public static LocalFile FromFileName ( string fileName )
278+ public void WriteAllBytes ( byte [ ] bytes )
199279 {
200- return new LocalFile ( Path . Combine ( Path . GetTempPath ( ) , fileName ) ) ;
280+ lock ( _lockObject )
281+ {
282+ CloseFileStream ( ) ;
283+ File . WriteAllBytes ( FilePath , bytes ) ;
284+ }
201285 }
202286
203- public static LocalFile FromTempFile ( )
287+ public Task WriteAllBytesAsync ( byte [ ] bytes )
204288 {
205- return new LocalFile ( ) ;
289+ lock ( _lockObject )
290+ {
291+ CloseFileStream ( ) ;
292+ return File . WriteAllBytesAsync ( FilePath , bytes ) ;
293+ }
206294 }
295+
296+ #endregion
207297}
0 commit comments