diff --git a/Tests/Jinget.Core.Tests/Jinget.Core.Tests.csproj b/Tests/Jinget.Core.Tests/Jinget.Core.Tests.csproj index d161914..8b31b56 100644 --- a/Tests/Jinget.Core.Tests/Jinget.Core.Tests.csproj +++ b/Tests/Jinget.Core.Tests/Jinget.Core.Tests.csproj @@ -22,10 +22,4 @@ - - - Always - - - diff --git a/Tests/Jinget.Core.Tests/Utilities/Compression/ZipUtilityTests.cs b/Tests/Jinget.Core.Tests/Utilities/Compression/ZipUtilityTests.cs index cd499a0..86e0cab 100644 --- a/Tests/Jinget.Core.Tests/Utilities/Compression/ZipUtilityTests.cs +++ b/Tests/Jinget.Core.Tests/Utilities/Compression/ZipUtilityTests.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Security.Cryptography; using Jinget.Core.Utilities.Compression; namespace Jinget.Core.Tests.Utilities.Compression; @@ -6,6 +7,26 @@ namespace Jinget.Core.Tests.Utilities.Compression; [TestClass] public class ZipUtilityTests { + string Create15MBFile() + { + string filename = Path.ChangeExtension(Path.GetTempFileName(), ".txt"); + long fileSizeInBytes = 15 * 1024 * 1024; // 15MB + using (FileStream fs = new(filename, FileMode.Create, FileAccess.Write)) + using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) + { + byte[] buffer = new byte[1024 * 1024]; // 1 MB buffer + long bytesWritten = 0; + + while (bytesWritten < fileSizeInBytes) + { + int bytesToWrite = (int)Math.Min(buffer.Length, fileSizeInBytes - bytesWritten); + rng.GetBytes(buffer, 0, bytesToWrite); + fs.Write(buffer, 0, bytesToWrite); + bytesWritten += bytesToWrite; + } + } + return filename; + } private async Task CompressAsync(string fileName, string password = "") { using (var tw = new StreamWriter(fileName, true)) @@ -18,9 +39,9 @@ private async Task CompressAsync(string fileName, string password = "" return tobeCompressed[0]; } - private async Task CompressLargeFileAsync(string fileName, string password = "", int eachFileMaxSize = 1) + private async Task CompressLargeFileAsync(string password = "", int eachFileMaxSize = 1) { - new FileInfo("sample.txt").CopyTo(fileName); + string fileName = Create15MBFile(); FileInfo file = new(fileName); int maxDOP = 5; @@ -32,13 +53,12 @@ private async Task CompressLargeFileAsync(string fileName, string pass [TestCleanup] public void Cleanup() { - string[] zipFiles = Directory.GetFiles(".", "*.zip"); // Get all zip files in the root directory + string[] zipFiles = Directory.GetFiles(".", "*.zip"); foreach (string zipFile in zipFiles) { File.Delete(zipFile); } - string[] txtFiles = Directory.GetFiles(".", "*.txt") - .Where(x => x != ".\\sample.txt").ToArray(); // Get all zip files in the root directory + string[] txtFiles = Directory.GetFiles(".", "*.txt"); foreach (string txtFile in txtFiles) { File.Delete(txtFile); @@ -88,24 +108,24 @@ public async Task should_decompressAsync() [TestMethod] public async Task should_compress_large_file_and_chunkAsync() { - var compressedFile = await CompressLargeFileAsync($"{Guid.NewGuid()}.txt", eachFileMaxSize: 1); + var compressedFile = await CompressLargeFileAsync(eachFileMaxSize: 1); string compressedFileName = $"{Path.GetFileNameWithoutExtension(compressedFile.Name)}"; - Assert.IsTrue(compressedFile.Directory.GetFiles($"{compressedFileName}.zip").Length == 1); - Assert.IsTrue(compressedFile.Directory.GetFiles($"{compressedFileName}-part?.zip").Length > 0); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}.zip").Length == 1); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}-part?.zip").Length > 0); } [TestMethod] public async Task should_decompress_large_file_with_chunkAsync() { - var compressedFile = await CompressLargeFileAsync($"{Guid.NewGuid()}.txt", eachFileMaxSize: 1); + var compressedFile = await CompressLargeFileAsync(eachFileMaxSize: 1); string compressedFileName = $"{Path.GetFileNameWithoutExtension(compressedFile.Name)}"; FileInfo file = new($"{compressedFileName}.zip"); List files = [file]; await ZipUtility.DecompressAsync([.. files], files[0].DirectoryName); - Assert.IsTrue(file.Directory.GetFiles($"{compressedFileName}.txt").Length == 1); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}.txt").Length == 1); } [TestMethod] @@ -149,23 +169,23 @@ await Assert.ThrowsExceptionAsync(async () => [TestMethod] public async Task should_compress_large_file_using_password_and_chunkAsync() { - var compressedFile = await CompressLargeFileAsync($"{Guid.NewGuid()}.txt", password: "123", eachFileMaxSize: 1); + var compressedFile = await CompressLargeFileAsync(password: "123", eachFileMaxSize: 1); string compressedFileName = $"{Path.GetFileNameWithoutExtension(compressedFile.Name)}"; - Assert.IsTrue(compressedFile.Directory.GetFiles($"{compressedFileName}.zip").Length == 1); - Assert.IsTrue(compressedFile.Directory.GetFiles($"{compressedFileName}-part?.zip").Length > 0); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}.zip").Length == 1); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}-part?.zip").Length > 0); } [TestMethod] public async Task should_decompress_large_file_using_password_and_chunkAsync() { - var compressedFile = await CompressLargeFileAsync($"{Guid.NewGuid()}.txt", password: "123", eachFileMaxSize: 1); + var compressedFile = await CompressLargeFileAsync(password: "123", eachFileMaxSize: 1); string compressedFileName = $"{Path.GetFileNameWithoutExtension(compressedFile.Name)}"; FileInfo file = new($"{compressedFileName}.zip"); List files = [file]; await ZipUtility.DecompressAsync([.. files], files[0].DirectoryName, password: "123"); - Assert.IsTrue(file.Directory.GetFiles($"{compressedFileName}.txt").Length == 1); + Assert.IsTrue(Directory.GetFiles(".", $"{compressedFileName}.txt").Length == 1); } } \ No newline at end of file diff --git a/Tests/Jinget.Core.Tests/sample.txt b/Tests/Jinget.Core.Tests/sample.txt deleted file mode 100644 index 8e72db1..0000000 --- a/Tests/Jinget.Core.Tests/sample.txt +++ /dev/null @@ -1,135233 +0,0 @@ - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - c# - ArgumentOutOfRangeException when reading bytes from stream - Stack Overflow - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- - -
- Skip to main content -
- -
- -
- - - - - - - - - - - - - - -
-
- - - - - - - -
- - - - - - - - -
- - - -
- - -
- - - -
-
- Asked - -
-
- Modified - 14 years, 5 months ago -
-
- Viewed - 4k times -
-
- - - -
- -
- - -
-
- - -
- - -
- 0 -
- - - - - - - - - - - - - - - - - -
- -
- - - -
- -
- -

I'm trying to read the response stream from an HttpWebResponse object. I know the length of the stream (_response.ContentLength) however I keep getting the following exception:

- -

Specified argument was out of the range of valid values. -Parameter name: size

- -

While debugging, I noticed that at the time of the error, the values were as such:

- -

length = 15032 //the length of the stream as defined by _response.ContentLength

- -

bytesToRead = 7680 //the number of bytes in the stream that still need to be read

- -

bytesRead = 7680 //the number of bytes that have been read (offset)

- -

body.length = 15032 //the size of the byte[] the stream is being copied to

- -

The peculiar thing is that the bytesToRead and bytesRead variables are ALWAYS 7680, regardless of the size of the stream (contained in the length variable). Any ideas?

- -

Code:

- -
int length = (int)_response.ContentLength;
-
-byte[] body = null;
-
-if (length > 0)
-{
-    int bytesToRead = length;
-    int bytesRead = 0;
-
-    try
-    {
-        body = new byte[length];
-
-        using (Stream stream = _response.GetResponseStream())
-        {
-            while (bytesToRead > 0)
-            {                                                        
-                // Read may return anything from 0 to length.
-                int n = stream.Read(body, bytesRead, length);
-
-                // The end of the file is reached.
-                if (n == 0)
-                    break;
-
-                bytesRead += n;
-                bytesToRead -= n;
-            }
-            stream.Close();
-        }
-    }
-    catch (Exception exception)
-    {
-        throw;
-    }   
-}
-else
-{
-    body = new byte[0];
-}
-
-_responseBody = body;
-
-
- - - -
-
-
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - -
- -
- - -
- -
- - -
-
-
-
- -
- -
-
- - - -
-
-
- -
- - - - - -
-
-
    - -
-
-
-
-
- - -
-
- -
-
- -
- - - - - -
- -
-
-
-

- 1 Answer - 1 -

-
-
- - -
-
- - - Reset to default - -
-
- -
-
- - -
-
- -
- - - - -
-
-
- - -
- - -
- 2 -
- - - - - - - - - - - - - -
-
- -
-
- - - - -
- -
- - - -
- -
-

You want this line:

- -
int n = stream.Read(body, bytesRead, length);
-
- -

to be this:

- -
int n = stream.Read(body, bytesRead, bytesToRead);
-
- -

You are saying the maximum number of bytes to read is the stream's length, but it isn't since it is actually only the remaining information in the stream after the offset has been applied.

- -

You also shouldn't need this part:

- -
if (n == 0)
-   break;
-
- -

The while should end the reading correctly, and it is possible that you won't read any bytes before you have finished the whole thing (if the stream is filling slower than you are taking the data out of it)

-
-
-
- -
- - - -
- -
- -
-
CC BY-SA 2.5
-
- - -
- Edit -
- -
- -
- - - - -
- -
- - -
-
-
-
-
- -
- - -
- - - -
-
- - -
- -
- - - - - 1 -
-
-
    - -
  • -
    -
    -
    -
    - - - -
    -
    - -
    -
    -
    -
    - - You, sir, are a genius. How would I go about remedying the second problem (stream filling slower than I am reading it)? Wouldn't setting bytesToRead = length ensure that all of the bytes of the stream were being read? - -
    -– user345194 -
    - - Commented - May 19, 2010 at 16:48 - -
    -
    -
  • - -
-
-
-
-
- - -
-
-
- - - -
- - - -

- Your Answer -

- - - - - - -
- - -
-
-
- - -
-
- -
-
-
-
-
- - - - - -
- - -
- -
-
-
- -
- -
-
- -
-
- -
- - -
- -
- - -
- -
- -
- - -
-
-
- - - -

-
-Not the answer you're looking for? Browse other questions tagged or ask your own question.
-

-
-
- - - - -
- -
- - - - -
- - -
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file