This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes InputStreams reading text and bytes from socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "InputStreams reading text and bytes from socket" Watch "InputStreams reading text and bytes from socket" New topic
Author

InputStreams reading text and bytes from socket

Lukasz Jarocki
Greenhorn

Joined: Jan 01, 2013
Posts: 10
Hello,
this is my first post on board as you may noticed.

I have a problem with understanding streams, and how they collaborate with sockets.

I need to implement simple HTTP client in Java. I didnt have any problems with connection, sending requests (GET, DELETE etc.), sending text or reading pure html. But I am stucked on reading bytes from stream.
Assume that my client sending GET to server.


Server response with HTTP header, blank line and html text go on. I am reading that with following code:

It works well.


But imagine that I read server response and I noticed that I need to download external images from server (These days nearly everysite has images). So what I do? Sending GET to server. But what to do next?
I expect that response will contain text (HTTP header) blank line and bytes (image). So how can I read that?

In the end, my question is: how to read from InputStream when I expect that data will be mixed? I know that, servers usually send type of data with Content-type attribute, so its easy to check (by reading HTTP header) what will be send in HTTP body. But I dont know how to mix streams or read mix data.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
If you use the built in java.net.HttpURLConnection rather than directly working with sockets it will be a lot easier. It will automatically handle the headers and leave it up to you how to handle the InputStream as characters or bytes.

Otherwise, I would read bytes and watch for the "blank line" that follows the headers - the crlfcrlf character sequence.

Bill
Lukasz Jarocki
Greenhorn

Joined: Jan 01, 2013
Posts: 10
Thanks for reply. Unfortunately, I must use Sockets instead of built in java.net.HttpURLConnection. Also, I am not sure that you understood my problem.

Once more: I am able to detect beginning of response body. Its not hard. I have problems with mixed data.
As you may know, if client sends GET, server replies with header (text data) and some data (sometimes binary [image] sometimes not [example: js script]).

Assume that I ask server for jpeg image. I use BufferedReader stream

to read header with that code:

But later I have to read bytes. I know Content-Length (how much bytes to read) - its in the header but I can't use BufferedReader because it doesn't have method like readByte() or readFully().
So how can I read them? I've tried to read bytes from buffer by readLine() method and than call getBytes() on returned String but it doesn't work (image downloaded had similiar size to original but was broken). Also I've tried to read whole data with DataInputStream - it has appropriate methods to read bytes but method readLine() in this class is deprecated so I can't read header that way.

My code:


Note that, this code doesn't work - there is an error in line number 29. I can't read data in that way.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
But later I have to read bytes. I know Content-Length (how much bytes to read) - its in the header but I can't use BufferedReader because it doesn't have method like readByte() or readFully().
So how can I read them? I've tried to read bytes from buffer by readLine() method and than call getBytes() on returned Str


You can't open the response input streat 2 ways, so obviously you have to open it as a byte strream instead of as a Reader and interpret the bytes. A Reader will try to interpret the image bytes as some sort of characters - disasterous.

Just recognize the sequence crlfcrlf in the byte stream to detect the end of the headers. Thats the byte sequence 0x0D,0x0A,0x0D,0x0A in hex.

Save those bytes as you are reading them, then you can go back and interpret as characters.

Bill
Lukasz Jarocki
Greenhorn

Joined: Jan 01, 2013
Posts: 10
Thank you. It looks like there is some work to do, I just thought there is an easier way. Temporarily I use deprecated method from DataInputStream to read header. So far so good. I'll fix it later.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: InputStreams reading text and bytes from socket
 
Similar Threads
need some help....
Help with HTTP I/O
Java Http Server and Java Http Client
Downloading Multiple Files
how to implement the "session" function in my socket program?