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:
@ -93802,6 +93802,8 @@
|
|||||||
</parameter>
|
</parameter>
|
||||||
<parameter name="length" type="int">
|
<parameter name="length" type="int">
|
||||||
</parameter>
|
</parameter>
|
||||||
|
<exception name="IOException" type="java.io.IOException">
|
||||||
|
</exception>
|
||||||
</constructor>
|
</constructor>
|
||||||
<method name="allowPurging"
|
<method name="allowPurging"
|
||||||
return="boolean"
|
return="boolean"
|
||||||
|
@ -37,15 +37,15 @@ public class MemoryFile
|
|||||||
private static String TAG = "MemoryFile";
|
private static String TAG = "MemoryFile";
|
||||||
|
|
||||||
// returns fd
|
// 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
|
// returns memory address for ashmem region
|
||||||
private native int native_mmap(int fd, int length);
|
private static native int native_mmap(int fd, int length) throws IOException;
|
||||||
private native void native_close(int fd);
|
private static native void native_close(int fd);
|
||||||
private native int native_read(int fd, int address, byte[] buffer,
|
private static native int native_read(int fd, int address, byte[] buffer,
|
||||||
int srcOffset, int destOffset, int count, boolean isUnpinned);
|
int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
|
||||||
private native void native_write(int fd, int address, byte[] buffer,
|
private static native void native_write(int fd, int address, byte[] buffer,
|
||||||
int srcOffset, int destOffset, int count, boolean isUnpinned);
|
int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
|
||||||
private native void native_pin(int fd, boolean pin);
|
private static native void native_pin(int fd, boolean pin) throws IOException;
|
||||||
|
|
||||||
private int mFD; // ashmem file descriptor
|
private int mFD; // ashmem file descriptor
|
||||||
private int mAddress; // address of ashmem memory
|
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 name optional name for the file (can be null).
|
||||||
* @param length of the memory file in bytes.
|
* @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;
|
mLength = length;
|
||||||
mFD = native_open(name, length);
|
mFD = native_open(name, length);
|
||||||
mAddress = native_mmap(mFD, length);
|
mAddress = native_mmap(mFD, length);
|
||||||
|
Reference in New Issue
Block a user