mirror of
https://github.com/nextcloud/android.git
synced 2024-11-28 18:28:59 +03:00
Use try-with-resources to autoclose closable resources
This commit is contained in:
parent
cb0429c00a
commit
6f7fc2de8e
5 changed files with 3 additions and 88 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<VCard> 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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue