Re: How to fetch HTML documents from Sockets

I am not very good with English but let me rephrase my question.
I am suppose to retrieve the content of a file in a server
the file could be a html file, a text file or an image file which i do not know.
My above code is able to fetch the contents of html and text file no problem.

but I am not able to get an image. I know the codes of retrieving an image but that would mean that I could not get if the file is html or text file

So i was wondering is there any way I could know if the file is text/html/image before I fetched it?
I try reading them all as bytes b[] but for image I would still have to convert into image but I do not need to do that for text file and html.

Re: How to fetch HTML documents from Sockets

yes you can't use a reader to fetch binary data, you need to use a variation of a regular InputStream. The reader will try to interpret the data as text, but an image is not text.

And this is the moment where you figure out that programming is hard. But lucky for you, there is a way out. See you can't read an binary file as text (characters in stead of bytes)... but you can read a text file as binary (bytes in stead of characters). So don't use a Reader for your data, use an InputStream for all files you fetch.

Re: How to fetch HTML documents from Sockets

yup.. I close all the streams after writing.. in addition, the output file contains all the http response code which I don't want. but i could not omit them as i am reading everything as bytes, I do not know the place where the http response code will end, if I read as strings, at least I can know where it ends and start writing just the content to the output file. This is driving me crazy..