Joehot200
Joehot200

Reputation: 1090

Downloading long files freezes the computer

I have made a launcher for my game Privateers. It works perfectly, downloads everything needed - But, for some reason, it freezes the entire computer!

The freeze usually occurs when doing something, if I afk at my computer until the download completes, then nothing happens. However, when I tested it on my mothers computer playing the game "World Of Tanks", the computer froze almost immediately. If I play a game then the launcher also has a tendency to freeze my computer.

I use windows 8, my mother uses windows 7.

On my own computer, when this happens I am able to move the mouse very slowly (between 30 second to 2 minute delay), alt+tab won't work, control+alt+delete will work (but when opening task manager the task manager does not appear). On my mothers' computer it is basically the same except that EVERYTHING is frozen 100% except for the mouse which is working fine.

It only happens when downloading large (5MB+) files. When my launcher downloads smaller files there is no issue.

I use the following code to download files:

void download(String source, String destination, int size) {

    File ofile = new File(System.getProperty("user.dir") + "", destination);
    System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
    try {
        if (ofile.exists()) ofile.delete();
        if (!ofile.createNewFile()) {
            throw new IOException("Can't create " + ofile.getAbsolutePath());
        }

        int inChar = 0;
        URL url = new URL(source);
        InputStream input = url.openStream();
        FileOutputStream fos = new FileOutputStream(ofile);
        for (int i = 0; i < size && inChar != -1; i++) {
            int percentage = (int) ((i * 100.0f) / size);

            progressBar.setValue(((int) ((percentage * 100.0f) / 100)));
            fr.setTitle(ofile.getName() + ": " + progressBar.getValue() + "%" + " Total: " + oprogressBar.getValue() + "%");
            inChar = input.read();
            fos.write(inChar);
        }

        input.close();
        fos.close();
        System.out.println("Downloaded " + ofile.getAbsolutePath());
    } catch (EOFException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I have been unable to find a duplicate of this happening when searching on the internet. Any help is appreciated.

Upvotes: 1

Views: 125

Answers (2)

bmargulies
bmargulies

Reputation: 100152

Buffer the input stream, or read more than one character at a time, or both.

Upvotes: 1

SteBra
SteBra

Reputation: 4247

Maybe multithreading will help you out over here.

See more about it in this post

Upvotes: 3

Related Questions