From b4df9f0cb6575b129da5c3559f29c7f88108a14a Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Mon, 24 Dec 2018 17:50:49 +0100 Subject: [PATCH] Use try-with-resource Reviewers: #kde_connect, albertvaka Reviewed By: #kde_connect, albertvaka Subscribers: kdeconnect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D17778 --- .../BluetoothBackend/BluetoothLink.java | 8 +------ .../kdeconnect/Helpers/ContactsHelper.java | 21 ++----------------- 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java index 8004a17b..815b337a 100644 --- a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java +++ b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java @@ -198,8 +198,7 @@ public class BluetoothLink extends BaseLink { sendMessage(np); if (serverSocket != null) { - BluetoothSocket transferSocket = serverSocket.accept(); - try { + try (BluetoothSocket transferSocket = serverSocket.accept()) { serverSocket.close(); int idealBufferLength = 4096; @@ -223,11 +222,6 @@ public class BluetoothLink extends BaseLink { } catch (Exception e) { callback.onFailure(e); return false; - } finally { - try { - transferSocket.close(); - } catch (IOException ignored) { - } } } diff --git a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java index d08cd002..00bc8121 100644 --- a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java +++ b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java @@ -91,12 +91,8 @@ public class ContactsHelper { } Uri photoUri = Uri.parse(photoId); - InputStream input = null; - Base64OutputStream output = null; - try { - ByteArrayOutputStream encodedPhoto = new ByteArrayOutputStream(); - output = new Base64OutputStream(encodedPhoto, Base64.DEFAULT); - input = context.getContentResolver().openInputStream(photoUri); + ByteArrayOutputStream encodedPhoto = new ByteArrayOutputStream(); + try (InputStream input = context.getContentResolver().openInputStream(photoUri); Base64OutputStream output = new Base64OutputStream(encodedPhoto, Base64.DEFAULT)) { byte[] buffer = new byte[1024]; int len; //noinspection ConstantConditions @@ -107,19 +103,6 @@ public class ContactsHelper { } catch (Exception ex) { Log.e("ContactsHelper", ex.toString()); return ""; - } finally { - try { - //noinspection ConstantConditions - input.close(); - } catch (Exception ignored) { - } - - try { - //noinspection ConstantConditions - output.close(); - } catch (Exception ignored) { - } - } }