Reading from InputStream in several formats

I am trying to write a class that reads HTTP requests and responses and parses them. Since the headers are plain text, the easiest way to read them is with the BufferedReader and readLine methods. This will obviously not do for the data body, since it can be binary, so I want to switch to reading raw bytes after reading the headers.

Now I am doing something like this:

InputStream input=socket.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(input)); BufferedInputStream binstream=new BufferedInputStream(input); 

The problem is that the BufferedReader reads ahead and absorbs all binary data from the stream before I can get to it using the bin stream.

Is there a way to prevent it from reading outside the newline for every call to readLine ? Or is there a better way to read single lines of ASCII text with raw binary data?

+6
java inputstream bufferedreader
source share
3 answers

Java already has a class for handling HTTP requests and responses. You should use this instead of trying to analyze the answer yourself. Parsing an HTTP response is more complicated than you think, because there are different encoding methods that you have to deal with. This is not raw binary data in the response payload. The HttpURLConnection class will parse the headers for you and give you an InputStream for the payload.

http://download.oracle.com/javase/1.4.2/docs/api/java/net/HttpURLConnection.html

+6
source share

If you do not want to use a ready-made HTTP client / server implementation, as Konstantin suggested, DataInputStream has a readLine method. It is deprecated since it doesnโ€™t perform the correct conversion (basically direct byte customization โ†’ char conversion), but I think you should be good for clean ASCII header lines.

(You should put a BufferedInputStream under the DataInputStream, since readLine reads each byte individually.)

+4
source share

commons-httpclient can save you a ton of work here.

+2
source share

All Articles