在Android N设备上测试应用程序时,我发现与使用BufferedinputStream的方式有关的许多新问题.问题似乎是我如何解释BufferedinputStream.read()的返回值.如果返回0,则假定可以从基础流读取0字节,如果为-1,则假定基础流已到达文件末尾(并退出读取).直到牛轧糖,这一直很好.现在,当基础inputStream.read()返回0时,BufferedinputStream.read()返回-1.正确的行为是什么?为什么改变了?
这是一个提炼的例子…
class ZeroBytesReadinputStream extends inputStream { ... @OverrIDe public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { return 0; } } private int testZeroBytesReadBufferedinputStream() { inputStream inputStream = new ZeroBytesReadinputStream(); BufferedinputStream bufferedinputStream = new BufferedinputStream(inputStream); int valueReturned = 0; try { byte[] toStream = new byte[100]; valueReturned = bufferedinputStream.read(toStream, 0, 100); Log.d("Zero Bytes Read Test", "BufferedinputStream returned = " + valueReturned + ". AndroID Version = " + androID.os.Build.VERSION.SDK_INT); } catch (IOException e) { e.printstacktrace(); } return valueReturned; }
@H_404_9@运行时输出-
10-19 09:28:51.970 9138 9138 D零字节读取测试:返回的BufferedinputStream = -1. AndroID版本= 2410-19 09:32:19.200 12675 12675 D零字节读取测试:BufferedinputStream返回= 0.AndroID版本= 23
解决方法:
根据InputStream.read(byte[] b, int off, int len)的文档,除非len参数为0,否则该方法实际上不应返回0:
This method blocks until input data is available, end of file is
detected, or an exception is thrown.If len is zero, then no bytes are read and 0 is returned; otherwise,
there is an attempt to read at least one byte. If no byte is available
because the stream is at end of file, the value -1 is returned;
otherwise, at least one byte is read and stored into b.因此,无阻碍的流实现是有问题的,因此BufferedinputStream的行为是不确定的.
总结以上是内存溢出为你收集整理的为什么BufferedInputStream.read()在用于返回0时在Android Nougat中返回-1?全部内容,希望文章能够帮你解决为什么BufferedInputStream.read()在用于返回0时在Android Nougat中返回-1?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)