Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading data from url and then outputting as string?

 
Jason Mill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am opening a connection to a URL and I want to send the data read onto a browser. In this case I am reading a pdf file. It looks like the null characters in the pdf file are not being sent or being changed somehow and so subsequently the pdf file is empty. It works great for html/text files. Im using DataInputStream with read to get the url data and DataOuputStream.writeBytes to output the data. I have tried many other combinations. (BufferedInputStream, BufferedReader, OutputStreamWriter)
Am I missing something - how do I stop the file being corrupted, Can this be done?
Any help greatly appreciated.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PDF file doesn't contain a String, and trying to treat it as one is going to cause problems when Java converts to/from the internal Unicode representation of character data. While this will work OK for textual data like HTML, it will totally destroy a PDF file.
Don't wrap the InputStream you get from the URLConnection with anything -- just call read(byte[]) on it directly, and write the data using FileOutputStream.write(byte[]). This will result in an exact copy of what you read from the URL being put into your file, and your problem should go away.
The classic implementation would look something like this:
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know exactly what you're trying to do, but why not just send the URL itself to the browser and let the browser handle it? You can do it easily with the BrowserLauncher class from http://browserlauncher.sourceforge.net
Brian
[ October 16, 2003: Message edited by: Brian Pipa ]
 
Jason Mill
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest, that pointed me in the right driection. I'm in the process of converting from using strings.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic