So I was running a profiler on my (admittedly pretty simple) application in Java, and was surprised that second only to methods that required making HTTP requests in terms of time was my inputStreamToString
method. It's currently defined like this:
public static String inputStreamToString(InputStream in) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
in.close();
return sb.toString();
}
How can I make this faster? (And yes, I really do need strings, and no, the InputStrings aren't that large, and no, this method is being called less frequently than most methods in the program, and no, I there is no way to avoid the need of conversion.)