[RESOLVED] Array Copy / Concatenation Problem
I am trying to concatenate several byte arrays into one. I have hit upon a roadblock, where my tests show that I cannot copy more than two arrays into a different array at the same time. Here's what I have tried:
1. Read file1 into byte array1.
2. Read file2 into byte array2.
3. Read file3 into byte array3.
4. Create a byte arrayTotal with length equal to array1.length + array2.length + array3.length
5. Use Buffer.BlockCopy to copy contents of array1 to arrayTotal. Works fine.
6. Use Buffer.BlockCopy to copy contents of array2 to arrayTotal. Works fine.
7. Use Buffer.BlockCopy to copy contents of array3 to arrayTotal. Fails.
I tried using Array.Copy instead of Buffer.BlockCopy with identical results. The only solution I have found so far is:
4. Create a byte arrayTotal1 with length equal to array1.length + array2.length.
5. Use Buffer.BlockCopy / Array.Copy to copy array1 to arrayTotal1.
6. Use Buffer.BlockCopy / Array.Copy to copy array2 to arrayTotal1.
7. Create a byte arrayTotal2 with length equal to arrayTotal1.length + array3.length.
8. Use Buffer.BlockCopy / Array.Copy to copy arrayTotal1 to arrayTotal2.
9. Use Buffer.BlockCopy / Array.Copy to copy array3 to arrayTotal2.
Can anyone shed light on why this is happening and what is the solution to it?
[Note: There is no mistake in the offsets. With just two arrays the code works perfectly. With three arrays, the copy works perfectly till I copy the third array. I have verified this through my functional tests as well as going through random elements in the source and destination arrays in the debugger.]
.
Re: Array Copy / Concatenation Problem
It could stand some cleaning up but this worked fine for me:
csharp Code:
byte[] data;
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fileName1 = "file name 1 here";
var fileName2 = "file name 2 here";
var fileName3 = "file name 3 here";
using (var file1 = new FileStream(Path.Combine(folderPath, fileName1), FileMode.Open))
using (var file2 = new FileStream(Path.Combine(folderPath, fileName2), FileMode.Open))
using (var file3 = new FileStream(Path.Combine(folderPath, fileName3), FileMode.Open))
{
var length1 = file1.Length;
var length2 = file2.Length;
var length3 = file3.Length;
var offset = 0;
int bytesRead;
var blockSize = 4096;
data = new byte[length1 + length2 + length3];
bytesRead = file1.Read(data, offset, blockSize);
while (bytesRead > 0)
{
offset += bytesRead;
bytesRead = file1.Read(data, offset, blockSize);
}
bytesRead = file2.Read(data, offset, blockSize);
while (bytesRead > 0)
{
offset += bytesRead;
bytesRead = file2.Read(data, offset, blockSize);
}
var count = length3;
if (count < blockSize)
{
blockSize = Convert.ToInt32(count);
}
bytesRead = file3.Read(data, offset, blockSize);
while (bytesRead > 0)
{
offset += bytesRead;
count -= bytesRead;
if (count < blockSize)
{
blockSize = Convert.ToInt32(count);
}
bytesRead = file3.Read(data, offset, blockSize);
}
}
Re: Array Copy / Concatenation Problem
That's a good example, though for some reason I won't be able to apply it directly to my project. However my original question still remains unanswered: Does array.copy or buffer.blockcopy NOT work with three arrays at the same time?
.
Re: Array Copy / Concatenation Problem
It works fine. I use it on approximately 100 arrays in one "sweep" in one of my projects.
What's the error message you are getting? That'll help us narrow the problem down.
(And I use Array.Copy for the record)
Re: Array Copy / Concatenation Problem
Quote:
Originally Posted by
jmcilhinney
...
csharp Code:
bytesRead = file1.Read(data, offset, blockSize);
while (bytesRead > 0)
{
offset += bytesRead;
bytesRead = file1.Read(data, offset, blockSize);
}
You read and then you loop to read. I just wanted to point out a do...while loop would have been more efficient (code-wise - not execution wise - it would be the same in that regard)
csharp Code:
do
{
bytesRead = file1.Read(data, offset, blockSize);
offset += bytesRead;
}while (bytesRead > 0);
Re: Array Copy / Concatenation Problem
Quote:
Originally Posted by
Lord_Rat
It works fine. I use it on approximately 100 arrays in one "sweep" in one of my projects.
What's the error message you are getting? That'll help us narrow the problem down.
(And I use Array.Copy for the record)
No errors, but the final array is all messed up, i.e. it does not correspond to the elements of array1, array2 and array3.
.
Re: Array Copy / Concatenation Problem
You are apparently doing something wrong but you haven't actually shown us yet what you're doing. You've provided a description but that's not going to do it. Show us the actual code and might be able to see the actual mistake.
Re: Array Copy / Concatenation Problem
Array.Copy() does very much what it says on the tin, i.e. it copies a specified block of elements from one array to a specified offset of another. If something is not working then I suspect the error is with your code and not with the copying itself. Perhaps you could show us your code.
edit: a case of not refreshing the page for an hour, repeat of #7
Re: Array Copy / Concatenation Problem
Quote:
Originally Posted by
jmcilhinney
You are apparently doing something wrong but you haven't actually shown us yet what you're doing. You've provided a description but that's not going to do it. Show us the actual code and might be able to see the actual mistake.
I am not at my office pooter, so I shall try and post the relevant code here. Pardon me for any syntax goof ups.
Code:
byte[] arr1 = <read a file into a byte array>
byte[] arr2 = <read a file into a byte array>
byte[] arr3 = <read a file into a byte array>
byte[] arrFinal = new byte[arr1.length + arr2.length + arr3.length];
Array.Copy(arr1, 0, arrFinal, 0, arr1.length); //Copy all contents of arr1 from offset zero to arrFinal at offset 0
Array.Copy(arr2, 0, arrFinal, arr1.length, arr2.length); //Copy all contents of arr2 from offset zero to arrFinal at offset <arr1.length>
Array.Copy(arr3, 0, arrFinal, arr2.length, arr3.length); //Copy all contents of arr3 from offset zero to arrFinal at offset <arr2.length>
This is the code I remember. I think I see the problem. When copying the third array, the offset of the arrFinal should actually be arr1.length + arr2.length. Since I am only using arr2.length there, that's why the code is not working.
Sorry folks to have posted a stupid error like this :( I need to cut down on the late sittings in the office :o
.
Re: [RESOLVED] Array Copy / Concatenation Problem
This should illustrate why you need to post the relevant code. When you describe something you describe what you think is happening rather than what is actually happening. This:
Code:
Array.Copy(arr3, 0, arrFinal, arr2.length, arr3.length);
should be this:
Code:
Array.Copy(arr3, 0, arrFinal, arr1.length + arr2.length, arr3.length);