Why does bufferedinputstream. Read() return – 1 in Android nougat when used to return 0?

When testing the application on Android N devices, I found many new problems related to the way I use bufferedinputstream. The problem seems to be how I interpret the return value of bufferedinputstream. Read(). If 0 is returned, it is assumed that 0 bytes can be read from the underlying stream. If - 1, it is assumed that the underlying stream has reached the end of the file (and quit reading). This has been good until nougat. Now, When the underlying InputStream. Read() returns 0, bufferedinputstream. Read() returns - 1. What is the correct behavior? Why has it changed?

This is a refined example

   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;
    }

Runtime output - 10-19 09:28:51.970 9138 9138 D zero byte read test: bufferedinputstream returned = - 1. Android version = 24

10-19 09:32:19.200 12675 12675 D zero byte read test: bufferedinputstream return = 0. Android version = 23

resolvent:

According to the document of inputstream.read (byte [] B, int off, int len), the method should not actually return 0 unless the len parameter is 0:

Therefore, unimpeded flow implementation is problematic, so the behavior of bufferedinputstream is uncertain

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>