diff options
| author | Steven Moreland <smoreland@google.com> | 2019-03-04 17:56:30 -0800 |
|---|---|---|
| committer | Steven Moreland <smoreland@google.com> | 2019-03-06 17:17:30 +0000 |
| commit | 0ff061a607341bca84e40f7dda5b713c88b522fb (patch) | |
| tree | 258bdce0fcd86538470f21782947c9bc830dd365 /core/java/android/os/NativeHandle.java | |
| parent | 8bf13f06d71f01b79752965526cbdf598c1067fd (diff) | |
NativeHandle.java: set Nullable/NonNull
Fixes: 126700972
Test: hidl_test_java
Test: atest android.os.cts.HwBinderTest
Change-Id: Id45c6d0757913014028f7629db78b64f27cad084
Diffstat (limited to 'core/java/android/os/NativeHandle.java')
| -rw-r--r-- | core/java/android/os/NativeHandle.java | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/core/java/android/os/NativeHandle.java b/core/java/android/os/NativeHandle.java index f13bf5fccd38..8d341b603eb4 100644 --- a/core/java/android/os/NativeHandle.java +++ b/core/java/android/os/NativeHandle.java @@ -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; - } - - try { - for (FileDescriptor fd : mFds) { - Os.close(fd); + checkOpen(); + + 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; } } |
