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 // See the LICENSE file in the project root for more information.
4 
5 using System.Diagnostics;
6 using System.IO;
7 using System.Threading;
8 using System.Threading.Tasks;
9 
10 namespace System.Net.Http
11 {
12     internal abstract class HttpContentWriteStream : HttpContentStream
13     {
14         protected HttpConnection _connection;
15 
HttpContentWriteStream(HttpConnection connection, CancellationToken cancellationToken)16         public HttpContentWriteStream(HttpConnection connection, CancellationToken cancellationToken)
17         {
18             Debug.Assert(connection != null);
19             _connection = connection;
20             RequestCancellationToken = cancellationToken;
21         }
22 
23         /// <summary>Cancellation token associated with the send operation.</summary>
24         /// <remarks>
25         /// Because of how this write stream is used, the CancellationToken passed into the individual
26         /// stream operations will be the default non-cancelable token and can be ignored.  Instead,
27         /// this token is used.
28         /// </remarks>
29         internal CancellationToken RequestCancellationToken { get; }
30 
31         public override bool CanRead => false;
32         public override bool CanSeek => false;
33         public override bool CanWrite => true;
34 
35         public override long Length => throw new NotSupportedException();
36 
37         public override long Position
38         {
39             get { throw new NotSupportedException(); }
40             set { throw new NotSupportedException(); }
41         }
42 
Flush()43         public override void Flush() => FlushAsync().GetAwaiter().GetResult();
44 
Seek(long offset, SeekOrigin origin)45         public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
46 
SetLength(long value)47         public override void SetLength(long value) => throw new NotSupportedException();
48 
Read(byte[] buffer, int offset, int count)49         public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
50 
Write(byte[] buffer, int offset, int count)51         public override void Write(byte[] buffer, int offset, int count) =>
52             WriteAsync(buffer, offset, count, CancellationToken.None).GetAwaiter().GetResult();
53 
FinishAsync()54         public abstract Task FinishAsync();
55 
Dispose(bool disposing)56         protected override void Dispose(bool disposing)
57         {
58             if (disposing)
59             {
60                 if (_connection != null)
61                 {
62                     _connection.Dispose();
63                     _connection = null;
64                 }
65             }
66 
67             base.Dispose(disposing);
68         }
69     }
70 }
71