Skip to content

Commit ba354b6

Browse files
committed
Move moveFile methods from ExportAsyncTask to FileUtils
1 parent c3ae895 commit ba354b6

2 files changed

Lines changed: 54 additions & 48 deletions

File tree

app/src/main/java/org/gnucash/android/export/ExportAsyncTask.java

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.crashlytics.android.Crashlytics;
3636
import com.dropbox.core.v2.DbxClientV2;
3737
import com.dropbox.core.v2.files.FileMetadata;
38-
import com.dropbox.core.v2.files.Metadata;
3938
import com.google.android.gms.common.api.GoogleApiClient;
4039
import com.google.android.gms.drive.Drive;
4140
import com.google.android.gms.drive.DriveApi;
@@ -68,10 +67,8 @@
6867

6968
import java.io.File;
7069
import java.io.FileInputStream;
71-
import java.io.FileOutputStream;
7270
import java.io.IOException;
7371
import java.io.OutputStream;
74-
import java.nio.channels.FileChannel;
7572
import java.text.SimpleDateFormat;
7673
import java.util.ArrayList;
7774
import java.util.Date;
@@ -282,7 +279,7 @@ private void moveExportToUri() throws Exporter.ExporterException {
282279
try {
283280
OutputStream outputStream = mContext.getContentResolver().openOutputStream(exportUri);
284281
// Now we always get just one file exported (QIFs are zipped)
285-
moveFile(mExportedFiles.get(0), outputStream);
282+
org.gnucash.android.util.FileUtils.moveFile(mExportedFiles.get(0), outputStream);
286283
} catch (IOException ex) {
287284
throw new Exporter.ExporterException(mExportParams, "Error when moving file to URI");
288285
}
@@ -429,7 +426,7 @@ private List<String> moveExportToSDCard() throws Exporter.ExporterException {
429426
for (String src: mExportedFiles) {
430427
String dst = Exporter.getExportFolderPath(mExporter.mBookUID) + stripPathPart(src);
431428
try {
432-
moveFile(src, dst);
429+
org.gnucash.android.util.FileUtils.moveFile(src, dst);
433430
dstFiles.add(dst);
434431
} catch (IOException e) {
435432
throw new Exporter.ExporterException(mExportParams, e);
@@ -521,49 +518,6 @@ private ArrayList<Uri> convertFilePathsToUris(List<String> paths) {
521518
return exportFiles;
522519
}
523520

524-
/**
525-
* Moves a file from <code>src</code> to <code>dst</code>
526-
* @param src Absolute path to the source file
527-
* @param dst Absolute path to the destination file
528-
* @throws IOException if the file could not be moved.
529-
*/
530-
public void moveFile(String src, String dst) throws IOException {
531-
File srcFile = new File(src);
532-
File dstFile = new File(dst);
533-
FileChannel inChannel = new FileInputStream(srcFile).getChannel();
534-
FileChannel outChannel = new FileOutputStream(dstFile).getChannel();
535-
try {
536-
inChannel.transferTo(0, inChannel.size(), outChannel);
537-
} finally {
538-
if (inChannel != null)
539-
inChannel.close();
540-
outChannel.close();
541-
}
542-
srcFile.delete();
543-
}
544-
545-
/**
546-
* Move file from a location on disk to an outputstream.
547-
* The outputstream could be for a URI in the Storage Access Framework
548-
* @param src Input file (usually newly exported file)
549-
* @param outputStream Output stream to write to
550-
* @throws IOException if error occurred while moving the file
551-
*/
552-
public void moveFile(@NonNull String src, @NonNull OutputStream outputStream) throws IOException {
553-
byte[] buffer = new byte[1024];
554-
int read;
555-
try (FileInputStream inputStream = new FileInputStream(src)) {
556-
while ((read = inputStream.read(buffer)) != -1) {
557-
outputStream.write(buffer, 0, read);
558-
}
559-
} finally {
560-
outputStream.flush();
561-
outputStream.close();
562-
}
563-
Log.i(TAG, "Deleting temp export file: " + src);
564-
new File(src).delete();
565-
}
566-
567521
private void reportSuccess() {
568522
String targetLocation;
569523
switch (mExportParams.getExportTarget()){

app/src/main/java/org/gnucash/android/util/FileUtils.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package org.gnucash.android.util;
22

3+
import android.support.annotation.NonNull;
4+
import android.util.Log;
5+
6+
import org.gnucash.android.export.ExportAsyncTask;
7+
38
import java.io.File;
49
import java.io.FileInputStream;
510
import java.io.FileOutputStream;
611
import java.io.IOException;
712
import java.io.OutputStream;
13+
import java.nio.channels.FileChannel;
814
import java.util.List;
915
import java.util.zip.ZipEntry;
1016
import java.util.zip.ZipOutputStream;
@@ -13,6 +19,8 @@
1319
* Misc methods for dealing with files.
1420
*/
1521
public final class FileUtils {
22+
private static final String LOG_TAG = "FileUtils";
23+
1624
public static void zipFiles(List<String> files, String zipFileName) throws IOException {
1725
OutputStream outputStream = new FileOutputStream(zipFileName);
1826
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
@@ -31,4 +39,48 @@ public static void zipFiles(List<String> files, String zipFileName) throws IOExc
3139
}
3240
zipOutputStream.close();
3341
}
42+
43+
/**
44+
* Moves a file from <code>src</code> to <code>dst</code>
45+
* @param src Absolute path to the source file
46+
* @param dst Absolute path to the destination file
47+
* @throws IOException if the file could not be moved.
48+
*/
49+
public static void moveFile(String src, String dst) throws IOException {
50+
File srcFile = new File(src);
51+
File dstFile = new File(dst);
52+
FileChannel inChannel = new FileInputStream(srcFile).getChannel();
53+
FileChannel outChannel = new FileOutputStream(dstFile).getChannel();
54+
try {
55+
inChannel.transferTo(0, inChannel.size(), outChannel);
56+
} finally {
57+
if (inChannel != null)
58+
inChannel.close();
59+
outChannel.close();
60+
}
61+
srcFile.delete();
62+
}
63+
64+
/**
65+
* Move file from a location on disk to an outputstream.
66+
* The outputstream could be for a URI in the Storage Access Framework
67+
* @param src Input file (usually newly exported file)
68+
* @param outputStream Output stream to write to
69+
* @throws IOException if error occurred while moving the file
70+
*/
71+
public static void moveFile(@NonNull String src, @NonNull OutputStream outputStream)
72+
throws IOException {
73+
byte[] buffer = new byte[1024];
74+
int read;
75+
try (FileInputStream inputStream = new FileInputStream(src)) {
76+
while ((read = inputStream.read(buffer)) != -1) {
77+
outputStream.write(buffer, 0, read);
78+
}
79+
} finally {
80+
outputStream.flush();
81+
outputStream.close();
82+
}
83+
Log.i(LOG_TAG, "Deleting temp export file: " + src);
84+
new File(src).delete();
85+
}
3486
}

0 commit comments

Comments
 (0)