Merge "NativeHandle.java: set Nullable/NonNull" am: 6792c595a2
am: b5e85d6bb6
Change-Id: If8aa9d85330683954856e2f1f2755d251bd0bb36
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package android.os;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
|
||||
@@ -241,7 +242,7 @@ public class HwBlob {
|
||||
* @param x a {@link NativeHandle} instance to write
|
||||
* @throws IndexOutOfBoundsException when [offset, offset + sizeof(jobject)] is out of range
|
||||
*/
|
||||
public native final void putNativeHandle(long offset, NativeHandle x);
|
||||
public native final void putNativeHandle(long offset, @Nullable NativeHandle x);
|
||||
|
||||
/**
|
||||
* Put a boolean array contiguously at an offset in the blob.
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package android.os;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.annotation.TestApi;
|
||||
|
||||
@@ -123,7 +125,7 @@ public class HwParcel {
|
||||
*
|
||||
* @param val to write
|
||||
*/
|
||||
public native final void writeNativeHandle(NativeHandle val);
|
||||
public native final void writeNativeHandle(@Nullable NativeHandle val);
|
||||
|
||||
/**
|
||||
* Writes an array of boolean values to the end of the parcel.
|
||||
@@ -170,6 +172,9 @@ public class HwParcel {
|
||||
private native final void writeStringVector(String[] val);
|
||||
/**
|
||||
* Writes an array of native handles to the end of the parcel.
|
||||
*
|
||||
* Individual elements may be null but not the whole array.
|
||||
*
|
||||
* @param val array of {@link NativeHandle} objects to write
|
||||
*/
|
||||
private native final void writeNativeHandleVector(NativeHandle[] val);
|
||||
@@ -284,7 +289,7 @@ public class HwParcel {
|
||||
* Helper method to write a list of native handles to the end of the parcel.
|
||||
* @param val list of {@link NativeHandle} objects to write
|
||||
*/
|
||||
public final void writeNativeHandleVector(ArrayList<NativeHandle> val) {
|
||||
public final void writeNativeHandleVector(@NonNull ArrayList<NativeHandle> val) {
|
||||
writeNativeHandleVector(val.toArray(new NativeHandle[val.size()]));
|
||||
}
|
||||
|
||||
@@ -359,7 +364,7 @@ public class HwParcel {
|
||||
* @return a {@link NativeHandle} instance parsed from the parcel
|
||||
* @throws IllegalArgumentException if the parcel has no more data
|
||||
*/
|
||||
public native final NativeHandle readNativeHandle();
|
||||
public native final @Nullable NativeHandle readNativeHandle();
|
||||
/**
|
||||
* Reads an embedded native handle (without duplicating the underlying
|
||||
* file descriptors) from the parcel. These file descriptors will only
|
||||
@@ -372,7 +377,7 @@ public class HwParcel {
|
||||
* @return a {@link NativeHandle} instance parsed from the parcel
|
||||
* @throws IllegalArgumentException if the parcel has no more data
|
||||
*/
|
||||
public native final NativeHandle readEmbeddedNativeHandle(
|
||||
public native final @Nullable NativeHandle readEmbeddedNativeHandle(
|
||||
long parentHandle, long offset);
|
||||
|
||||
/**
|
||||
@@ -521,7 +526,7 @@ public class HwParcel {
|
||||
* @return array of {@link NativeHandle} objects.
|
||||
* @throws IllegalArgumentException if the parcel has no more data
|
||||
*/
|
||||
public final ArrayList<NativeHandle> readNativeHandleVector() {
|
||||
public final @NonNull ArrayList<NativeHandle> readNativeHandleVector() {
|
||||
return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray()));
|
||||
}
|
||||
|
||||
|
||||
@@ -99,6 +99,8 @@ public final class NativeHandle implements Closeable {
|
||||
* @return a boolean value
|
||||
*/
|
||||
public boolean hasSingleFileDescriptor() {
|
||||
checkOpen();
|
||||
|
||||
return mFds.length == 1 && mInts.length == 0;
|
||||
}
|
||||
|
||||
@@ -108,7 +110,7 @@ public final class NativeHandle implements Closeable {
|
||||
* If this method is called, this must also be explicitly closed with
|
||||
* {@link #close()}.
|
||||
*/
|
||||
public NativeHandle dup() throws java.io.IOException {
|
||||
public @NonNull NativeHandle dup() throws java.io.IOException {
|
||||
FileDescriptor[] fds = new FileDescriptor[mFds.length];
|
||||
try {
|
||||
for (int i = 0; i < mFds.length; i++) {
|
||||
@@ -123,6 +125,12 @@ public final class NativeHandle implements Closeable {
|
||||
return new NativeHandle(fds, mInts, true /*own*/);
|
||||
}
|
||||
|
||||
private void checkOpen() {
|
||||
if (mFds == null) {
|
||||
throw new IllegalStateException("NativeHandle is invalidated after close.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the file descriptors if they are owned by this object.
|
||||
*
|
||||
@@ -130,19 +138,20 @@ public final class NativeHandle implements Closeable {
|
||||
*/
|
||||
@Override
|
||||
public void close() throws java.io.IOException {
|
||||
if (!mOwn) {
|
||||
return;
|
||||
}
|
||||
checkOpen();
|
||||
|
||||
try {
|
||||
for (FileDescriptor fd : mFds) {
|
||||
Os.close(fd);
|
||||
if (mOwn) {
|
||||
try {
|
||||
for (FileDescriptor fd : mFds) {
|
||||
Os.close(fd);
|
||||
}
|
||||
} catch (ErrnoException e) {
|
||||
e.rethrowAsIOException();
|
||||
}
|
||||
} catch (ErrnoException e) {
|
||||
e.rethrowAsIOException();
|
||||
|
||||
mOwn = false;
|
||||
}
|
||||
|
||||
mOwn = false;
|
||||
mFds = null;
|
||||
mInts = null;
|
||||
}
|
||||
@@ -154,7 +163,9 @@ public final class NativeHandle implements Closeable {
|
||||
* @throws IllegalStateException if this object contains either zero or
|
||||
* more than one file descriptor, or a non-empty data stream.
|
||||
*/
|
||||
public FileDescriptor getFileDescriptor() {
|
||||
public @NonNull FileDescriptor getFileDescriptor() {
|
||||
checkOpen();
|
||||
|
||||
if (!hasSingleFileDescriptor()) {
|
||||
throw new IllegalStateException(
|
||||
"NativeHandle is not single file descriptor. Contents must"
|
||||
@@ -171,6 +182,8 @@ public final class NativeHandle implements Closeable {
|
||||
* @hide
|
||||
*/
|
||||
private int[] getFdsAsIntArray() {
|
||||
checkOpen();
|
||||
|
||||
int numFds = mFds.length;
|
||||
int[] fds = new int[numFds];
|
||||
|
||||
@@ -182,11 +195,13 @@ public final class NativeHandle implements Closeable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch file descriptors.
|
||||
* Fetch file descriptors
|
||||
*
|
||||
* @return the fds.
|
||||
*/
|
||||
public FileDescriptor[] getFileDescriptors() {
|
||||
public @NonNull FileDescriptor[] getFileDescriptors() {
|
||||
checkOpen();
|
||||
|
||||
return mFds;
|
||||
}
|
||||
|
||||
@@ -195,7 +210,9 @@ public final class NativeHandle implements Closeable {
|
||||
*
|
||||
* @return the opaque data stream.
|
||||
*/
|
||||
public int[] getInts() {
|
||||
public @NonNull int[] getInts() {
|
||||
checkOpen();
|
||||
|
||||
return mInts;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user