MemoryFile constructor and native methods throw IOExceptions.

These native methods in android.os.MemoryFile throw IOException but their
Java declarations did not include "throws IOException":
native_open(),native_mmap(),native_read(),native_write(),native_pin()

The MemoryFile(String,int) constructor calls native_open and
native_mmap, but does not declare that it throws IOException. The other
Java methods that call the native methods do actually declare that they
throw IOException.

This means that any code that created memory files could throw
an IOException, without knowing about it.

This changes adds "throws IOException" to the native methods and to
the constructor. The constructor change changes the public API, but
maintains binary compatibility. There is some precedent for making
source incompatible source API changes for this sort of thing
(see https://mondrian.corp.google.com/changelist/124214-p9).

The change also makes the native methods static, which
they seem to have been intended to be, as indicated by the
second parameter to the native implementations being named
"clazz".

This requires changes to the Compatibility Test Suite to catch the exceptions.
This is done in https://android-git.corp.google.com/g/2617
Unfortunately that change must be submitted together with this one in order
not to break the build.

Fixes http://b/issue?id=1881829
This commit is contained in:
Bjorn Bringert
2009-05-28 14:48:32 +01:00
parent 607384d45f
commit 9fc2e9c965
2 changed files with 12 additions and 9 deletions

View File

@ -93802,6 +93802,8 @@
</parameter>
<parameter name="length" type="int">
</parameter>
<exception name="IOException" type="java.io.IOException">
</exception>
</constructor>
<method name="allowPurging"
return="boolean"

View File

@ -37,15 +37,15 @@ public class MemoryFile
private static String TAG = "MemoryFile";
// returns fd
private native int native_open(String name, int length);
private static native int native_open(String name, int length) throws IOException;
// returns memory address for ashmem region
private native int native_mmap(int fd, int length);
private native void native_close(int fd);
private native int native_read(int fd, int address, byte[] buffer,
int srcOffset, int destOffset, int count, boolean isUnpinned);
private native void native_write(int fd, int address, byte[] buffer,
int srcOffset, int destOffset, int count, boolean isUnpinned);
private native void native_pin(int fd, boolean pin);
private static native int native_mmap(int fd, int length) throws IOException;
private static native void native_close(int fd);
private static native int native_read(int fd, int address, byte[] buffer,
int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
private static native void native_write(int fd, int address, byte[] buffer,
int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
private static native void native_pin(int fd, boolean pin) throws IOException;
private int mFD; // ashmem file descriptor
private int mAddress; // address of ashmem memory
@ -57,8 +57,9 @@ public class MemoryFile
*
* @param name optional name for the file (can be null).
* @param length of the memory file in bytes.
* @throws IOException if the memory file could not be created.
*/
public MemoryFile(String name, int length) {
public MemoryFile(String name, int length) throws IOException {
mLength = length;
mFD = native_open(name, length);
mAddress = native_mmap(mFD, length);