Stream.CopyTo bug?

Jun 22, 2010 at 1:14 PM


I haven't tested this, but I just came across something that seems like a bug in StreamExtension.cs

public static Stream CopyTo(this Stream stream, Stream targetStream, int bufferSize)
    if (stream.CanRead == false) throw new InvalidOperationException("Source stream does not support reading.");
    if (targetStream.CanRead == false) throw new InvalidOperationException("Target stream does not support writing.");

    var buffer = new byte[bufferSize];
    var bytesRead = 0;

    while ((bytesRead = stream.Read(buffer, 0, bufferSize)) > 0)
        targetStream.Write(buffer, 0, bytesRead);
    return stream;

The second line reads "targetStream.CanRead ==false". Shouldn't that be "CanWrite == false"?

Jun 23, 2010 at 1:59 PM

Thanks, this has been fixed.