Results 1 to 10 of 10

Thread: [RESOLVED] Array Copy / Concatenation Problem

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Resolved [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.]

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array Copy / Concatenation Problem

    It could stand some cleaning up but this worked fine for me:
    csharp Code:
    1. byte[] data;
    2.  
    3. var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    4. var fileName1 = "file name 1 here";
    5. var fileName2 = "file name 2 here";
    6. var fileName3 = "file name 3 here";
    7.  
    8. using (var file1 = new FileStream(Path.Combine(folderPath, fileName1), FileMode.Open))
    9. using (var file2 = new FileStream(Path.Combine(folderPath, fileName2), FileMode.Open))
    10. using (var file3 = new FileStream(Path.Combine(folderPath, fileName3), FileMode.Open))
    11. {
    12.     var length1 = file1.Length;
    13.     var length2 = file2.Length;
    14.     var length3 = file3.Length;
    15.     var offset = 0;
    16.     int bytesRead;
    17.     var blockSize = 4096;
    18.  
    19.     data = new byte[length1 + length2 + length3];
    20.  
    21.     bytesRead = file1.Read(data, offset, blockSize);
    22.  
    23.     while (bytesRead > 0)
    24.     {
    25.         offset += bytesRead;
    26.         bytesRead = file1.Read(data, offset, blockSize);
    27.     }
    28.  
    29.     bytesRead = file2.Read(data, offset, blockSize);
    30.  
    31.     while (bytesRead > 0)
    32.     {
    33.         offset += bytesRead;
    34.         bytesRead = file2.Read(data, offset, blockSize);
    35.     }
    36.  
    37.     var count = length3;
    38.  
    39.     if (count < blockSize)
    40.     {
    41.         blockSize = Convert.ToInt32(count);
    42.     }
    43.  
    44.     bytesRead = file3.Read(data, offset, blockSize);
    45.  
    46.     while (bytesRead > 0)
    47.     {
    48.         offset += bytesRead;
    49.         count -= bytesRead;
    50.  
    51.         if (count < blockSize)
    52.         {
    53.             blockSize = Convert.ToInt32(count);
    54.         }
    55.  
    56.         bytesRead = file3.Read(data, offset, blockSize);
    57.     }
    58. }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    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?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    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)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Array Copy / Concatenation Problem

    Quote Originally Posted by jmcilhinney View Post
    ...
    csharp Code:
    1. bytesRead = file1.Read(data, offset, blockSize);
    2.  
    3.     while (bytesRead > 0)
    4.     {
    5.         offset += bytesRead;
    6.         bytesRead = file1.Read(data, offset, blockSize);
    7.     }
    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:
    1. do
    2.     {
    3.         bytesRead = file1.Read(data, offset, blockSize);
    4.         offset += bytesRead;
    5.     }while (bytesRead > 0);
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Array Copy / Concatenation Problem

    Quote Originally Posted by Lord_Rat View Post
    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.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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
    W o t . S i g

  9. #9

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: Array Copy / Concatenation Problem

    Quote Originally Posted by jmcilhinney View Post
    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

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width