Performance hit due to converting REST API response to String

Viewed 153

There is a rest endpoint that I am hitting via http library and converting the response to String (as shown below), which is then written to HDFS (Hadoop Distributed File System).

HttpClient httpclient = new HttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response= httpclient.execute(httpget);

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
    content.append(inputLine);
}
String respStr = content.toString();

What I realized that though REST API is responding in milliseconds but due to above code of converting the response to string, it is taking a lot of time.

Can someone suggest how to speed up the behavior or how to do it right way?

1 Answers

I suspect that the time is takes to convert into a String is mostly spent reading from the network stream. Unless that String is gigantic, it should not take too long to build the String itself.

In any case this would be shorter (in terms of lines of code):

String respStr = EntityUtils.toString(response.getEntity().getContent());

If you copy that data without any transformation, it should be faster skipping the String conversion and directly writing to the outpustream, for instance using org.apache.commons.io.IOUtils:

IOUtils.copy(response.getEntity().getContent(), outpustream)
Related