Measure intermediate throughput when writing to OutputStream [or] Why is write() blocking, while read() is not?

萝らか妹 提交于 2019-12-11 06:57:48

问题


I need to measure the intermediate throughput of uploading and downloading a large file (via FTP using Apache Commons Net API).

Download: (No problems)

Everything is fine for downloading, which uses code like the following:

InputStream is = ftp.retrieveFileStream(filePath);
byte[] buffer = new byte[256 * 1024];
int read;

while ((read = is.read(buffer, 0, buffer.length)) != -1) {
    Log.v(TAG, "Read = " + read);
}

Here, the buffer is 256 KB in size, and the total size of the file being downloaded is 1 MB. Still each individual call to is.read() returns only about 2 KB. In this case read() does not block until the buffer is full. This enables me to measure intermediate download throughput. So far so good.

Upload: (Problematic)

Now comes the pain. When I allocate a similar sized buffer to write to an OutputStream, the call to write() blocks until the entire 256 KB is written.

Why does only write() block, whereas read() doesn't?

So I tried to use nio. Unlike Outputstream's write(), the write() method of WritableByteChannel returns the number of bytes actually written. So it must be non-blocking, right? No such luck. This write() also blocks until the whole buffer is written. Here is the code:

byte[] buffer = new byte[256 * 1024];
new Random.nextBytes(byteArray);

OutputStream os = ftp.storeFileStream(filePath);
WritableByteChannel channel = Channels.newChannel(os);
ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
int written = channel.write(byteBuffer);

So how can I measure the intermediate throughput when the upload is blocking?

One way is to use Android's TrafficStats API. But it comes with its own set of frustrations. As per the documentation, these statistics may not be available on all platforms. Also it needs handling of scenarios like integer counter overflow (after 2GB), and counter reset bugs on some platforms when switching between 3G and Wifi.

Can someone show me a way out?


回答1:


Why does only write() block, whereas read() doesn't?

Because that's the way the underlying operating system behaves.



来源:https://stackoverflow.com/questions/10179177/measure-intermediate-throughput-when-writing-to-outputstream-or-why-is-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!