I get BufferedReader from the following networking code.
URL url = new URL(request);
HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpUrlConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- I was wondering, which way give higher efficiency in term of execution speed?
-
Is 1024 size a good choice for Android? Or, is there any system constant value I can use?
int charRead = 0; char[] buffer = new char[1024]; StringBuffer stringBuffer = new StringBuffer(); while ((charRead = bufferedReader.read(buffer)) > 0) { stringBuffer.append(buffer, 0, charRead); }
String line = "";
StringBuffer stringBuffer = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
I think the first approach is more efficient, but I’d rather set higher buffer size ex. 8*1024. This is default CoreConnectionPNames.SOCKET_BUFFER_SIZE in HttpClient.
I’d rather say that something below 8*1024 is unreasonable because most of HTTP buffers are set to 8*1024.
In the second approach you’re assuming new line characters in the stream. What in case of 20MB json optimized in such way that there is no new line characters? This will blow your app.
In my opinion the best choice is to use multiplication of 8*1024. The upper constraint is bound to size of memory and connection speed with respect to stream’s buffer processing time.