diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-09 10:00:48 +0200 |
---|---|---|
committer | Michael Weghorn <m.weghorn@posteo.de> | 2021-04-12 07:19:26 +0200 |
commit | 224df2980f0626b1e0b7f40c14348771ffff58fe (patch) | |
tree | 59cd3f6c74aeb61f551a717e151e5f9f95c3ee16 /android | |
parent | 79d9a7349174a7e2b2aa7fcf324a6987edbbd555 (diff) |
android: Extract method to copy stream
Extract method 'copyStream' used to copy the
temporary file to the actual document URI when
saving.
It will also be used for copying the other way around
when initially opening the document.
Change-Id: I5382f4a7c49b454ff38fb8f95afab3c39145c11f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113879
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
Diffstat (limited to 'android')
-rw-r--r-- | android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java index 2a391dab4c8d..2f067b48f90b 100644 --- a/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java +++ b/android/source/src/java/org/libreoffice/LibreOfficeMainActivity.java @@ -44,6 +44,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; @@ -368,21 +369,15 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin if (isReadOnlyMode() || mInputFile == null || mDocumentUri == null || !mDocumentUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) return; - FileInputStream inputStream = null; - OutputStream outputStream = null; - + boolean copyOK = false; try { - inputStream = new FileInputStream(mInputFile); - // OutputStream for the actual (original) location - outputStream = getContentResolver().openOutputStream(mDocumentUri); - - byte[] buffer = new byte[4096]; - int readBytes = inputStream.read(buffer); - while (readBytes != -1) { - outputStream.write(buffer, 0, readBytes); - readBytes = inputStream.read(buffer); - } - + final FileInputStream inputStream = new FileInputStream(mInputFile); + final OutputStream outputStream = getContentResolver().openOutputStream(mDocumentUri); + copyOK = copyStream(inputStream, outputStream); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + if (copyOK) { runOnUiThread(new Runnable() { @Override public void run() { @@ -391,7 +386,7 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin } }); setDocumentChanged(false); - } catch (Exception e) { + } else { runOnUiThread(new Runnable() { @Override public void run() { @@ -399,16 +394,6 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin Toast.LENGTH_SHORT).show(); } }); - e.printStackTrace(); - } finally { - try { - if (inputStream != null) - inputStream.close(); - if (outputStream != null) - outputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } } } @@ -946,6 +931,33 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin } } + /** + * Copies everything from the given input stream to the given output stream + * and closes both streams in the end. + * @return Whether copy operation was successful. + */ + private boolean copyStream(InputStream inputStream, OutputStream outputStream) { + try { + byte[] buffer = new byte[4096]; + int readBytes = inputStream.read(buffer); + while (readBytes != -1) { + outputStream.write(buffer, 0, readBytes); + readBytes = inputStream.read(buffer); + } + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } finally { + try { + inputStream.close(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + public void showCustomStatusMessage(String message){ Snackbar.make(mDrawerLayout, message, Snackbar.LENGTH_LONG).show(); } @@ -990,7 +1002,6 @@ public class LibreOfficeMainActivity extends AppCompatActivity implements Settin protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } - } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |