Getting a wrong FileChannel size

假装没事ソ 提交于 2019-12-20 02:14:06

问题


I'm trying to get the size of a file contained in assets. I'm using a FileChannel because I need a FileChannel later. The file myfile.txt contains 7 bytes.

Here is my code:

AssetManager amgr;
AssetFileDescriptor afd;
FileChannel fchIn;
FileInputStream fis;

amgr=context.getAssets();
afd=amgr.openFd("myfile.txt");
fis=afd.createInputStream();
fchIn=fis.getChannel();

Log.d("mytag", fchIn.size());
Log.d("mytag", fis.available());

And the output is:

7237492
7

Why is the size returned by the FileChannel.size() method wrong ?

Thanks for your help


回答1:


FileInputStream.getChannel() documentation says it "Returns a read-only FileChannel that shares its position with this stream." You are assuming that the channel begins and ends exactly at the boundaries of your file, which is the only way its total size() would match what you expect, but the documentation does not make that guarantee.

Also note that FileInputStream.available() is not documented to mean the same thing as total size -- technically, it is the amount of data available to read without needing to load/buffer more from the source. Unless the entire source has already been read into memory, it may have almost nothing to do with the actual file size.

I expect that the underlying FileChannel has access to a large range of bytes for multiple files, like all of your assets combined (and hence the large size), and you'd need to reference the AssetFileDescriptor's getStartOffset() and getLength() methods to know the actual position of the associated file's bytes within the channel. My guess is that AssetFileDescriptor.createInputStream() is giving you an object that already knows the position and range and takes care of that for you. If you're going to use the raw channel, then honor the information in the file descriptor. Also note that, per the documentation, if you move the position of the FileInputStream (e.g. by reading bytes) then you also move the position of the channel at the same time, so be careful if you're trying to use both.



来源:https://stackoverflow.com/questions/22020604/getting-a-wrong-filechannel-size

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