[Android] Properly close streams

Geoffrey Métais git at videolan.org
Tue Feb 17 17:43:49 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 17 17:33:55 2015 +0100| [fb738dd0a65c12c465dbac0f5b6b8a7b6de153db] | committer: Geoffrey Métais

Properly close streams

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=fb738dd0a65c12c465dbac0f5b6b8a7b6de153db
---

 vlc-android/src/org/videolan/vlc/VLCCrashHandler.java    |    9 +++------
 .../src/org/videolan/vlc/gui/DebugLogService.java        |   14 ++++----------
 .../src/org/videolan/vlc/gui/NativeCrashActivity.java    |    4 ++--
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java        |    4 +---
 .../src/org/videolan/vlc/util/AndroidDevices.java        |    7 +------
 vlc-android/src/org/videolan/vlc/util/Logcat.java        |   12 ++++++------
 vlc-android/src/org/videolan/vlc/util/Util.java          |    9 ++++++---
 7 files changed, 23 insertions(+), 36 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java b/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
index 764feaf..0d1d897 100644
--- a/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
+++ b/vlc-android/src/org/videolan/vlc/VLCCrashHandler.java
@@ -31,6 +31,7 @@ import java.io.Writer;
 import java.lang.Thread.UncaughtExceptionHandler;
 
 import org.videolan.vlc.util.Logcat;
+import org.videolan.vlc.util.Util;
 
 import android.os.Environment;
 import android.text.format.DateFormat;
@@ -97,12 +98,8 @@ public class VLCCrashHandler implements UncaughtExceptionHandler {
         } catch (IOException e) {
             e.printStackTrace();
         }finally {
-            try {
-                bw.close();
-                output.close();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+            Util.close(bw);
+            Util.close(output);
         }
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/DebugLogService.java b/vlc-android/src/org/videolan/vlc/gui/DebugLogService.java
index d399d0a..8a2f4a6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DebugLogService.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DebugLogService.java
@@ -30,6 +30,7 @@ import java.util.List;
 
 import org.videolan.vlc.R;
 import org.videolan.vlc.util.Logcat;
+import org.videolan.vlc.util.Util;
 
 import android.app.Notification;
 import android.app.PendingIntent;
@@ -191,16 +192,9 @@ public class DebugLogService extends Service implements Logcat.Callback, Runnabl
         } catch (IOException ioe) {
             saved = false;
         } finally {
-            try {
-                if (bw != null)
-                    bw.close();
-                if (output != null)
-                    output.close();
-                if (fos != null)
-                    fos.close();
-            } catch (IOException e) {
-                saved = false;
-            }
+            saved &= Util.close(bw);
+            saved &= Util.close(output);
+            saved &= Util.close(fos);
         }
         synchronized (this) {
             mSaveThread = null;
diff --git a/vlc-android/src/org/videolan/vlc/gui/NativeCrashActivity.java b/vlc-android/src/org/videolan/vlc/gui/NativeCrashActivity.java
index c71022b..f72118a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/NativeCrashActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/NativeCrashActivity.java
@@ -131,9 +131,9 @@ public class NativeCrashActivity extends Activity {
             ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
             GZIPOutputStream gos = new GZIPOutputStream(os);
             gos.write(string.getBytes());
-            gos.close();
+            Util.close(gos);
             byte[] compressed = os.toByteArray();
-            os.close();
+            Util.close(os);
             return compressed;
         }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
index 41ebc09..a2645e6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -295,9 +295,7 @@ public class AudioUtil {
         } catch (Exception e) {
             Log.e(TAG, "writeBitmap failed : "+ e.getMessage());
         } finally {
-            if (out != null) {
-                out.close();
-            }
+            Util.close(out);
         }
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index 183f70c..826ad88 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -134,12 +134,7 @@ public class AndroidDevices {
         catch (FileNotFoundException e) {}
         catch (IOException e) {}
         finally {
-            if (bufReader != null) {
-                try {
-                    bufReader.close();
-                }
-                catch (IOException e) {}
-            }
+            Util.close(bufReader);
         }
         return list;
     }
diff --git a/vlc-android/src/org/videolan/vlc/util/Logcat.java b/vlc-android/src/org/videolan/vlc/util/Logcat.java
index 8c299bb..de4bcbf 100644
--- a/vlc-android/src/org/videolan/vlc/util/Logcat.java
+++ b/vlc-android/src/org/videolan/vlc/util/Logcat.java
@@ -132,10 +132,10 @@ public class Logcat implements Runnable {
             }
         }catch(Exception e) {}
         finally {
-            bw.close();
-            output.close();
-            br.close();
-            input.close();
+            Util.close(bw);
+            Util.close(output);
+            Util.close(br);
+            Util.close(input);
         }
     }
 
@@ -158,8 +158,8 @@ public class Logcat implements Runnable {
         while ((line = br.readLine()) != null)
             log.append(line + "\n");
 
-        br.close();
-        input.close();
+        Util.close(br);
+        Util.close(input);
 
         return log.toString();
     }
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 28f0cc8..2ac0320 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -281,10 +281,13 @@ public class Util {
         return deleted;
     }
 
-    public static void close(Closeable closeable) {
+    public static boolean close(Closeable closeable) {
         try {
             closeable.close();
-        } catch (IOException e) {}
-        catch (NullPointerException e) {}
+            return true;
+        } catch (IOException e) {
+            return false;
+        } catch (NullPointerException e) {
+            return false;}
     }
 }



More information about the Android mailing list