小编Aka*_*hav的帖子

为什么BufferedInputStream一次读取最多2048个字节?

我正在尝试使用我的Android应用程序HttpUrlConnection中的网络读取文件AsyncTask.

虽然我注意到文件下载工作的速度比它应该有的慢一点,因为我使用的网络速度更快.

所以我检查并发现该BufferedInputStream对象一次最多只能读取2048个字节.没有我设置的缓冲区大小.甚至内部默认缓冲区大小为BufferedInputStream8192字节.

我在这里添加我的代码以供参考.

private class DownloadFileTask extends AsyncTask<String, Integer, String> {

  @Override
  protected String doInBackground(String... params) {
    HttpURLConnection connection = null;
    BufferedInputStream input = null;
    OutputStream output = null;
    int lengthOfFile;
    int totalBytesDownloaded = 0;
    int count;
    final int bufferSize = 8 * 1024; // 8KB
    try {
      // Create the URL
      URL url = new URL(params[0]);
      // Open connection
      connection = (HttpURLConnection) url.openConnection();
      // Get the file length
      lengthOfFile = …
Run Code Online (Sandbox Code Playgroud)

java io android bufferedinputstream httpurlconnection

0
推荐指数
1
解决办法
1171
查看次数