读取 HttpURLConnection 的 InputStream 时,是否有理由使用以下其中之一?我已经在示例中看到了两者。
手动缓冲:
while ((length = inputStream.read(buffer)) > 0) {
os.write(buf, 0, ret);
}
缓冲输入流
is = http.getInputStream();
bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append(current);
}
编辑一般来说,我对 HTTP 还是很陌生,但我想到的一个考虑是,如果我使用的是持久 HTTP 连接,我不能在输入流为空之前读取,对吗?在那种情况下,我不需要读取消息长度并只读取该长度的输入流吗?
同样,如果不使用持久连接,我包含的代码在正确读取流方面是否 100% 好?