improve stream disposal

This commit is contained in:
Luke Pulverenti 2013-05-07 14:57:27 -04:00
parent 156453cc42
commit baa779fb31
2 changed files with 9 additions and 15 deletions

View File

@ -410,11 +410,13 @@ namespace MediaBrowser.Server.Implementations.IO
return false;
}
FileStream stream = null;
try
{
stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
using (new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//file is not locked
return false;
}
}
catch
{
@ -424,14 +426,6 @@ namespace MediaBrowser.Server.Implementations.IO
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return false;
}
/// <summary>

View File

@ -457,9 +457,9 @@ namespace MediaBrowser.Server.Implementations.Providers
{
var ms = new MemoryStream();
using (var input = dataToSave)
using (dataToSave)
{
await input.CopyToAsync(ms).ConfigureAwait(false);
await dataToSave.CopyToAsync(ms).ConfigureAwait(false);
}
ms.Position = 0;
@ -470,9 +470,9 @@ namespace MediaBrowser.Server.Implementations.Providers
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
using (var input = dataToSave)
using (dataToSave)
{
await input.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
await dataToSave.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
}
}