From b433bb8c96f98d280f4a8508ba500bd8f196a773 Mon Sep 17 00:00:00 2001 From: Amith Yamasani Date: Wed, 18 Sep 2013 15:10:16 -0700 Subject: Changes to PFD based on API review Removed boolean param to ask for exception on detached fd. Use a subclass of IOException instead. Bug: 10461576 Change-Id: If7db16120297edcdb7d5d5905ed453003be0e38e --- core/java/android/os/ParcelFileDescriptor.java | 59 ++++++++++++++------------ 1 file changed, 32 insertions(+), 27 deletions(-) (limited to 'core/java/android/os') diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java index 579971d..c8b6539 100644 --- a/core/java/android/os/ParcelFileDescriptor.java +++ b/core/java/android/os/ParcelFileDescriptor.java @@ -80,7 +80,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { private byte[] mStatusBuf; /** - * Status read by {@link #checkError(boolean)}, or null if not read yet. + * Status read by {@link #checkError()}, or null if not read yet. */ private Status mStatus; @@ -371,7 +371,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { *

* The write end has the ability to deliver an error message through * {@link #closeWithError(String)} which can be handled by the read end - * calling {@link #checkError(boolean)}, usually after detecting an EOF. + * calling {@link #checkError()}, usually after detecting an EOF. * This can also be used to detect remote crashes. */ public static ParcelFileDescriptor[] createReliablePipe() throws IOException { @@ -409,7 +409,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { *

* Both ends have the ability to deliver an error message through * {@link #closeWithError(String)} which can be detected by the other end - * calling {@link #checkError(boolean)}, usually after detecting an EOF. + * calling {@link #checkError()}, usually after detecting an EOF. * This can also be used to detect remote crashes. */ public static ParcelFileDescriptor[] createReliableSocketPair() throws IOException { @@ -685,7 +685,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * Indicates if this ParcelFileDescriptor can communicate and detect remote * errors/crashes. * - * @see #checkError(boolean) + * @see #checkError() */ public boolean canDetectErrors() { if (mWrapped != null) { @@ -703,17 +703,16 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * If this ParcelFileDescriptor is unable to detect remote errors, it will * return silently. * - * @param throwIfDetached requests that an exception be thrown if the remote - * side called {@link #detachFd()}. Once detached, the remote + * @throws IOException for normal errors. + * @throws FileDescriptorDetachedException + * if the remote side called {@link #detachFd()}. Once detached, the remote * side is unable to communicate any errors through - * {@link #closeWithError(String)}. An application may pass true - * if it needs a stronger guarantee that the stream was closed - * normally and was not merely detached. + * {@link #closeWithError(String)}. * @see #canDetectErrors() */ - public void checkError(boolean throwIfDetached) throws IOException { + public void checkError() throws IOException { if (mWrapped != null) { - mWrapped.checkError(throwIfDetached); + mWrapped.checkError(); } else { if (mStatus == null) { if (mCommFd == null) { @@ -726,8 +725,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { mStatus = readCommStatus(mCommFd, getOrCreateStatusBuffer()); } - if (mStatus == null || mStatus.status == Status.OK - || (mStatus.status == Status.DETACHED && !throwIfDetached)) { + if (mStatus == null || mStatus.status == Status.OK) { // No status yet, or everything is peachy! return; } else { @@ -868,13 +866,26 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { * attached has been closed. * * @param e error state, or {@code null} if closed cleanly. - * @param fromDetach indicates if close event was result of - * {@link ParcelFileDescriptor#detachFd()}. After detach the - * remote side may continue reading/writing to the underlying - * {@link FileDescriptor}, but they can no longer deliver - * reliable close/error events. + * If the close event was the result of + * {@link ParcelFileDescriptor#detachFd()}, this will be a + * {@link FileDescriptorDetachedException}. After detach the + * remote side may continue reading/writing to the underlying + * {@link FileDescriptor}, but they can no longer deliver + * reliable close/error events. */ - public void onClose(IOException e, boolean fromDetach); + public void onClose(IOException e); + } + + /** + * Exception that indicates that the file descriptor was detached. + */ + public static class FileDescriptorDetachedException extends IOException { + + private static final long serialVersionUID = 0xDe7ac4edFdL; + + public FileDescriptorDetachedException() { + super("Remote side is detached"); + } } /** @@ -917,7 +928,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { case ERROR: return new IOException("Remote error: " + msg); case DETACHED: - return new IOException("Remote side is detached"); + return new FileDescriptorDetachedException(); case LEAKED: return new IOException("Remote side was leaked"); default: @@ -942,13 +953,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { @Override public void handleMessage(Message msg) { final Status s = (Status) msg.obj; - if (s.status == Status.DETACHED) { - listener.onClose(null, true); - } else if (s.status == Status.OK) { - listener.onClose(null, false); - } else { - listener.onClose(s.asIOException(), false); - } + listener.onClose(s != null ? s.asIOException() : null); } }; } -- cgit v1.1