| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace System.Net.Http |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Helper class is used to copy the content of a source stream to a destination stream, |
| | | 13 | | /// with optimizations based on expected usage within HttpClient and with the ability |
| | | 14 | | /// to dispose of the source stream when the copy has completed. |
| | | 15 | | /// </summary> |
| | | 16 | | internal static class StreamToStreamCopy |
| | | 17 | | { |
| | | 18 | | /// <summary>Copies the source stream from its current position to the destination stream at its current positio |
| | | 19 | | /// <param name="source">The source stream from which to copy.</param> |
| | | 20 | | /// <param name="destination">The destination stream to which to copy.</param> |
| | | 21 | | /// <param name="bufferSize">The size of the buffer to allocate if one needs to be allocated. If zero, use the d |
| | | 22 | | /// <param name="disposeSource">Whether to dispose of the source stream after the copy has finished successfully |
| | | 23 | | public static void Copy(Stream source, Stream destination, int bufferSize, bool disposeSource) |
| | 0 | 24 | | { |
| | 0 | 25 | | Debug.Assert(source != null); |
| | 0 | 26 | | Debug.Assert(destination != null); |
| | 0 | 27 | | Debug.Assert(bufferSize >= 0); |
| | | 28 | | |
| | 0 | 29 | | if (bufferSize == 0) |
| | 0 | 30 | | { |
| | 0 | 31 | | source.CopyTo(destination); |
| | 0 | 32 | | } |
| | | 33 | | else |
| | 0 | 34 | | { |
| | 0 | 35 | | source.CopyTo(destination, bufferSize); |
| | 0 | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | if (disposeSource) |
| | 0 | 39 | | { |
| | 0 | 40 | | DisposeSource(source); |
| | 0 | 41 | | } |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Copies the source stream from its current position to the destination stream at its current positio |
| | | 45 | | /// <param name="source">The source stream from which to copy.</param> |
| | | 46 | | /// <param name="destination">The destination stream to which to copy.</param> |
| | | 47 | | /// <param name="bufferSize">The size of the buffer to allocate if one needs to be allocated. If zero, use the d |
| | | 48 | | /// <param name="disposeSource">Whether to dispose of the source stream after the copy has finished successfully |
| | | 49 | | /// <param name="cancellationToken">CancellationToken used to cancel the copy operation.</param> |
| | | 50 | | public static Task CopyAsync(Stream source, Stream destination, int bufferSize, bool disposeSource, Cancellation |
| | 0 | 51 | | { |
| | 0 | 52 | | Debug.Assert(source != null); |
| | 0 | 53 | | Debug.Assert(destination != null); |
| | 0 | 54 | | Debug.Assert(bufferSize >= 0); |
| | | 55 | | |
| | | 56 | | try |
| | 0 | 57 | | { |
| | 0 | 58 | | Task copyTask = bufferSize == 0 ? |
| | 0 | 59 | | source.CopyToAsync(destination, cancellationToken) : |
| | 0 | 60 | | source.CopyToAsync(destination, bufferSize, cancellationToken); |
| | | 61 | | |
| | 0 | 62 | | if (!disposeSource) |
| | 0 | 63 | | { |
| | 0 | 64 | | return copyTask; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | switch (copyTask.Status) |
| | | 68 | | { |
| | | 69 | | case TaskStatus.RanToCompletion: |
| | 0 | 70 | | DisposeSource(source); |
| | 0 | 71 | | return Task.CompletedTask; |
| | | 72 | | |
| | | 73 | | case TaskStatus.Faulted: |
| | | 74 | | case TaskStatus.Canceled: |
| | 0 | 75 | | return copyTask; |
| | | 76 | | |
| | | 77 | | default: |
| | 0 | 78 | | return DisposeSourceAsync(copyTask, source); |
| | | 79 | | |
| | | 80 | | static async Task DisposeSourceAsync(Task copyTask, Stream source) |
| | 0 | 81 | | { |
| | 0 | 82 | | await copyTask.ConfigureAwait(false); |
| | 0 | 83 | | DisposeSource(source); |
| | 0 | 84 | | } |
| | | 85 | | } |
| | | 86 | | } |
| | 0 | 87 | | catch (Exception e) |
| | 0 | 88 | | { |
| | | 89 | | // For compatibility with the previous implementation, catch everything (including arg exceptions) and |
| | | 90 | | // store errors into the task rather than letting them propagate to the synchronous caller. |
| | 0 | 91 | | return Task.FromException(e); |
| | | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary>Disposes the source stream.</summary> |
| | | 96 | | private static void DisposeSource(Stream source) |
| | 0 | 97 | | { |
| | | 98 | | try |
| | 0 | 99 | | { |
| | 0 | 100 | | source.Dispose(); |
| | 0 | 101 | | } |
| | 0 | 102 | | catch (Exception e) |
| | 0 | 103 | | { |
| | | 104 | | // Dispose() should never throw, but since we're on an async codepath, make sure to catch the exception. |
| | 0 | 105 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Error(null, e); |
| | 0 | 106 | | } |
| | 0 | 107 | | } |
| | | 108 | | } |
| | | 109 | | } |