lucidiot/NullSharp
lucidiot
/
NullSharp
Archived
1
0
Fork 0

Implement NullStream

This commit is contained in:
Lucidiot 2019-04-08 01:05:52 +02:00
parent 41cd8d8623
commit 73da9da5ef
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
1 changed files with 56 additions and 0 deletions

56
NullSharp/NullStream.cs Normal file
View File

@ -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);
}
}