Obviously, since I can't use an HTTP-related class like any sane programmer would do, this is an assignment.
So I'm given a URL that directly links to an image. My task is to download that image to a local file. I've written a class for it that does the job of separating the HTTP header from the rest of the stream, and writes the file. I've compared normally-downloaded image file with the one I've downloaded using this class, and size-wise they have a very small difference. For some reason, however, they are not identical and the one that I download through Java does not open. So far I wasn't able to figure out the problem.
The biggest problem is that I need to separate the header from the rest of the response, and outside of using a BufferedReader I see no other way of doing it without having to write temporary files and removing the header manually.
All of the StackOverflow answers to similiar questions only provide either methods that use HTTP-related classes (easy as pie) or methods that read the entire byte stream and don't separate the header.
I think you will find the Java Tutorials similarly of no help because they will use HTML related classes too. Start by seeing what you can read from the socket, and print it to the command line/terminal.
I've already done that. The code that I've provided in my OP perfectly separates the header from the rest of the stream. I think the problem lies somewhere in the fact that the BufferedReader is reading into a String, and then in turn that String gets turned into a byte array to be fed to the FileOutputStream. I have a feeling something happens in that conversion of Byte -> String - > Byte that messes up the image. The problem is that if I use a normal reader instead of a buffered reader I have no way to separate the header, since normal readers only read byte-by-byte.
So your file consists partially of text (the header) and partially of binary data (the image).
A file reader or similar reads char by char (I think) and returns them as ints. A buffered reader converts a series of such ints into a String (probably converting them back to chars in the process).
A data stream can read your binary data.
Is there any way you can read the header, close the buffered reader, work out an offset from the header's length, and start again with an input stream to read the binary data?