简体   繁体   English

IOUtils.toString需要很长时间

[英]IOUtils.toString taking to long

I have this class which opens a HTTP-Server and listens on a port. 我有一个此类,它将打开一个HTTP服务器并在端口上侦听。 The reply is a http-header plus a json object. 答复是一个HTTP标头和一个json对象。 The class takes the input stream from the server, converts it to a string, filters the http header and parses the json object. 该类从服务器获取输入流,将其转换为字符串,过滤http头并解析json对象。

The thing is that the conversion from input stream to string is taking about 3 seconds. 问题是从输入流到字符串的转换大约需要3秒钟。 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
    }

}

}

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. ...您似乎认为getInputStream()返回时已经读取了所有数据。 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. 我的猜测是大约3秒钟……这可能是由于网络速度慢,或者客户端花费很长时间甚至发送所有数据所致。 (It could connect, sleep for 2.9 seconds and then send a bunch of data.) (它可以连接,睡眠2.9秒, 然后发送一堆数据。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 大型流的IOUtils.toString()的替代方法? - Alternative to IOUtils.toString() for large streams? 相当于IOUtils.toString(InputStream)的番石榴 - Guava equivalent for IOUtils.toString(InputStream) 要使用 IOUtils.toString() 导入什么? - What to import to use IOUtils.toString()? IOUtils.toString() 异常 java.net.SocketTimeoutException: null - IOUtils.toString() exception java.net.SocketTimeoutException: null 在使用commons-io的IOUtils.toString(输入)后,是否需要手动关闭输入流? - Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io? JAVA使用IOUtils.toString和HttpEntity.getContent()将InputStream转换为null - JAVA Using IOUtils.toString with HttpEntity.getContent() converting the InputStream to null 使用 `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); 时无法解码特殊字符 ` - Cannot decode special character when using `IOUtils.toString(containerRequestContext.getEntityStream(),“UTF-8”); ` Java:IOUtils.toByteArray(input) 对于某些导致高 TP99.9 的请求花费的时间太长 - Java: IOUtils.toByteArray(input) taking too long for some requests causing high TP99.9 在Java中使用toString打印Long值 - Printing Long values with toString in Java ModelMapper是否采用toString方法转换属性? - ModelMapper is taking toString method to convert property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM