61 lines
2.2 KiB
C#
61 lines
2.2 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace ln.http.content;
|
|
|
|
public class HttpContentStreamChunked : Stream
|
|
{
|
|
private Stream _stream;
|
|
private string _tempFileName;
|
|
|
|
public HttpContentStreamChunked(Stream connectionStream)
|
|
{
|
|
byte[] transferBuffer;
|
|
_tempFileName = Path.GetTempFileName();
|
|
_stream = new FileStream(_tempFileName, FileMode.Open, FileAccess.ReadWrite);
|
|
|
|
transferBuffer = new byte[1024 * 1024];
|
|
|
|
int nChunkSize;
|
|
do
|
|
{
|
|
string chunkSize = HttpConnection.ReadLine(connectionStream);
|
|
nChunkSize = int.Parse(chunkSize, System.Globalization.NumberStyles.HexNumber);
|
|
int length = nChunkSize;
|
|
|
|
while (length > 0)
|
|
{
|
|
int size = length > transferBuffer.Length ? transferBuffer.Length : length;
|
|
int nread = connectionStream.Read(transferBuffer, 0, size);
|
|
_stream.Write(transferBuffer, 0, nread);
|
|
length -= nread;
|
|
}
|
|
|
|
HttpConnection.ReadLine(connectionStream);
|
|
} while (nChunkSize != 0);
|
|
|
|
_stream.Position = 0;
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) => _stream.Read(buffer, offset, count);
|
|
public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin);
|
|
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 void Flush() => throw new System.NotImplementedException();
|
|
|
|
public override bool CanRead { get; } = true;
|
|
public override bool CanSeek { get; } = true;
|
|
public override bool CanWrite { get; } = false;
|
|
public override long Length => _stream.Length;
|
|
public override long Position { get => _stream.Position; set => _stream.Position = value; }
|
|
|
|
public override int Read(Span<byte> buffer) => _stream.Read(buffer);
|
|
public override void Close() => _stream.Close();
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
_stream.Dispose();
|
|
if (_tempFileName is not null)
|
|
File.Delete(_tempFileName);
|
|
}
|
|
} |