Stream.CopyToを使用して特定のバイト数のみを宛先ストリームにコピーする方法はありますか?最善の回避策は何ですか?
編集:
私の回避策(一部のコードは省略):
internal sealed class Substream : Stream
{
private readonly Stream stream;
private readonly long origin;
private readonly long length;
private long position;
public Substream(Stream stream, long length)
{
this.stream = stream;
this.origin = stream.Position;
this.position = stream.Position;
this.length = length;
}
public override int Read(byte[] buffer, int offset, int count)
{
var n = Math.Max(Math.Min(count, origin + length - position), 0);
int bytesRead = stream.Read(buffer, offset, (int) n);
position += bytesRead;
return bytesRead;
}
}
次に、nバイトをコピーします。
var substream = new Substream(stream, n);
substream.CopyTo(stm);