From 065b299df4159602327977dd007cb2cd6b64ab20 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Sun, 5 Aug 2012 14:16:48 -0700 Subject: [PATCH] Make LocalSocket Closeable. Enables usage of IoUtils.closeQuietly(). Change-Id: I91126297c1f235ae9da09f82d8f4f22db46558eb --- api/current.txt | 2 +- core/java/android/net/LocalSocket.java | 4 +++- .../java/com/android/server/connectivity/Vpn.java | 14 ++++---------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/api/current.txt b/api/current.txt index 4b433185811cf..6e0cd7c05b1b6 100644 --- a/api/current.txt +++ b/api/current.txt @@ -12443,7 +12443,7 @@ package android.net { method public android.net.LocalSocketAddress getLocalSocketAddress(); } - public class LocalSocket { + public class LocalSocket implements java.io.Closeable { ctor public LocalSocket(); method public void bind(android.net.LocalSocketAddress) throws java.io.IOException; method public void close() throws java.io.IOException; diff --git a/core/java/android/net/LocalSocket.java b/core/java/android/net/LocalSocket.java index 34e0d9a92f3b7..14a80948b7977 100644 --- a/core/java/android/net/LocalSocket.java +++ b/core/java/android/net/LocalSocket.java @@ -16,6 +16,7 @@ package android.net; +import java.io.Closeable; import java.io.FileDescriptor; import java.io.IOException; import java.io.InputStream; @@ -26,7 +27,7 @@ import java.net.SocketOptions; * Creates a (non-server) socket in the UNIX-domain namespace. The interface * here is not entirely unlike that of java.net.Socket */ -public class LocalSocket { +public class LocalSocket implements Closeable { private LocalSocketImpl impl; private volatile boolean implCreated; @@ -167,6 +168,7 @@ public class LocalSocket { * * @throws IOException */ + @Override public void close() throws IOException { implCreateIfNeeded(); impl.close(); diff --git a/services/java/com/android/server/connectivity/Vpn.java b/services/java/com/android/server/connectivity/Vpn.java index b12d5976bf91a..d788eba9de444 100644 --- a/services/java/com/android/server/connectivity/Vpn.java +++ b/services/java/com/android/server/connectivity/Vpn.java @@ -53,6 +53,8 @@ import java.io.OutputStream; import java.nio.charset.Charsets; import java.util.Arrays; +import libcore.io.IoUtils; + /** * @hide */ @@ -228,11 +230,7 @@ public class Vpn extends INetworkManagementEventObserver.Stub { mConnection = connection; mInterface = interfaze; } catch (RuntimeException e) { - try { - tun.close(); - } catch (Exception ex) { - // ignore - } + IoUtils.closeQuietly(tun); throw e; } Log.i(TAG, "Established by " + config.user + " on " + mInterface); @@ -442,11 +440,7 @@ public class Vpn extends INetworkManagementEventObserver.Stub { // We assume that everything is reset after stopping the daemons. interrupt(); for (LocalSocket socket : mSockets) { - try { - socket.close(); - } catch (Exception e) { - // ignore - } + IoUtils.closeQuietly(socket); } }