IOUtils.toString taking to long

I have this class which opens a HTTP-Server and listens on a port. The reply is a http-header plus a json object. The class takes the input stream from the server, converts it to a string, filters the http header and parses the json object.

The thing is that the conversion from input stream to string is taking about 3 seconds. Is there a way to read the inputstream faster?

public class GamestateListener {

private static int PORT = 3001; // Port specified in your cfg
public static ServerSocket listenServer;
private static JSONObject MYJSONOBJ;

public GamestateListener() {
    try {
        listenServer = new ServerSocket(PORT); // open new Server Socket
        System.out.println("Started Socket..."); // printing out started
                                                    // listening
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public JSONObject listenAndParseJSON() throws IOException {

    System.out
            .println("Listening for connection on port " + PORT + " ...."); // printing
                                                                            // out
                                                                            // started
                                                                            // listening

    try (Socket socket = listenServer.accept()) { // wait for connection

        System.out.println("Start get From Socket           "
                + System.currentTimeMillis());
        InputStream mis = socket.getInputStream();
        System.out.println("Stop get From Socket           "
                + System.currentTimeMillis());
        String responseString = IOUtils.toString(mis, "UTF-8");
        System.out.println("Stop to String           "
                + System.currentTimeMillis());
        MYJSONOBJ = new JSONObject(responseString.substring(responseString
                .indexOf("{")));// split the response string

        return MYJSONOBJ;// return the json obj

    } catch (Exception e) {
        MYJSONOBJ = new JSONObject("{ERROR:True}");// create error obj
        return MYJSONOBJ;// return it
    }

}

}
Jon Skeet
people
quotationmark

You're not measuring what you think you are. Here:

System.out.println("Start get From Socket           "
        + System.currentTimeMillis());
InputStream mis = socket.getInputStream();
System.out.println("Stop get From Socket           "
        + System.currentTimeMillis());

... you seem to think that you've read all the data when getInputStream() returns. You haven't. You've just got a stream you can read from. It means there's a connection, but that's all.

If you want to measure how long it takes just to read all the data (and not actually process it) you could do something like:

System.out.println("Start get From Socket           "
        + System.currentTimeMillis());
InputStream mis = socket.getInputStream();
byte[] buffer = new byte[8192];
// Read all the data (but ignore it)
while ((mis.read(buffer)) != -1) ;
System.out.println("Stop get From Socket           "
        + System.currentTimeMillis());

Of course, there then won't be any data to read for the rest of the code, but you'll see how long just the data reading takes. My guess is that that'll be about 3 seconds... which could be due to slow networking, or the client taking a long time to even send all the data. (It could connect, sleep for 2.9 seconds and then send a bunch of data.)

people

See more on this question at Stackoverflow