Implement NullStream
This commit is contained in:
parent
41cd8d8623
commit
73da9da5ef
|
@ -0,0 +1,56 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NullSharp
|
||||
{
|
||||
public class NullStream : Stream
|
||||
{
|
||||
public const string DefaultEndpoint = "https://devnull-as-a-service.com/dev/null";
|
||||
private readonly HttpClient Client;
|
||||
|
||||
public NullStream() : this(DefaultEndpoint) { }
|
||||
|
||||
public NullStream(string endpoint) : this(new Uri(endpoint)) { }
|
||||
|
||||
public NullStream(Uri endpoint) {
|
||||
this.Client = new HttpClient() { BaseAddress = endpoint };
|
||||
this.Client.DefaultRequestHeaders.Add("User-Agent", "NullSharp");
|
||||
}
|
||||
|
||||
public override bool CanRead => false;
|
||||
|
||||
public override bool CanSeek => false;
|
||||
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public override bool CanTimeout => true;
|
||||
|
||||
public override long Length => throw new NotSupportedException();
|
||||
|
||||
public override long Position {
|
||||
get => throw new NotSupportedException();
|
||||
set => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Flush() { }
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override int WriteTimeout {
|
||||
get => (int)this.Client.Timeout.TotalMilliseconds;
|
||||
set => this.Client.Timeout = new TimeSpan(0, 0, 0, 0, value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => this.WriteAsync(buffer, offset, count).Wait();
|
||||
|
||||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
||||
=> this.Client.PostAsync("", new ByteArrayContent(buffer), cancellationToken);
|
||||
}
|
||||
}
|
Reference in New Issue