Java tutorial
//package com.java2s; import java.io.Closeable; import java.net.DatagramSocket; import java.net.Socket; import android.util.Log; public class Main { static final String TAG = "Helper"; public static void closeSilently(Object... xs) { // Note: on Android API levels prior to 19 Socket does not implement Closeable for (Object x : xs) { if (x != null) { try { Log.d(TAG, "closing: " + x); if (x instanceof Closeable) { ((Closeable) x).close(); } else if (x instanceof Socket) { ((Socket) x).close(); } else if (x instanceof DatagramSocket) { ((DatagramSocket) x).close(); } else { Log.d(TAG, "cannot close: " + x); throw new RuntimeException("cannot close " + x); } } catch (Throwable e) { Log.e(TAG, e.getMessage()); } } } } }