ln.protocols.helper/RequestContentStream.cs

70 lines
2.0 KiB
C#

using System;
using System.IO;
namespace ln.protocols.helper
{
public class RequestContentStream : Stream
{
public Stream BaseStream { get; }
private int contentLength;
private int position;
public RequestContentStream(Stream baseStream, int contentLength)
{
BaseStream = baseStream;
this.contentLength = contentLength;
}
public override int ReadByte()
{
int b = BaseStream.ReadByte();
if (b >= 0)
position++;
return b;
}
public override void Flush() { }
public override int Read(byte[] buffer, int offset, int count)
{
if (position == contentLength)
return 0;
if (position + count > contentLength)
count = contentLength - position;
int nread = BaseStream.Read(buffer, offset, count);
if (nread >= 0)
position += nread;
return nread;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new System.NotImplementedException();
}
public override void SetLength(long value) => throw new System.NotImplementedException();
public override void Write(byte[] buffer, int offset, int count) => throw new System.NotImplementedException();
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => contentLength;
public override long Position
{
get => position;
set => throw new NotImplementedException();
}
protected override void Dispose(bool disposing)
{
if (position < contentLength)
{
byte[] buffer = new byte[4096];
while (Read(buffer, 0, buffer.Length) > 0) {}
}
}
}
}