aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2014-04-07 12:48:57 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2014-04-07 12:48:57 -0400
commit63aea3d90836765ceb38f0b9b130970af9b132d9 (patch)
tree811867ce7235b47e0baa87c51c895f77bf04d6cd
parent17a2beb7a2c548e85c5aef16178bc257314d54e6 (diff)
Remove buffering from RangeRequestWriter
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs36
1 files changed, 32 insertions, 4 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs
index 08e9403aa..1ff199eb4 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/RangeRequestWriter.cs
@@ -1,4 +1,5 @@
-using ServiceStack.Web;
+using System.Threading;
+using ServiceStack.Web;
using System;
using System.Collections.Generic;
using System.Globalization;
@@ -178,8 +179,35 @@ namespace MediaBrowser.Server.Implementations.HttpServer
using (var source = SourceStream)
{
- //Since we've already set the postion of the sourcestream, just copy the remains to the output
- await source.CopyToAsync(responseStream).ConfigureAwait(false);
+ // If the requested range is "0-", we can optimize by just doing a stream copy
+ if (RangeEnd >= TotalContentLength - 1)
+ {
+ await source.CopyToAsync(responseStream).ConfigureAwait(false);
+ }
+ else
+ {
+ await CopyToAsyncInternal(source, responseStream, Convert.ToInt32(RangeLength), CancellationToken.None).ConfigureAwait(false);
+ }
+ }
+ }
+
+ private async Task CopyToAsyncInternal(Stream source, Stream destination, int copyLength, CancellationToken cancellationToken)
+ {
+ const int bufferSize = 81920;
+ var array = new byte[bufferSize];
+ int count;
+ while ((count = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
+ {
+ var bytesToCopy = Math.Min(count, copyLength);
+
+ await destination.WriteAsync(array, 0, bytesToCopy, cancellationToken).ConfigureAwait(false);
+
+ copyLength -= bytesToCopy;
+
+ if (copyLength <= 0)
+ {
+ break;
+ }
}
}
@@ -201,4 +229,4 @@ namespace MediaBrowser.Server.Implementations.HttpServer
public string StatusDescription { get; set; }
}
-}
+} \ No newline at end of file