Below is my method where I pass in as a parameter java.net.Socket:getInputStream().
This works perfectly well at almost all times.
Problem occurs when input stream from the socket is a big chunk of empty bytes (ambiguous data). This makes my program to stop responding altogether. Does anyone know what’s going on? Should I not get IOException or something rather than just stop responding? How can I just quit the read if e.g. data is some useless ambiguous information.
public static String fromStream(InputStream in) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
return out.toString();
}
The $64,000 question is what is this “meaningless ambiguous data”?
If it is truly meaningless, then you should really be focussing on why it is being sent. The chances are that it is caused by a bug in the application that is sending the data. Find and fix that, and you don’t need to change your client code.
On the other hand, it could be meaningful data … but you / your application don’t know what it means. In that case, the root problem is that the file is not a text file, and it is therefore incorrect to read it using a
Reader/BufferedReader.If you want to persist with trying to read this data as text … in such a way that the zero bytes / characters don’t cause you grief, then you need to read from the
BufferedReaderone character at a time. When you encounter a zero character (or bad ones) your character reading code can bail out. Otherwise, assemble the non-bad characters into lines if that is what you actually need.There are a couple of things to note from your comment:
Firstly, it sounds like you might be trying to interact with an HTTP server using plain sockets. This is a bad idea. A really bad idea! The chances are that you plain socket code will not be able to correctly interpret the various different ways that a kosher HTTP server could send a response. (And that would explain the following …)
Secondly, an HTTP response includes a “Content-type” that gives you the media type of stuff in the response body. If your client ignores the Content-type header, you risk processing the response body the wrong way. For instance, you might get a PDF file or a TAR file which could certainly contain large numbers of zero bytes.
On the other hand, you could be doing these things the right way, and the HTTP server could just be broken.