public override int Read (byte [] buffer, int offset, int count)
{
if (buffer == null)
throw new ArgumentNullException ("buffer");
if (offset < 0 || count < 0)
throw new ArgumentOutOfRangeException ("offset or count less than zero.");
if (buffer.Length - offset < count )
throw new ArgumentException ("offset+count",
"The size of the buffer is less than offset + count.");
if (count == 0 || request.worker_request == null)
return 0;
if (!checked_maxRequestLength) {
int content_length_kb = content_length / 1024;
HttpRuntimeSection config = HttpRuntime.Section;
if (content_length_kb > config.MaxRequestLength)
throw HttpException.NewWithCode (400, "Upload size exceeds httpRuntime limit.", WebEventCodes.RuntimeErrorPostTooLarge);
else
checked_maxRequestLength = true;
}
// Serve the bytes we might have preloaded already.
if (!preloaded_served) {
if (preloadedBuffer == null)
preloadedBuffer = request.worker_request.GetPreloadedEntityBody ();
if (preloadedBuffer != null) {
long bytes_left = preloadedBuffer.Length-position;
int n = (int) Math.Min (count, bytes_left);
Array.Copy (preloadedBuffer, position, buffer, offset, n);
position += n;
if (n == bytes_left)
preloaded_served = true;
return n;
}
else
preloaded_served = true;
}
// serve bytes from worker request if available
if (position < content_length) {
long bytes_left = content_length-position;
int n = count;
if (bytes_left < count)
n = (int) bytes_left;
int bytes_read = request.worker_request.ReadEntityBody (buffer, offset, n);
position += bytes_read;
return bytes_read;
}
return 0;
}