Note that there are some explanatory texts on larger screens.

plurals
  1. POJava downloading files sometimes result in CRC
    primarykey
    data
    text
    <p>I've written code to automatically download a batch of files using an InputStream and a FileOutputStream. The code is very straightforward:</p> <pre><code>is = urlConn.getInputStream(); fos = new FileOutputStream(outputFile); eventBus.fireEvent(this, new DownloadStartedEvent(item)); int read; byte[] buffer = new byte[2048]; while ((read = is.read(buffer)) != -1) { fos.write(buffer, 0, read); } eventBus.fireEvent(this, new DownloadCompletedEvent(item)); </code></pre> <p>At first sight this works very well, files get downloaded without any problems, however, occasionally while trying to extract a batch of downloaded rar files, extraction fails with one of the rar parts having a CRC error.</p> <p>As this happened a few times already, although not consistently, I started to suspect that something in this code is not correct/optimal. It will be helpful to know that there are 4 downloads executing concurrently using the JDK FixedThreadPool mechanism:</p> <pre><code>execService.execute(new Runnable() { @Override public void run() { if (item.getState().equals(DownloadCandidateState.WAITING)) { Downloader downloader = new Downloader(eventBus); downloader.download(item, item.getName()); } } }); </code></pre> <p>But because every download thread uses a new instance of the Downloader class, I believe this problem is not a side effect of concurrency? Any ideas if this occasional CRC error has to do with the code or if it has to do with something else?</p> <p><strong><em>UPDATE</em></strong></p> <p>I can verify that the file size of a problematic file is correct. I also did a diff (on linux) on the automatically downloaded file and the manually downloaded file. The filesize is the exact same for both files, however, diff says that the binary content differs between the 2 files:</p> <p>Binary files file.rar and file(2).rar differ</p> <p><strong><em>UPDATE 2</em></strong></p> <p>I used a visual binary diff tool and could see that a sequence of 128 bytes was different, somewhere in the middle of the file. I don't understand how that could happen, as the file being downloaded doesn't change and it is being read byte per byte using an input stream. Any ideas??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload