7
7elephants
I have the following piece of code to take data from one stream and put
it into another...
Int32 bufferSize = 100;
Int32.TryParse(ConfigurationManager.AppSettings["bufferLimit"].ToString(),
out bufferSize);
byte[] buffer = new byte[bufferSize];
//make sure stream is at the beginning
inputStream.Position = 0;
while (true) {
//read input stream
int bytesRead = inputStream.Read(buffer, offset,
bufferSize);
if (bytesRead > 0) {
//write to output stream
outputStream.Write(buffer, offset, bytesRead);
offset += bytesRead;
} else {
break;
}
}
+--------------------------------------------------------------+
When I run the code above, I get a System.ArgumentException with the
following message "Offset and length were out of bounds for the array
or count is greater than the number of elements from index to the end
of the source collection." In the test file I am using, all the data
is read in the first read, but I can't be guaranteed that when in
PRODUCTION mode. I also can't be guaranteed that the input stream is
text-based (or else I would use readers and writers).
So, my two questions are 1) Can anybody tell me when I am getting this
error? and 2) Is there a better way for me to do this?
In my research before posting I found
http://www.geocities.com/firepower_50ae/CodeNote/dot-Net-Error-FileSystem-Read.html,
but I don't necessary understand how to correct the problem.
Thanks for any help in advance.
it into another...
Int32 bufferSize = 100;
Int32.TryParse(ConfigurationManager.AppSettings["bufferLimit"].ToString(),
out bufferSize);
byte[] buffer = new byte[bufferSize];
//make sure stream is at the beginning
inputStream.Position = 0;
while (true) {
//read input stream
int bytesRead = inputStream.Read(buffer, offset,
bufferSize);
if (bytesRead > 0) {
//write to output stream
outputStream.Write(buffer, offset, bytesRead);
offset += bytesRead;
} else {
break;
}
}
+--------------------------------------------------------------+
When I run the code above, I get a System.ArgumentException with the
following message "Offset and length were out of bounds for the array
or count is greater than the number of elements from index to the end
of the source collection." In the test file I am using, all the data
is read in the first read, but I can't be guaranteed that when in
PRODUCTION mode. I also can't be guaranteed that the input stream is
text-based (or else I would use readers and writers).
So, my two questions are 1) Can anybody tell me when I am getting this
error? and 2) Is there a better way for me to do this?
In my research before posting I found
http://www.geocities.com/firepower_50ae/CodeNote/dot-Net-Error-FileSystem-Read.html,
but I don't necessary understand how to correct the problem.
Thanks for any help in advance.