【发布时间】:2014-08-29 14:18:14
【问题描述】:
根据:
请注意,虽然 InputStream 的某些实现会返回 流中的总字节数,很多不会。它从来不是 正确使用此方法的返回值分配缓冲区 旨在保存此流中的所有数据。
来自:
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
这是否意味着使用below function 导致无法完全读取文件?
/**
* 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;
}
【问题讨论】:
标签: java android file-io inputstream bufferedreader