• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference in the size of file downloaded using BufferedReader and browser  RSS feed

 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I find a small difference in the size of files downloaded using BufferedReader and using browser.

For example, I am using the following snippet


and the icon of the exe file also changes when i download it using java;It shows up the normal exe icon in case of java whereas if I download it using browser i could find the vendor provided icon for that exe. Is there any matured way of downloading files from internet using java than these buffered readers that doesnt suffer from such problems.

Thanks in advance,
Muthu
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use BufferedReader, or any other Reader, for non-character data. A .exe is clearly binary and should therefore be handled with InputStream and OutputStream only.

Readers convert bytes to characters, sometimes taking a few bytes together to form one character. That really messes up binary files.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Prime,

Now I have changed as follows


But when i click on the file it says "not a valid Win32 application'. Can you please help. Now I have the icon in place.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't use a Writer, either, because that causes the same problem as the Reader. Use a BufferedOutputStream instead, like you did before.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that doesnt seem to work again.. here is my code

Is there anything I should change..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try flushing the BOS before closing it.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Ulf Dittmer,

Flushing the outputstream also didnt help me..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed that you're still using a Reader for the input stream - as Rob said, that's not going to work. For binary data you MUST use one of the Stream classes.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I am sorry for that.. Can you please give me a sample usage..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of how to read bytes from an InputStream: http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot people

It helped me and I am able to download successfully It was a good experience for me..

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!