When downloading a rar file from the internet with the code below, the downloaded file is larger than it actually is.
Not sure what causes this?
bis = new BufferedInputStream(urlConn.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(outputFile));
eventBus.fireEvent(this, new DownloadStartedEvent(item));
int read;
byte[] buffer = new byte[2048];
while ((read = bis.read(buffer)) != -1) {
bos.write(buffer);
}
eventBus.fireEvent(this, new DownloadCompletedEvent(item));
You are writing a full buffer to the output with every write, even if the
read(byte[])operation didn’t completely fill it.Also, since you are reading into a
byte[]already, the buffered streams are just counter-productive overhead. Use buffered streams with the single-byteread()andwrite()methods.Here is a better pattern to follow.