Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmented C# file downloader

Have been trying to write a program in C# that downloads a file using multiple segments at once like most download managers, and I've run into the issue that the files downloaded are corrupted. E.g., I download a video and it plays for 2 seconds then WMP says it's unplayable.

I hexedited the downloaded file and it appears there are sections of zerobytes scattered throughout the file, anyone have any ideas why? VS reports no errors.

getPart() is called for each segment in a separate thread.

public long start;
public long end;
public int thread;
public Form1 handle;
public myFile handler;
public void getPart()
{
    log("getting part " + start.ToString() + "," + end.ToString());
    HttpWebRequest part = (HttpWebRequest)WebRequest.Create(handler.url);
    part.AddRange((int)start,(int) end);
    HttpWebResponse pr = (HttpWebResponse)part.GetResponse();
    Stream rstream = pr.GetResponseStream();
    log("Beginning part " + start.ToString());
    int totalbytes = 0;
    byte[] buffer = new byte[256];
    int x = rstream.Read(buffer, 0, 256);
    while (x > 0)
    {
        handler.writeFile(buffer, (int)(totalbytes + start), x);
        totalbytes += x;
        x = rstream.Read(buffer, 0, 256);
    }
    log(start.ToString() + "-byte start part done...");
    rstream.Close();
    pr.Close();
    handler.partDone(thread);
    return;
}

public void writeFile(byte[] buffer, int start, int size)
{
    mFileStream.Seek(start, SeekOrigin.Begin);
    mFileStream.Write(buffer, 0, size);
    return;
}
like image 696
James Avatar asked Dec 02 '09 07:12

James


People also ask

What is a segmented sieve?

Segmented Sieve (Print Primes in a Range) Prime Factorization using Sieve O(log n) for multiple queries. Efficient program to print all prime factors of a given number. Prime Factor. Pollard's Rho Algorithm for Prime Factorization.

What is the time complexity of segmented sieve?

Time complexity : O(N * log(logN)) Therefore, time complexity of segmented sieve = time complexity of simpleSieve = O(N * log(logN)).

Is simple sieve cache friendly?

This is not very cache friendly.

What is simple sieve algorithm?

The sieve of Eratosthenes algorithm is an ancient algorithm that is used to find all the prime numbers less than given number T. It can be done using O(n*log(log(n))) operations. Using this algorithm we can eliminate all the numbers which are not prime and those that are less than given T.


1 Answers

Well I've figured it out, just thought I'd leave an answer for anyone having similar issues. A lock is required around the file writing stream.

public void writeFile(byte[] buffer, int start, int size)
    {
        mFileStream.Seek(start, SeekOrigin.Begin);
        mFileStream.Write(buffer, 0, size);
        return;
    }

becomes

public void writeFile(byte[] buffer, int start, int size)
    {
        lock (mFileStream)
        {
        mFileStream.Seek(start, SeekOrigin.Begin);
        mFileStream.Write(buffer, 0, size);
        return;
        }
    }
like image 108
James Avatar answered Sep 21 '22 02:09

James