• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing an Image Into a Local File from a Socket Stream Without Using HTTP-related Classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

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.

Here's my code:

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult for “beginning”. Moving discussion.

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.
 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is where I ought to say, “Don't know.”

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?

Anybody else?
 
John Jeffersonian
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've managed to crack this puzzle! I'm not sure if this is the optimal solution, but it works.

 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done

Don't call close() on your streams. Use try‑with‑resources (Java7+) instead.
What does .*/ mean in a regular expression?
reply
    Bookmark Topic Watch Topic
  • New Topic