Files downloaded from an applet are getting corrupted?

I’m trying to make an applet loader for my game. Once downloaded, the 3 .jar files it needs are the correct size, but they don’t work. I opened them with a hex editor and found that some bytes are being changed to other bytes, seemingly at random. However, it isn’t random because if I delete the files and have them download again, the same incorrect bytes are in the same places! It’s really weird.

Here is the code.


                                    URL url = new URL(urlString);
                                    URLConnection conn = url.openConnection();
                                    int len = conn.getContentLength();
                                    downloadStream = new ProgressBarInputStream(conn.getInputStream(), updatePanel.progressBar, len);
                                    String filePath = gamePath + "/" + urlString.replace("http://mydomain.net/game/", "");
                                    FileOutputStream out = new FileOutputStream(filePath);
                                    int bytes = 0;
                                    while (bytes < len) {
                                        out.write(downloadStream.read());
                                        bytes++;
                                    }
                                    out.close();
                                    downloadStream.close();

For one thing, it is best to buffer downloaded data when transferring data across the interwebz.


int buffer[] = new int[8192]; //buffer data 8KB at a time (or any other buffer rate you like)
while(downloadStream.read(buffer) > 0)
    out.write(buffer);

Other than that, your code looks fine to me.