From 6f7fc2de8ef683ded64ffe433cd1f4eabda8c92b Mon Sep 17 00:00:00 2001 From: eho Date: Tue, 27 Mar 2018 09:37:30 +0200 Subject: [PATCH] Use try-with-resources to autoclose closable resources --- .../datamodel/FilesystemDataProvider.java | 7 +-- .../android/jobs/ContactsImportJob.java | 7 +-- .../operations/DownloadFileOperation.java | 7 +-- .../com/owncloud/android/utils/IOHelper.java | 24 ---------- .../owncloud/android/utils/IOHelperTest.java | 46 ------------------- 5 files changed, 3 insertions(+), 88 deletions(-) delete mode 100644 src/main/java/com/owncloud/android/utils/IOHelper.java delete mode 100644 src/test/java/com/owncloud/android/utils/IOHelperTest.java diff --git a/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java b/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java index 906db6e74c..29c176878c 100644 --- a/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java +++ b/src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java @@ -26,7 +26,6 @@ import android.net.Uri; import com.owncloud.android.db.ProviderMeta; import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.utils.IOHelper; import java.io.BufferedInputStream; import java.io.FileInputStream; @@ -214,9 +213,7 @@ public class FilesystemDataProvider { private long getFileChecksum(String filepath) { - InputStream inputStream = null; - try { - inputStream = new BufferedInputStream(new FileInputStream(filepath)); + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filepath))){ CRC32 crc = new CRC32(); int cnt; while ((cnt = inputStream.read()) != -1) { @@ -229,8 +226,6 @@ public class FilesystemDataProvider { return -1; } catch (IOException e) { return -1; - } finally { - IOHelper.close(inputStream); } } } diff --git a/src/main/java/com/owncloud/android/jobs/ContactsImportJob.java b/src/main/java/com/owncloud/android/jobs/ContactsImportJob.java index 0651994f1b..cf5b2cd09e 100644 --- a/src/main/java/com/owncloud/android/jobs/ContactsImportJob.java +++ b/src/main/java/com/owncloud/android/jobs/ContactsImportJob.java @@ -30,7 +30,6 @@ import com.evernote.android.job.Job; import com.evernote.android.job.util.support.PersistableBundleCompat; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.ui.fragment.contactsbackup.ContactListFragment; -import com.owncloud.android.utils.IOHelper; import java.io.File; import java.io.IOException; @@ -116,9 +115,7 @@ public class ContactsImportJob extends Job { String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); VCard vCard = null; - InputStream inputStream = null; - try { - inputStream = getContext().getContentResolver().openInputStream(uri); + try (InputStream inputStream = getContext().getContentResolver().openInputStream(uri)){ ArrayList vCardList = new ArrayList<>(); vCardList.addAll(Ezvcard.parse(inputStream).all()); if (vCardList.size() > 0) { @@ -127,8 +124,6 @@ public class ContactsImportJob extends Job { } catch (IOException e) { Log_OC.d(TAG, e.getMessage()); - } finally { - IOHelper.close(inputStream); } return vCard; } diff --git a/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java b/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java index 70ff6058db..a51b20597d 100644 --- a/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java +++ b/src/main/java/com/owncloud/android/operations/DownloadFileOperation.java @@ -37,7 +37,6 @@ import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.lib.resources.files.DownloadRemoteFileOperation; import com.owncloud.android.utils.EncryptionUtils; import com.owncloud.android.utils.FileStorageUtils; -import com.owncloud.android.utils.IOHelper; import java.io.File; import java.io.FileOutputStream; @@ -202,16 +201,12 @@ public class DownloadFileOperation extends RemoteOperation { byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles() .get(mFile.getEncryptedFileName()).getAuthenticationTag()); - FileOutputStream fileOutputStream = null; - try { + try (FileOutputStream fileOutputStream = new FileOutputStream(tmpFile)){ byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag); - fileOutputStream = new FileOutputStream(tmpFile); fileOutputStream.write(decryptedBytes); } catch (Exception e) { return new RemoteOperationResult(e); - } finally { - IOHelper.close(fileOutputStream); } } moved = tmpFile.renameTo(newFile); diff --git a/src/main/java/com/owncloud/android/utils/IOHelper.java b/src/main/java/com/owncloud/android/utils/IOHelper.java deleted file mode 100644 index a17da1f16b..0000000000 --- a/src/main/java/com/owncloud/android/utils/IOHelper.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.owncloud.android.utils; - -import com.owncloud.android.lib.common.utils.Log_OC; - -import java.io.Closeable; -import java.io.IOException; - -/** - * Static system IO helper methods - */ - -public class IOHelper { - private static final String TAG = IOHelper.class.getSimpleName(); - - public static void close(Closeable c) { - if (c == null) return; - try { - c.close(); - } catch (IOException e) { - Log_OC.e(TAG, "Error closing stream", e); - } - } - -} diff --git a/src/test/java/com/owncloud/android/utils/IOHelperTest.java b/src/test/java/com/owncloud/android/utils/IOHelperTest.java deleted file mode 100644 index ae275112ab..0000000000 --- a/src/test/java/com/owncloud/android/utils/IOHelperTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.owncloud.android.utils; - -import junit.framework.Assert; - -import org.junit.Test; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import static junit.framework.Assert.assertEquals; - -/** - * Unit tests for the ({@link IOHelper}) class. - */ - -public class IOHelperTest { - - private final static String outputFileName = "output.txt"; - - @Test - public void testInputStreamIsClosed() throws Exception { - // Define FileOutputStream for testing. - FileOutputStream mOutputStream = null; - try { - // init object - mOutputStream = new FileOutputStream(outputFileName); - - } catch (Exception ex) { - ex.printStackTrace(); - } finally { - // Close output stream. - IOHelper.close(mOutputStream); - } - // try to write to stream. This should fail since we have now closed the output stream. - try { - mOutputStream.write("Hello World".getBytes()); - Assert.fail("No exception was thrown"); - } catch (IOException e) { - assertEquals(e.getMessage(), "Stream Closed"); - } - - Files.deleteIfExists(Paths.get(outputFileName)); - } -}