根据:
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
来自:
http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29
和这个注释
In particular, code of the form
int n = in.available();
byte buf = new byte[n];
in.read(buf);
is not guaranteed to read all of the remaining bytes from the given input stream.
http://docs.oracle.com/javase/8/docs/technotes/guides/io/troubleshooting.html
这是否意味着使用下面的函数
导致无法完全读取文件?
/**
* Reads a file from /raw/res/ and returns it as a byte array
* @param res Resources instance for Mosembro
* @param resourceId ID of resource (ex: R.raw.resource_name)
* @return byte[] if successful, null otherwise
*/
public static byte[] readRawByteArray(Resources res, int resourceId)
{
InputStream is = null;
byte[] raw = new byte[] {};
try {
is = res.openRawResource(resourceId);
raw = new byte[is.available()];
is.read(raw);
}
catch (IOException e) {
e.printStackTrace();
raw = null;
}
finally {
try {
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return raw;
}
最佳答案
available() 返回可以无阻塞地读取的字节数。该数字(可以为零)与文件的总长度之间没有必然的相关性。
关于java - InputStream.available() 并从 oracle 中完整读取文件注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25570213/