distinguish between general i/o error and write exception when copying files

This commit is contained in:
Daniel Gultsch 2016-11-15 15:43:04 +01:00
parent d61b00604d
commit e84af51272
3 changed files with 27 additions and 13 deletions

View file

@ -25,6 +25,7 @@ import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.AbstractConnectionManager;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.FileWriterException;
public class HttpDownloadConnection implements Transferable { public class HttpDownloadConnection implements Transferable {
@ -141,16 +142,12 @@ public class HttpDownloadConnection implements Transferable {
mXmppConnectionService.updateConversationUi(); mXmppConnectionService.updateConversationUi();
} }
private class WriteException extends IOException {
}
private void showToastForException(Exception e) { private void showToastForException(Exception e) {
if (e instanceof java.net.UnknownHostException) { if (e instanceof java.net.UnknownHostException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_server_not_found);
} else if (e instanceof java.net.ConnectException) { } else if (e instanceof java.net.ConnectException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_connect);
} else if (e instanceof WriteException) { } else if (e instanceof FileWriterException) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_could_not_write_file);
} else if (!(e instanceof CancellationException)) { } else if (!(e instanceof CancellationException)) {
mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found); mXmppConnectionService.showErrorToastInUi(R.string.download_failed_file_not_found);
@ -305,7 +302,7 @@ public class HttpDownloadConnection implements Transferable {
try { try {
os.write(buffer, 0, count); os.write(buffer, 0, count);
} catch (IOException e) { } catch (IOException e) {
throw new WriteException(); throw new FileWriterException();
} }
updateProgress((int) ((((double) transmitted) / expected) * 100)); updateProgress((int) ((((double) transmitted) / expected) * 100));
if (canceled) { if (canceled) {
@ -315,7 +312,7 @@ public class HttpDownloadConnection implements Transferable {
try { try {
os.flush(); os.flush();
} catch (IOException e) { } catch (IOException e) {
throw new WriteException(); throw new FileWriterException();
} }
} catch (CancellationException | IOException e) { } catch (CancellationException | IOException e) {
throw e; throw e;

View file

@ -54,6 +54,7 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.ExifHelper; import eu.siacs.conversations.utils.ExifHelper;
import eu.siacs.conversations.utils.FileUtils; import eu.siacs.conversations.utils.FileUtils;
import eu.siacs.conversations.utils.FileWriterException;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
public class FileBackend { public class FileBackend {
@ -238,11 +239,21 @@ public class FileBackend {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int length; int length;
while ((length = is.read(buffer)) > 0) { while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length); try {
os.write(buffer, 0, length);
} catch (IOException e) {
throw new FileWriterException();
}
}
try {
os.flush();
} catch (IOException e) {
throw new FileWriterException();
} }
os.flush();
} catch(FileNotFoundException e) { } catch(FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found); throw new FileCopyException(R.string.error_file_not_found);
} catch(FileWriterException e) {
throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
throw new FileCopyException(R.string.error_io_exception); throw new FileCopyException(R.string.error_io_exception);
@ -287,8 +298,13 @@ public class FileBackend {
InputStream is = null; InputStream is = null;
OutputStream os = null; OutputStream os = null;
try { try {
file.createNewFile(); if (!file.exists() && !file.createNewFile()) {
throw new FileCopyException(R.string.error_unable_to_create_temporary_file);
}
is = mXmppConnectionService.getContentResolver().openInputStream(image); is = mXmppConnectionService.getContentResolver().openInputStream(image);
if (is == null) {
throw new FileCopyException(R.string.error_not_an_image_file);
}
Bitmap originalBitmap; Bitmap originalBitmap;
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
int inSampleSize = (int) Math.pow(2, sampleSize); int inSampleSize = (int) Math.pow(2, sampleSize);
@ -315,7 +331,6 @@ public class FileBackend {
quality -= 5; quality -= 5;
} }
scaledBitmap.recycle(); scaledBitmap.recycle();
return;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new FileCopyException(R.string.error_file_not_found); throw new FileCopyException(R.string.error_file_not_found);
} catch (IOException e) { } catch (IOException e) {
@ -330,8 +345,6 @@ public class FileBackend {
} else { } else {
throw new FileCopyException(R.string.error_out_of_memory); throw new FileCopyException(R.string.error_out_of_memory);
} }
} catch (NullPointerException e) {
throw new FileCopyException(R.string.error_io_exception);
} finally { } finally {
close(os); close(os);
close(is); close(is);

View file

@ -0,0 +1,4 @@
package eu.siacs.conversations.utils;
public class FileWriterException extends Exception {
}