diff options
author | Bjorn Bringert <bringert@android.com> | 2009-06-01 10:53:06 +0100 |
---|---|---|
committer | Bjorn Bringert <bringert@android.com> | 2009-06-03 12:53:42 +0100 |
commit | c1823701cc76790494fb622fe58f0942236cd7d0 (patch) | |
tree | 4732327a73e57fa5ad55e1724d79fbbf35654141 /core/java | |
parent | 963cd006c45716b034f656bf7e7179e6476f7e4d (diff) | |
download | frameworks_base-c1823701cc76790494fb622fe58f0942236cd7d0.zip frameworks_base-c1823701cc76790494fb622fe58f0942236cd7d0.tar.gz frameworks_base-c1823701cc76790494fb622fe58f0942236cd7d0.tar.bz2 |
Handle EOF correctly in MemoryFile input stream.
Before, the variants of MemoryFile.MemoryInputStream.read() would throw
IOException or IndexOutOfBoundsException if EOF was encountered
before the requested number of bytes was read. This violates
the contract of InputStream.read().
This patch makes read() return the number of bytes available, if any.
If already at EOF, -1 is returned. The patch also adds new tests,
which checks cases where MemoryFile.MemoryInputStream.read()
should throw IndexOutOfBoundsException or return -1. several of these
tests failed with the old code and pass now.
This fixes http://b/issue?id=1881894
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/android/os/MemoryFile.java | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/core/java/android/os/MemoryFile.java b/core/java/android/os/MemoryFile.java index 7e4cf8a..c14925c 100644 --- a/core/java/android/os/MemoryFile.java +++ b/core/java/android/os/MemoryFile.java @@ -353,13 +353,22 @@ public class MemoryFile } int result = read(mSingleByte, 0, 1); if (result != 1) { - throw new IOException("read() failed"); + return -1; } return mSingleByte[0]; } @Override public int read(byte buffer[], int offset, int count) throws IOException { + if (offset < 0 || count < 0 || offset + count > buffer.length) { + // readBytes() also does this check, but we need to do it before + // changing count. + throw new IndexOutOfBoundsException(); + } + count = Math.min(count, available()); + if (count < 1) { + return -1; + } int result = readBytes(buffer, mOffset, offset, count); if (result > 0) { mOffset += result; |