File Download results with corrupted file. End of File problem

Hello
I am tring to make a download program. I am tring to download an exe file from internet. Lets say the file in this website
http://www.oldversion.com/download/winrar154.exe
The part that helps me downloading is here, which is encapsulated with a "while" statement at an upper layer. So that code is a loop
                             byte buffer[];
                             if (size - downloaded > MAX_BUFFER_SIZE) {
                                 buffer = new byte[MAX_BUFFER_SIZE];
                             } else {
                                 buffer = new byte[size - downloaded];
                             // Read from server into buffer.
                             int read = inFromServer.read(buffer);
                             if (read == -1)
                                 break;
                             // Writing buffer to file below that partBut when there is no bytes left to download, it doesn't understand that end of file reached. The inFromServer.read method must return -1 whenever it reaches to an end of file, but in my code it continues looping within my "while" statement because it doesn't understand end of file has came, read becames 0 after there is no bytes left to download. It doesn't see that end of file is reached. And when download ends, the exe that I made is not working. By the way since this is my homework I am restricted to use many java classes like starts with http or url, so you don't need to suggest them. Here is my debug results for my integer read in the code: 1024 1024 1024 ....................... 512 0 0 0 0 0 ......, So till the end of the file it continues as 1024 which is my default buffer size, and at the end it continues as 0.
I would be happy if you help me.

There's a terrible amount of rubbish in this thread.
while(myStream.read(buffer) != -1){ //this will give the pretest you need for the eof.
//your stuff.
} This is rubbish because the return value isn't stored, so you don't know how much data was returned if EOS didn't occur.
If you're getting reads of length 0... you must have a bug in your code. read() methods do not return zero except via NIO in non-blocking mode.
if you get 10 0's in a rowYou can't even get one 0 in a row, see above.
int read = inFromServer.read(buffer);
if (read == 0)And again. This cannot occur.
The correct way to download a file, or indeed copy any input stream, is as follows:
int count;
byte[] buffe r= new byte[8192]; // or more
while ((count = in.read(buffer)) > 0)
  out.write(buffer, 0, count);
out.close();
in.close();

Similar Messages

  • I am having trouble downloading ios5 with the error "there was a problem downloading the software.The network connection timed out. Please help!!!

    Please can someone help me with my error message. I cannot download the new software IOS 5.0.1. My phone is double slow now..

    Disable your firewall and antivirus software while you do the download and update. (Guessing you are using a Windows PC)

  • Very slow downloading webpages with Firefox 3.6.8; no problem with IE;

    Firefox is very slow loading web pages. Was OK with the old version. This happened after I downloaded latest upgraded version 3.6.8
    == This happened ==
    Every time Firefox opened
    == after loading new version of Firefox, 3.6.8

    Not only do I have the snail version of firefox but I keep getting messages that Firefox can't find the server, or that it has times out. I have had to retry numerous times. I just had a problem with FF opening a one-page pdf. After what seemed like forever, I copied and pasted the url into IE and it opened almost immediately. After I read the pdf and downloaded it, I closed IE and saw that FF was still in the process of opening the page. I loved FF,but don't have time to wait until my next birthday to view/open a site. Incidentally, my birthdate is 07/11. This is seriously hampering my participation in two online graduate courses. I have been running Win 7 since March, but not had these problems until recent updates. I even had firefox set to notify me and ask before installing updates, but it was changed to automatically install updates when I just checked. I am the only user of this computer (live alone).
    ''' ALSO''', when I checked update history, there is an update for (3.6.4) that shows as successful that took place on '''Wednesday December 31, 1969.''' I don't believe that Firefox existed in 1969 or Internet Explorer for that matter. I couldn't highlight and copy the info in my history, but did copy with pen and paper. This is what was showing for the 3.6.4 update:
    '''FireFox 3.6.4 (20100611143157)'''
    Security Update
    '''Installed''' on '''Wednesday, December 31, 1969''' 6:00:00 PM
    Status The update was successfully installed
    There is definitely something amiss unless the creators of FireFox have mastered time travel!
    Since I do not have the time to open FireFox and check here to see if a solution to these problems has been determined (based on s-l-o-w performance of FF over the past week or so) would someone email me with a link that I can access through another browser, if necessary?
    Email: [email protected]

  • And here we go again with corrupt download files unable to expand

    There seems to be a pattern with major updates from Apple and the ability of the servers providing the content - be it iTunes or iOS or Mac OS updates - I have been unale to download sucessfully the larger updates but smaller (22MB for AirPOrt Utility) work fine.
    The annoying thing is that in almost all cases the erorr dosen't occur until AFTER the download is complete - meaning it takes up allt he time and bandwidth etc with no result and you have to start all over again.
    This has happened to enoguh people and with enough different software releases that I posit that it is NOT the result of any partiucalr configuration of netwokring and or hardware and or firmware and or OS version etc - but that the it is a systemic issue on the provider side of things.
    Perhaps they (Apple's content providers) should stagger access or something - by time zone - or Apple should release updates over the course of a week - not all on the same day.
    I would much rather get a message that the server is busy try agian later - or have one update a day - and have it work on the first try - that to waste time retrying the same downloads over and over again.

    While it is entirely possible taht some combination of DOCIS or firmware etc on the user end is a contirbuting facotr - it is not hard to find numerous reports of exactly what I am talking about along with other similar issues.
    I don't recall having this issue before I upgraded to 50Mbps download service - so there could be a connection there. But the "solution" in my case so far has been to just simply keep trying and eventually it works just fine.
    I suppose I may have forggotten as they are so few and far between - but in my recollection - the ONLY issues I have had have been with software from Apple - whether downloaded via software update - or the App store - or iTunes - and no where else - on both Mac and Windows (had Apple Software Update fail to get iTunes update a couple times on Windows, but windows is easy to goof up if you have other installers running at the same time).
    On the other hand I don't recall too many cases in the past where the downloads were so large as they are nowadays - and certainly the number of users after those downloads has only increased over time.
    https://discussions.apple.com/thread/4010441?start=0&tstart=0
    http://support.apple.com/kb/TS1813?viewlocale=en_US&locale=en_US
    http://macmost.com/forum-software-updates-corrupted.html
    https://discussions.apple.com/thread/3356863?start=15&tstart=0
    http://reviews.cnet.com/8301-13727_7-57357319-263/dealing-with-corrupted-downloa ded-files-in-os-x/
    http://forums.macrumors.com/showthread.php?t=1268440
    http://forums.untangle.com/networking/8171-downloads-corrupt.html
    http://forums.macrumors.com/showthread.php?t=970911
    https://discussions.apple.com/thread/3378898?start=0&tstart=0
    http://mybroadband.co.za/vb/showthread.php/287165-iOS-4-2-download-corrupted
    http://stackoverflow.com/questions/7199094/what-is-the-cause-of-frequent-corrupt ed-incomplete-download-from-amazon-cdn-to-i
    http://forums.macrumors.com/showthread.php?t=1292841
    http://www.jailbreakqa.com/questions/39174/is-ios-download-corrupt
    http://www.tuaw.com/2011/11/10/ios-5-0-1-now-available-for-download/
    I could go on but you get the point.

  • Major problem with Photoshop unexpected end of file...

    Every time I save a file in Photoshop, I have to save it twice per format (psd, jpg, etc...).  That's because if I've ever saved the file before, I get an unexpected end of file error and the file disappears until I do a save as.  I downloaded the latest update today and it claimed to have fixed major errors with Photoshop.  I wonder what major errors exactly were fixed; and why not this one?  As far as errors go, it's pretty effing major.  It adds significant time to all of my projects and frankly, if I'm spending exorbitant amounts of money for "industry-leading" software, I expect to be able to save my work painlessly.  Oh well, I guess it'll only be another couple months before I have to shell out another $1200 bucks for the CS6 right?  Maybe then I'll be able to actually save my work.
    p.s. CS2/3/4 did not have this issue - sweet upgrade Adobe, thanks.

    Again, this is not a known issue.   You are the first person to bring this up without a bad disk drive, and it is probably due to something on your file server.
    Yes, other people get the same error message - because they are dealing with corrupt files for one reason or another.  You are getting the message because the files are corrupt, probably because of an issue with your file server.
    Photoshop CS4 and earlier used the Carbon framework on MacOS.  Photoshop CS5 is written to the Cocoa framework/APIs and uses different OS code paths for many things.  We have seen many cases where the Cocoa APIs had problems that the Carbon APIs did not.  Unfortunately, if we cannot work around those API issues, then they are up to Apple to fix.
    But in this case, I doubt it is an OS API bug as such.  I suspect it is a change in the way the OS APIs interact with the file server.
    And cases like this are high up on the list of why we say NOT to open or save from a file server -- you can get a lot of odd, difficult to diagnose errors because of problems on the server or network.  Those problems have nothing to do with the application, but may appear to because of the complexity of file servers and networking.
    Some Photoshop users do open and save off a file server all the time - without problems.  When we test with file servers here inside Adobe, we rarely have problems (and the rare problem was due to a bad disk on the server).  But we can't guarantee that everyone's network and everyone's file server is going to work perfectly - and evidence suggests that many of them don't work perfectly.
    In short: you probably have a bad file server.  It is highly unlikely that this is a bug in Photoshop.

  • I am trying to download lion with the thumb drive how do i back up my files is time machine good enough or must i transfer everything to my hard drive

    I am trying to download lion with the thumb drive how do i back up my files is time machine good enough or must i transfer everything to my hard drive

    A erase of the drive or boot partition is not always necessary.
    Flashing question mark at boot could be a easy fix, sometimes it's the firmware that simply forgot what bootable volume to boot from.
    Try holding the Option key down while booting the machine, a choice of bootable options appears, select your OS X and boot up.
    When you get in, head to System Preferences > Startup disk and set it again new. This will tell the firmware what to boot from. Test it to see.
    Now if you don't have a selection of bootable options, it could be that the drive is dead, or OS X is erased or corrupted so it can't boot.
    You'll have to run through this list of fixes to see what's going on, if you need a hardware fix or what, I've also included links if you can't fix it and need to recover your data etc.
    (If it's not remmebering your boot selection then also run through the list to reset things.)
    Step by Step to fix your Mac

  • When I download something with Firefox, it show the progress but when it's finished, the file that i downloaded dissapears.

    While downloading something with Firefox, i can see the file in the downloads folder while it's downloading but as when it's complete, the file dissapears.

    It is possible that your anti-virus software is corrupting the downloaded files or otherwise interfering with downloading files by Firefox.
    Try to disable the real-time (live) scanning of files in your anti-virus software temporarily to see if that makes downloading work.
    See:
    * http://kb.mozillazine.org/Unable_to_save_or_download_files

  • How to open a file with a .Ink ending?

    I installed Dropbox (from Dropbox.com) and my files transfered from a windows computer were labeled with the .INK ending> was not able to open them on the mac... any ideas as to how or why?

    ascala wrote:
    I installed Dropbox (from Dropbox.com) and my files transfered from a windows computer were labeled with the .INK ending> was not able to open them on the mac... any ideas as to how or why?
    Your subject line refers to a .lnk file extension and the body of your post refers to a .INK file extension. Which is it? If it is .lnk then it is a link to a file, not a file. In Windows a file name ending with .lnk is a shortcut link to the file. You likely copied over the shortcut rather than the file itself. Therefor you can't open it because it is not there on your Mac.
    Message was edited by: BobTheFisherman

  • I dont have a internet connection at home, i connect my iphone 5 to my computer, it says i need new version of ITunes 11.2, i cant download it with out internet, i am at Library can i get ITunes 11.2 in a zipped files or exe file downloaded to my usb driv

    i dont have a internet connection at home, i connect my iphone 5 to my computer, it says i need new version of ITunes 11.2, i cant download it with out internet, i am at Library can i get ITunes 11.2 in a zipped files or exe file downloaded to my usb driv

    i can take pictures off my phone but thats it, i have some nice programs for pictures and art work and cant get them back on my phone to send them to friends and family. Also have some OLD pictures of Family on this Computer that was given to me i want to get off of it and send out but i can dragg and dropp from phone to computer but i cant drag and drop onto phone from computer and there the same jpg files, sad. i like my iphone5 and want another one but i might have to change when it time to upgrade , i dont want to, but i cant afford internet let alone a mac or pc, this one was given to me, was lucky to have saved to get the phone and service on a family plan, i have unlimited internet on phone that is my life source for internet, wish io could afford both phone and internet, its phone or internet, and with phone i have both in a way

  • How to get the file name from downloads folder with the applescript??

    Hi All..
    I am downloading an application to downloads folder, I need to install the same application after downloading programatically.
    So how do I get the filename in runtime while downloading or after downloading from downloads folder with Applescript or Java?
    Also my download URL link doesnot contain full name of the download file.
    Pls suggest.

    Hi,
    The best method is to use curl.
    Other methods are not reliable, but can work, it depends on several factors (what the script does, what the user does).
    Here is an example with the curl command:
    set tUrl to "http://sourceforge.net/projects/vlc/files/2.0.4/macosx/vlc-2.0.4.dmg/download?ac cel_key=60%3A1350656196%3Ahttp%253A//www.videolan.org/vlc/download-macosx.html%3 A7233e4e9%245818d66f39b0576e1e9c4a0f3b46b1991180bfdd&click_id=976a88ee-19f7-11e2 -9a21-0200ac1d1d8e&source=accel"
    set dFolder to path to downloads folder
    tell application "Finder" to set newFolder to (make new folder at dFolder) as string
    set f to quoted form of POSIX path of newFolder
    do shell script "cd " & f & " && curl -L " & tUrl & " -o 'vlc.dmg'" -- download  to file "vlc.dmg"
    set downloadedFile to newFolder & "vlc.dmg" --<-- HFS path
    --do something with the downloadedFile

  • I downloaded Premiere with no trouble, but can't find a setup or install file. What's the deal?

    I purchased a one year membership a few months ago, and installed Lightroom and Photoshop without any troubles.  I now need Premiere for my new classes, so I downloaded it with no trouble.  However, I can't find an install or setup file.  What's the deal?  I've paid for software that I can't install, and I need it for class which started about a week ago.  Help!

    Cloud Getting Started https://helpx.adobe.com/creative-cloud.html
    -Install, update or UNinstall, and launch after installing

  • When going through the download process with social monkee and getting to the point of adding it to firefox I get the following message: 'Social Monkee could not be installed because firefox cannot modify the needed file'. How can this be fixed?

    When going through the download process with social monkee and getting to the point of adding it to firefox I get the following message: 'Social Monkee could not be installed because firefox cannot modify the needed file'. How can this be fixed?

    That is usually caused by a missing unpack directive (&lt;em:unpack&gt;true&lt;/em:unpack&gt;) in the file install.rdf of that extension.
    See https://developer.mozilla.org/En/Updating_extensions_for_Firefox_4.0#XPI_unpacking
    -----

  • I cannot download videos with flash video downloader from other websites also.Whenever I want to download video it says choose file.

    I cannot download videos with flash video downloader from other websites also.Whenever I want to download video it says choose file.

    I don't know that any Flash downloader extensions work in Firefox for Android. You would be best off asking for support from the extension authors at https://fvdmedia.userecho.com/list/21650-firefox-extensions/?category=4944

  • Does Adobe Send allow you to select how many days your files can be downloaded - as with Adobe Send Now? Also, why don't I see the virus scanning message any longer with "Send" versus "Send Now"?

    Does Adobe Send allow you to select how many days your files can be downloaded - as with Adobe Send Now? Also, why don't I see the virus scanning message any longer with "Send" versus "Send Now"?

    We relied upon the "days available for download" to get people to take
    action more quickly. The "virus scanning" progress bar was simply another
    way of knowing this activity was actually taking place - unless this is
    written somewhere in the Adobe Send User Guide.....wherever that is.
    On Tue, Dec 2, 2014 at 12:09 PM, bob.treitman <[email protected]>

  • Incorrect file name while downloading it with JDK1.4

    Hello people!
    I'm facing a strange problem in my downloading servlet with JDK1.4 and Tomcat4.1 when trying to set Cyrillic file name: download window pops up with some abracadabra name. After I click "Open" twice the file opens correctly. It never happens with JDK1.3.
    Some backward compatibility feature from SUN???
    Here is the code:
    res.setContentType("application/octet-stream; charset=windows-1251");
    res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    I'm almost sure it's a bug in Tomcat 4.1. All you have to do is to switch from 4.1 to 4.0 and the problem is fixed. We also checked what line goes to the response header - it's correct.
    Did I mention this problem happens even with Latin charactes? The file name is correct but download box opens twice.

Maybe you are looking for