Android DataOutputStream Flush方法似乎并没有真正刷新

发布于 2024-10-22 20:19:00 字数 944 浏览 1 评论 0原文

这是我正在做的一些快速代码。 (我删除了其中一些内容以使其更具可读性。)本质上,我正在打开一个文件,并一次处理 3k 块。这些被编码为 Base64(现在为 4k 块)并通过 HTTP post 上传。每次调用 DataOutputStream.writeBytes() 后,我还会调用 DataOutputStream.flush(),然后根据已发送的数量更新进度条。

File myImage = new File(somepath);
int bytesAvailable = myImage.length();
while (bytesAvailable > 0)
{
   byte[] buffer = new byte[Math.min(12288, (int)bytesAvailable)];
   bytesRead = fileInputStream.read(buffer, 0, Math.min(12288, (int)bytesAvailable));

   if (bytesRead > 0)
   {
      s2 = Base64.encodeBytes(buffer);
      bytesAvailable = fileInputStream.available();

      dataStream.writeBytes(s2);
      dataStream.flush();

      // UPDATE THE PROGRESS BAR HERE
   }
}

现在,当我运行该应用程序时,图像每次都会成功上传。但是,如果我观察移动连接指示器(向上/向下箭头),它们在进度条移动时不会亮起。对于 3MB 的图像,条形图将在 10 秒左右的时间内从 0% 变为 100%(对于我的 3G 手机来说太快了)。然后在 100% 时,循环完成后,调用 dataStream.close() 方法。这是数据传输开始的时候(如箭头所示)。等待几分钟后完成。

我是否没有正确理解flush()?难道不应该强制传输数据吗?

Here's some quick code I am doing. (I removed some of it to make it more readable.) Essentially, I'm opening a file, and processing 3k chunks at a time. These get encoded to Base64 (4k chunk now) and uploaded via HTTP post. After each call to DataOutputStream.writeBytes() I also call DataOutputStream.flush() and then I update a progress bar based on how much has been sent.

File myImage = new File(somepath);
int bytesAvailable = myImage.length();
while (bytesAvailable > 0)
{
   byte[] buffer = new byte[Math.min(12288, (int)bytesAvailable)];
   bytesRead = fileInputStream.read(buffer, 0, Math.min(12288, (int)bytesAvailable));

   if (bytesRead > 0)
   {
      s2 = Base64.encodeBytes(buffer);
      bytesAvailable = fileInputStream.available();

      dataStream.writeBytes(s2);
      dataStream.flush();

      // UPDATE THE PROGRESS BAR HERE
   }
}

Now, when I run the application, the image is successfully uploaded each time. However if I watch the mobile connection indicator (up/down arrows), they do not light up while the progress bar moves. The bar will go from 0 to 100% over the course of 10 seconds or so for a 3MB image (far too fast for my 3G phone). Then at 100%, after this loop is done, the dataStream.close() method is called. This is when the data transmission starts (as indicated by the arrows). This sits for a few minutes before finishing.

Am I not understanding flush() correctly? Shouldn't it force data to be transmitted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-10-29 20:19:00

在我看来,您正在将 4k 块添加到数据流中,然后将该数据流通过 http post 发送到服务器。如果是这种情况,那么您的循环本质上是在内存副本上进行迭代,并且 http 传输将在您完成数据流后发生。确定上传情况的唯一方法是掌握套接字及其数据流。如果您使用 DefaultHttpClientConnection 等,这看起来相当困难。

It sounds to me like you're adding 4k chunks into a data stream and then sending that data stream in a http post to a server. If this is the case, then your loop is essentially iterating over a memory copy, and the http transfer will happen after you finish with the data stream. The only way to know for sure how the upload is going will be to get your hands on the socket and its data stream. This looks quite difficult if your using DefaultHttpClientConnection and so on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文