tags:

views:

398

answers:

3
private Arraylist split(byte[] filebytes, int range)
{
}

how to split this filebytes by given range i.e if range is 100 it should split the filebytes by 100kb and put this into the arraylist untill the eof. i have .net framework 1.1 only... thanks in advance.

A: 

So, if I understand this correctly, you want and ArrayList of byte[] of size 100000? I can't write the code for you (don't have VS here at the moment), but the most efficient way is to create new array objects and use Array.Copy() to copy the data from your big array into the bunch of smaller arrays.

chris166
+3  A: 

If I understand the question correctly, you want to chop the existing array into arrays of (at most) 100k elements, and put those arrays in the array list?

ArrayList Split(byte[] filebytes, int range)
{
    range *= 1000;
    int pos = 0;
    int remaining;

    ArrayList result = new ArrayList();

    while ((remaining = filebytes.Length - pos) > 0)
    {
        byte[] block = new byte[Math.Min(remaining, range)];

        Array.Copy(filebytes, pos, block, 0, block.Length);
        result.Add(block);

        pos += block.Length;
    }

    return result;
}

Change 1000 to 1024 if that was your intention :)

I can't really think of a reason why you'd want to do something like this though. Loading one huge array of bytes and then splitting into blocks is rather inefficient if you could have split them into 100k blocks while reading your data.


Regarding your comment: sending an entire file to a socket should be much simpler than you're making it to be. I have no way of testing, but this should work in .NET 1.1.

private void SendFileToSocket(string fileName)
{
    Socket socket = new Socket(
        AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    using (socket)
    {
        socket.Connect("server.domain.com", 12345);

        using (NetworkStream networkStream = new NetworkStream(socket))
        using (FileStream fileStream = File.OpenRead(fileName))
        {
            byte[] buffer = new byte[32768];

            while (true)
            {
                int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                    break;

                networkStream.Write(buffer, 0, bytesRead);
            }
        }
    }
}

The buffer size is a little arbitrary, but it should be large enough.

Thorarin
You're trying to copy something into contb, after verifying it's null. You need to allocate the array first. I'm lost ok what you're trying to achieve though. Read, Split, Concatenate, Write? Makes no sense to me.
Thorarin
am doing file transfer prog using socket... all files r very lorge so here i have to split the file and send to the server. server ll Concatenate all bytes and finally write a file this is the goal of this proj.... now hopefully achived it. thanks lot.
RV
Read up on *streams* and check some examples. There is no need to build huge arrays containing the entire file. In fact, that's a very **bad** thing to do when your files are large. MSDN is just one of the possible places to read up: http://msdn.microsoft.com/en-us/library/336wast5%28VS.71%29.aspx
Thorarin
yes Thoarin, when i send the file this is good way. but client side i could't set buffer size more than 25 mb if set morethan that it's giving system lacked sufficient buffer space. in this case how can recive the file without data lose.
RV
A: 

The idea suggested by Thorarin is a good solution. However I would make a small change regarding the declaration of the byte[] block. It is better to place the declaration outside the while. As far as I know that is better for your performance. You won't notice very much (especially not on small versions of the filebytes array), but software like FXCop will warn you about declarations within while/for constructions (at least as far as I know). So the code would look like this:

ArrayList Split(byte[] filebytes, int range)
{
    range *= 1000;
    int pos = 0;
    int remaining;

    ArrayList result = new ArrayList();

    //Placed the declaration of block outside the while. 
    byte[] block = null; 
    while ((remaining = filebytes.Length - pos) > 0)
    {
        block = new byte[Math.Min(remaining, range)];

        Array.Copy(filebytes, pos, block, 0, block.Length);
        result.Add(block);

        pos += block.Length;
    }

    return result;
}

For more details look at: Declare variable inside or outside the [email protected]

Gertjan
Not true. The scope of the declaration has no effect on performance whatsoever. An declaration doesn't really *do* anything aftr all. The reason it's inside the loop is because it's value has no use outside the loop. In some similar cases, you could skip *allocating* a new array, but here we cannot.
Thorarin
Is that true? The variable block is a pointer to the byte[]. By declaring the variable in the while you will create a new pointer which might consume extra memory. I am not completely sure, but that is what I understand from it (the people in the link I poster are also not agreeing with each other). Apart from that: the way and place you declare your variables is a matter of taste and guidelines. Some tend to place all declarations at the beginning of the function.
Gertjan
There is no extra pointer. A declaration just gives the array a name you can use to refer to it. Even if there were an extra pointer, a 4-byte pointer is not significant, *especially* not when you're working with multiple 100kB arrays. Declaring variables at the start of a method is just archaic. You should be programming for clarity, not micro-optimizing something that doesn't need it.
Thorarin
Okay clear. I am also not very fond of placing the items on top of the function (I had to do it for one project because of guideline). But placing this kind of vars outside of the while is something I have always done (not even for performance, when I see a declaration inside a while I want to move it outside). It's just a feeling. But hey, I am glad I learned something today :) Your solution is also good because the var is not available outside the while (and cannot be abused).
Gertjan