61 lines
2.1 KiB
C#
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);
|
|
}
|
|
}
|