aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/StreamHelper.cs
diff options
context:
space:
mode:
authorBond_009 <Bond.009@outlook.com>2019-03-28 23:19:56 +0100
committerBond_009 <Bond.009@outlook.com>2019-03-28 23:26:43 +0100
commit41df562419d8f1681a9720ab1c62ffb9ad0f96cb (patch)
tree7ba05b8334df865560a46be46c0447ab5c5933b1 /Emby.Server.Implementations/IO/StreamHelper.cs
parent2dbc1153e8b96d562a5e4489e6fe883e8711839f (diff)
Improve IO code
* Style changes * Remove remnants of SMB support * Use `GetInvalidFileNameChars` instead of rolling our own * Remove possible unexpected behaviour with async file streams * Remove some dead code
Diffstat (limited to 'Emby.Server.Implementations/IO/StreamHelper.cs')
-rw-r--r--Emby.Server.Implementations/IO/StreamHelper.cs74
1 files changed, 8 insertions, 66 deletions
diff --git a/Emby.Server.Implementations/IO/StreamHelper.cs b/Emby.Server.Implementations/IO/StreamHelper.cs
index d02cd84a0..7c8c079e3 100644
--- a/Emby.Server.Implementations/IO/StreamHelper.cs
+++ b/Emby.Server.Implementations/IO/StreamHelper.cs
@@ -17,11 +17,11 @@ namespace Emby.Server.Implementations.IO
try
{
int read;
- while ((read = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
+ while ((read = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
cancellationToken.ThrowIfCancellationRequested();
- await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);
if (onStarted != null)
{
@@ -44,11 +44,11 @@ namespace Emby.Server.Implementations.IO
if (emptyReadLimit <= 0)
{
int read;
- while ((read = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)
+ while ((read = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
cancellationToken.ThrowIfCancellationRequested();
- await destination.WriteAsync(buffer, 0, read).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);
}
return;
@@ -60,7 +60,7 @@ namespace Emby.Server.Implementations.IO
{
cancellationToken.ThrowIfCancellationRequested();
- var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
+ var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
if (bytesRead == 0)
{
@@ -71,7 +71,7 @@ namespace Emby.Server.Implementations.IO
{
eofCount = 0;
- await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
}
}
}
@@ -109,64 +109,6 @@ namespace Emby.Server.Implementations.IO
}
}
- public async Task<int> CopyToAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
- {
- byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
- try
- {
- int bytesRead;
- int totalBytesRead = 0;
-
- while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0)
- {
- var bytesToWrite = bytesRead;
-
- if (bytesToWrite > 0)
- {
- await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
-
- totalBytesRead += bytesRead;
- }
- }
-
- return totalBytesRead;
- }
- finally
- {
- ArrayPool<byte>.Shared.Return(buffer);
- }
- }
-
- public async Task CopyToAsyncWithSyncRead(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
- {
- byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
- try
- {
- int bytesRead;
-
- while ((bytesRead = source.Read(buffer, 0, buffer.Length)) != 0)
- {
- var bytesToWrite = Math.Min(bytesRead, copyLength);
-
- if (bytesToWrite > 0)
- {
- await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
- }
-
- copyLength -= bytesToWrite;
-
- if (copyLength <= 0)
- {
- break;
- }
- }
- }
- finally
- {
- ArrayPool<byte>.Shared.Return(buffer);
- }
- }
-
public async Task CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
{
byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
@@ -208,7 +150,7 @@ namespace Emby.Server.Implementations.IO
if (bytesRead == 0)
{
- await Task.Delay(100).ConfigureAwait(false);
+ await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
}
}
@@ -225,7 +167,7 @@ namespace Emby.Server.Implementations.IO
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
- await destination.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
+ await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
}