internal int SetResponse(WebResponse response) {
ContentLength = response.ContentLength;
if (ContentLength == -1 || ContentLength > DefaultDownloadBufferLength) {
Length = DefaultDownloadBufferLength; // Read buffer length
} else {
Length = ContentLength; // Read buffer length
}
// If we are not writing to a stream, we are accumulating in memory
if (WriteStream == null) {
// We are putting a cap on the size we will accumulate in memory
if (ContentLength > Int32.MaxValue)
{
throw new WebException(SR.GetString(SR.net_webstatus_MessageLengthLimitExceeded), WebExceptionStatus.MessageLengthLimitExceeded);
}
SgBuffers = new ScatterGatherBuffers(Length); // Write buffer
}
InnerBuffer = new byte[(int)Length];
ReadStream = response.GetResponseStream();
if (Async && response.ContentLength >= 0)
Progress.TotalBytesToReceive = response.ContentLength;
if (Async) {
if (ReadStream == null || ReadStream == Stream.Null)
DownloadBitsReadCallbackState(this, null);
else
ReadStream.BeginRead(InnerBuffer, Offset, (int)Length-Offset, new AsyncCallback(DownloadBitsReadCallback), this);
} else {
if (ReadStream == null || ReadStream == Stream.Null)
return 0;
else
return ReadStream.Read(InnerBuffer, Offset, (int)Length-Offset);
}
return -1;
}