lucidiot/NullSharp
lucidiot
/
NullSharp
Archived
1
0
Fork 0
This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
NullSharp/NullSharp/NullStream.cs

61 lines
2.1 KiB
C#

using System;
using System.Linq;
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";
protected HttpClient Client { get; set; }
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.Skip(offset).Take(count).ToArray()),
cancellationToken);
}
}